spring配置和dubbo
2015-07-21 13:58
423 查看
暑假找到了实习,刚进公司就做了一个脱产的模拟系统,积分商城.在这中间的新同事用了很多以前不熟悉的东西,比如dubbo,第一个做项目不是以创建web项目的方式,而是建立一个普通的java工程,然后建立另一个表面的项目,作为接口的暴露项目,另一个项目作为实际代码的编写项目,这样处理的确可以更加的安全.收获很多,写一下收获和遇到的一些问题吧.
先来一个spring的配置文件吧:
applicationContext.xml的文件:
applicationContext-service.xml文件
Testdao1的class
还有的类和上面的类似,最后一个是供测试的类
App.class
spring获取bean的时候默认的名字首字母是小写,我之前就犯了这个错误,然后找了一上午,最后请教同事才解决了这个问题
spring的配置
以前用的都是使set/get的方式进行注入的,而在这个项目中使用的都是注解的方式。先来一个spring的配置文件吧:
applicationContext.xml的文件:
<?xml version="1.0" encoding="UTF-8"?> <!-- Application context definition. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/aop http://www.springframework.org/schema/jdbc/spring-jdbc.xsd" > <!-- 配置使用注解,这是统一的配置所有的注解,也可以单独的配置注解,比如单独配置@Autowired等 --> <context:annotation-config /> <!-- 自动扫描code包,将带有注解的类纳入spring容器管理,带有注释的比如@Autowired 等等的类加入spring容器管理 --> <context:component-scan base-package="test.code" /> <!-- 下面可以导入其他的xml文件--> <import resource="applicationContext-service.xml"/> </beans>
applicationContext-service.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!-- Application context definition. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/aop http://www.springframework.org/schema/jdbc/spring-jdbc.xsd" > <!-- bean define --> <!-- demo service for dubbo RPC --> <!-- <bean id="remoteService" class="com.pingan.demo.service.RemoteUserServiceImp" > <property name="userDao" ref="userDao"></property> </bean> DAO configuration <bean id="userDaoImpl" class="com.pingan.demo.dao.UserDaoImpl" parent="parentDao" /> <bean id="accountDao" class="com.pingan.demo.dao.AccountDaoImpl" parent="parentDao" /> <bean id="accountService" class="com.pingan.account.service.AccountServiceImpl"></bean> --> <bean id="Hello" class="test.mhctest.Hello"> <property name="name" value="Menghucheng"/> </bean> <!-- 如果不用注解的话就需要使用下面这种方式来注入 <bean id="TestDao1" class="test.code.dao.TestDao1"></bean> --> </beans>
Testdao1的class
package test.code.dao; import org.springframework.stereotype.Component; @Component /** * * @author MENGHUCHENG012 * 测试类 * 用Component注释将当前的类加入到spring的管理容器 */ public class TestDao1 implements ITestDao1 { public TestDao1(){ System.out.println("我是TestDao1的构造"); } @Override public void say1() { System.out.println("我是ITestDao1的say1方法"); } }
还有的类和上面的类似,最后一个是供测试的类
App.class
package test.mhctest; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import test.code.dao.ITestDao1; import test.code.dao.TestDao1; import test.code.service.ITestService1; import test.code.service.TestService1; /** * Hello world! * */ public class App { public static void main( String[] args ) { ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取bean的时候默认首字母是小写 TestService1 testService1 = (TestService1) act.getBean("testService1"); ITestDao1 testDao1 = (TestDao1)act.getBean("testDao1"); testService1.say(); System.out.println( "Hello World!" ); } }
spring获取bean的时候默认的名字首字母是小写,我之前就犯了这个错误,然后找了一上午,最后请教同事才解决了这个问题
相关文章推荐
- Java基础总结(二)
- java对象转为json数据在前台展示
- java数据类型讲解
- java 解析json格式数据
- JAVA 十六进制与字符串的转换
- java中JFrame类中函数addWindowListener(new WindowAdapter)
- MyEclipse注释模板设置
- Java JDK1.5 Condition方法
- 从Eclipse到Android Studio:Android项目如何进行迁移
- Spring MVC的单元测试
- SlidingMenu (一)在Eclipse中如何引入SlidingMenu和其Demo
- 修改eclipse启动时eclipse使用的jre
- 【SSH异常系列】关于SSH中连接数溢出问题的解决方法
- java基础 this 关键字!
- 在Ubuntu15.04下安装eclipse
- lambda表达式的应用例子和JavaSE 8特性
- Java调用存储过程时报 The user specified as a definer ('root'@'%') does not exist 解决方法...
- Java调用存储过程时报 The user specified as a definer ('root'@'%') does not exist 解决方法
- java生成word
- ActionBarSherlock(一)在Eclipse中如何引入ActionBarSherlock和其Demo