您的位置:首页 > 移动开发

Spring中IoC - 两种ApplicationContext加载Bean的配置

2015-03-18 13:32 507 查看
说明:SpringIoC其实就是在Service的实现中定义了一些以来的策略类,这些策略类不是通过初始化、Setter、工厂方法来确定的。而是通过一个叫做上下文的(ApplicationContext)组建来加载进来的。这里介绍两种Context组建的构件过程

前提条件:在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);


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: