Spring中IoC - 两种ApplicationContext加载Bean的配置
2015-03-18 13:32
507 查看
说明:SpringIoC其实就是在Service的实现中定义了一些以来的策略类,这些策略类不是通过初始化、Setter、工厂方法来确定的。而是通过一个叫做上下文的(ApplicationContext)组建来加载进来的。这里介绍两种Context组建的构件过程
前提条件:在Gradle工程的build.gradle文件中引入对Springframework的支持
第一种方式:AnnotationConfigApplicationContext类加载JavaClassFile的配置文件
配置文件JavaClass如下:
@Configuration:表示这是一个配置文件
@Bean:表示这是一个Bean(名字为方法名),将来他要用来与Service中的@Autowired属性配对,注意配对的时候是根据返回类型来对应的,也就是说所有的Service中但凡有@Autowired的属性,他们都是从这个配置文件中拿到的。
再来看一下使用@AutoWired的Service类。这个AutoWired将与上面配置文件中的@Bean结成一对儿
最后来看Main函数是如何将配置文件与Service文件结合在一起的。很简单
第二种方式:通过ClassPathXmlApplicationContext加载xml配置文件
首先是在resources/META-INFO/Config下面新建一个Beansdefinition的xml如下(注意这个地址至今我也不理解,放在其他的目录下老失败。好吧先mark了)
Property中定义了每个Bean中涉及的Property。注意这个地方因为是对属性的赋值,所以在你的Service类中必须支持Setter方法
在Main方法中使用其实也很简单
前提条件:在Gradle工程的build.gradle文件中引入对Springframework的支持
repositories{ mavenCentral() } dependencies{ compilegroup:'org.springframework',name:'spring-context',version:'4.1.5.RELEASE' compilegroup:'org.springframework',name:'spring-core',version:'4.1.5.RELEASE' testCompilegroup:'junit',name:'junit',version:'4.11' }
第一种方式:AnnotationConfigApplicationContext类加载JavaClassFile的配置文件
配置文件JavaClass如下:
@Configuration:表示这是一个配置文件
@Bean:表示这是一个Bean(名字为方法名),将来他要用来与Service中的@Autowired属性配对,注意配对的时候是根据返回类型来对应的,也就是说所有的Service中但凡有@Autowired的属性,他们都是从这个配置文件中拿到的。
@Configuration publicclassApplicationContextConfiguration{ @Bean publicAccountRepoisitoryaccountRepoisitory(){ returnnewAccountRepositoryImp(); } @Bean publicTransactionServicetransactionService(){ returnnewTransactionServiceImp(); } @Bean publicTransferServicetransferService(){ returnnewTransferServiceImp(); } }
再来看一下使用@AutoWired的Service类。这个AutoWired将与上面配置文件中的@Bean结成一对儿
publicclassTransactionServiceImpimplementsTransactionService{ @Autowired publicAccountRepoisitoryaccountRepoisitory; @Override publicvoidNewTransaction(StringaccountId1,StringaccountId2,doublemoney){ Accountaccount1=accountRepoisitory.GetAccountByAccountId(accountId1); Accountaccount2=accountRepoisitory.GetAccountByAccountId(accountId2); Transactiontransaction=newTransaction(){}; transaction.fromAccount=account1; transaction.toAccount=account2; transaction.moneyTransfered=money; transaction.transactionDate=Calendar.getInstance().getTime(); BankFactory.Transactions.add(transaction); } }
最后来看Main函数是如何将配置文件与Service文件结合在一起的。很简单
ApplicationContextcontext= newAnnotationConfigApplicationContext( com.ctrip.configuration.ApplicationContextConfiguration.class ); //接下来我们就可以使用任何Service中定义的方法了
AccountRepoisitoryaccountRepoisitory=context.getBean("accountRepoisitory", AccountRepositoryImp.class); TransactionServicetransactionService=context.getBean("transactionService", TransactionServiceImp.class); TransferServicetransferService=context.getBean("transferService", TransferServiceImp.class ); transferService.Transfer("1","2",234);
第二种方式:通过ClassPathXmlApplicationContext加载xml配置文件
首先是在resources/META-INFO/Config下面新建一个Beansdefinition的xml如下(注意这个地址至今我也不理解,放在其他的目录下老失败。好吧先mark了)
Property中定义了每个Bean中涉及的Property。注意这个地方因为是对属性的赋值,所以在你的Service类中必须支持Setter方法
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<beanid="accountRepoisitory"class="com.ctrip.depends.AccountRepositoryImp"></bean>
<beanid="transactionService"class="com.ctrip.depends.TransactionServiceImp">
<propertyname="accountRepoisitory"ref="accountRepoisitory"></property>
</bean>
<beanid="transferService"class="com.ctrip.depends.TransferServiceImp">
<propertyname="accountRepoisitory"ref="accountRepoisitory"></property>
<propertyname="transactionService"ref="transactionService"></property>
</bean>
</beans>
在Main方法中使用其实也很简单
ApplicationContextcontext=newClassPathXmlApplicationContext("classpath:/META-INF/config/application-context.xml");
Transfer(context);
//通过以上的步骤我们告诉了每个Service的存在和他们所依赖的Service,接下来可以使用任何Service中的方法了
AccountRepoisitoryaccountRepoisitory=context.getBean("accountRepoisitory", AccountRepositoryImp.class); TransactionServicetransactionService=context.getBean("transactionService", TransactionServiceImp.class); TransferServicetransferService=context.getBean("transferService", TransferServiceImp.class ); transferService.Transfer("1","2",234);
相关文章推荐
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
- 通过DefaultListableBeanFactory加载.xml配置文件学习Spring-IoC容器注册/加载bean的机制(源码走读)
- spring学习总结(六):IOC & DI 配置Bean之作用域、加载外部属性文件、SPEL
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
- Spring加载一些配置文件中不存在的bean,如DB2,SYBASE,Derby,H2等等,Error while extracting database product name
- Spring攻略学习笔记(2)------配置Spring IoC容器中的Bean
- 【Spring】IOC之基于注解的配置bean(上)
- 通过ApplicationContextAwareSpring实现手工加载配置的javabean
- spring加载配置文件的两种方式
- 快速上手Spring--3. 加载Bean的配置文件
- 快速上手Spring--3. 加载Bean的配置文件
- 10004--Spring 中的 Bean 配置--IOC 和 DI
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Spring声明式事务配置的两种策略SpringAop和Bean后处理器的代理BeanNameAutoProxyCreator
- spring在javaWeb环境中加载 bean 配置文件方式的选择
- spring加载配置文件的 两种方式
- 快速上手Spring--3. 加载Bean的配置文件
- 2、Spring4之Bean的两种配置方式
- Spring IOC-业务对象bean加载到bean工厂
- [Spring][Bean]Spring配置文件、属性注入、从IOC容器获取实例