Spring中IoC - 兩種ApplicationContext載入Bean的配置

來源:互聯網
上載者:User

標籤:

說明:Spring IoC其實就是在Service的實現中定義了一些以來的策略類,這些策略類不是通過 初始化、Setter、Factory 方法來確定的。而是通過一個叫做內容相關的(ApplicationContext)組建來載入進來的。這裡介紹兩種Context組建的構件過程

前提條件:在Gradle工程的build.gradle檔案中引入對Spring framework 的支援

repositories {    mavenCentral()}dependencies {    compile group: ‘org.springframework‘,name: ‘spring-context‘, version: ‘4.1.5.RELEASE‘    compile group: ‘org.springframework‘,name: ‘spring-core‘, version: ‘4.1.5.RELEASE‘    testCompile group: ‘junit‘, name: ‘junit‘, version: ‘4.11‘}

 

  1. 第一種方式:AnnotationConfigApplicationContext類載入JavaClass File的設定檔

設定檔JavaClass如下:

@Configuration:表示這是一個設定檔

@Bean: 表示這是一個Bean(名字為方法名),將來他要用來與Service中的@Autowired屬性配對,注意配對的時候是根據傳回型別來對應的,也就是說所有的Service中但凡有@Autowired的屬性,他們都是從這個設定檔中拿到的。

@Configurationpublic class ApplicationContextConfiguration {    @Bean    public AccountRepoisitory accountRepoisitory() {        return new AccountRepositoryImp();    }    @Bean    public TransactionService transactionService() {        return new TransactionServiceImp();    }    @Bean    public TransferService transferService() {        return new TransferServiceImp();    }}

再來看一下使用@AutoWired的Service類。這個AutoWired將與上面設定檔中的@Bean結成一對兒

public class TransactionServiceImp implements TransactionService {    @Autowired    public  AccountRepoisitory accountRepoisitory;    @Override    public void NewTransaction(String accountId1, String accountId2, double money) {        Account account1=accountRepoisitory.GetAccountByAccountId(accountId1);        Account account2=accountRepoisitory.GetAccountByAccountId(accountId2);        Transaction transaction=new Transaction(){ };        transaction.fromAccount=account1;        transaction.toAccount=account2;        transaction.moneyTransfered=money;        transaction.transactionDate= Calendar.getInstance().getTime();        BankFactory.Transactions.add(transaction);    }}

 

最後來看Main函數是如何將設定檔與Service檔案結合在一起的。 很簡單

  ApplicationContext context=                new AnnotationConfigApplicationContext(                        com.ctrip.configuration.ApplicationContextConfiguration.class                );

// 接下來我們就可以使用任何Service中定義的方法了
AccountRepoisitory accountRepoisitory=context.getBean("accountRepoisitory",
AccountRepositoryImp.class);

TransactionService transactionService=context.getBean("transactionService",
TransactionServiceImp.class);

TransferService transferService=context.getBean("transferService",
TransferServiceImp.class
);
transferService.Transfer("1","2",234);
 

 

  1. 第二種方式:通過ClassPathXmlApplicationContext載入xml設定檔

首先是在resources/META-INFO/Config下面建立一個Beans definition 的xml如下(注意這個地址至今我也不理解,放在其他的目錄下老失敗。好吧先mark了)

Property中定義了每個Bean中涉及的Property。 注意這個地方因為是對屬性的賦值,所以在你的Service類中必須支援Setter方法

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">              <bean id="accountRepoisitory" class="com.ctrip.depends.AccountRepositoryImp"></bean>              <bean id="transactionService" class="com.ctrip.depends.TransactionServiceImp">                     <property name="accountRepoisitory" ref="accountRepoisitory"></property>              </bean>              <bean id="transferService" class="com.ctrip.depends.TransferServiceImp">                     <property name="accountRepoisitory" ref="accountRepoisitory"></property>                     <property name="transactionService" ref="transactionService"></property>              </bean></beans>

在Main方法中使用其實也很簡單

    ApplicationContext context=new ClassPathXmlApplicationContext("classpath:/META-INF/config/application-context.xml");        Transfer(context);

//通過以上的步驟我們告訴了每個Service的存在和他們所依賴的Service,接下來可以使用任何Service中的方法了
AccountRepoisitory accountRepoisitory=context.getBean("accountRepoisitory",
AccountRepositoryImp.class);

TransactionService transactionService=context.getBean("transactionService",
TransactionServiceImp.class);

TransferService transferService=context.getBean("transferService",
TransferServiceImp.class
);
transferService.Transfer("1","2",234);
 

 

Spring中IoC - 兩種ApplicationContext載入Bean的配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.