1. 程式人生 > >裝配Bean:XML與JavaConfig混合裝配

裝配Bean:XML與JavaConfig混合裝配

IOC容器裝載Beans有三種方式:
【顯示】JavaConfig(當前流行)
【顯示】XML(古老的|不可替代的)
【隱式】自動裝配(註解掃描)
實際使用往往是交叉的
本節內容:
- 如何使用多個JavaConfig類進行配置
- 如何在JavaConfig中引入xml配置
- 如何在xml中引入JavaConfig配置

如何使用多個JavaConfig類進行配置
就像XML方式配置bean類似,如何將專案所有Bean都新增到同一個JavaConfig會顯得十分臃腫。那麼如何拆分呢?XML中可以使用<import>

,所以在JavaConfig類中我們可以使用@import
程式碼如下
Beans相關程式碼
介面類MobilePhone

package com.xbz.learn.spring.autowire;

public interface MobilePhone {
    /**
     * 給XXX打電話
     * @param phoneNumber
     */
    public void callPerson(String phoneNumber);
}

介面類Computer

package com.xbz.learn.spring.autowire;

/**
 * 電腦抽象介面
 * @author
xbz * */
public interface Computer { /** * 上網衝浪 */ public void surfInternet(); }

介面實現類

package com.xbz.learn.spring.autowire;

/**
 * MobilePhone介面的iphone實現類
 * @author xbz
 *
 */
public class Iphone implements MobilePhone{

    public void callPerson(String phoneNumber) {
        System.out.println("iphone 正在向【"
+phoneNumber+"】打電話"); } }
package com.xbz.learn.spring.autowire;

public class XiaoMiPhone implements MobilePhone {

    public void callPerson(String phoneNumber) {
        System.out.println("小米手機 正在向【"+phoneNumber+"】打電話");
    }

}
package com.xbz.learn.spring.autowire;

public class MacComputer implements Computer{

    public void surfInternet() {
        System.out.println("用Mac上網非常爽!!!");
    }

}
package com.xbz.learn.spring.autowire;

public class LenovoComputer implements Computer{

    public void surfInternet() {
        System.out.println("用聯想電腦上網,好燙啊!!!");
    }

}

JavaConfig

package com.xbz.learn.spring.autowire.javaConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xbz.learn.spring.autowire.Computer;
import com.xbz.learn.spring.autowire.LenovoComputer;
import com.xbz.learn.spring.autowire.MacComputer;

@Configuration
public class ComputerConfig {
    @Bean
    public Computer mac(){
        System.out.println("得到一個Mac電腦");
        return new MacComputer();
    }
    @Bean
    public Computer lenovo(){
        System.out.println("得到一個聯想電腦");
        return new LenovoComputer();
    }
}
package com.xbz.learn.spring.autowire.javaConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xbz.learn.spring.autowire.Iphone;
import com.xbz.learn.spring.autowire.MobilePhone;
import com.xbz.learn.spring.autowire.XiaoMiPhone;

@Configuration
public class MobilePhoneConfig {

    @Bean
    public MobilePhone iphone(){
        System.out.println("得到一個iphone手機");
        return new Iphone(); 
    }

    @Bean
    public MobilePhone xiaomi(){
        System.out.println("得到一個小米手機");
        return new XiaoMiPhone(); 
    }
}

注意點:@import 我們最終的根Configuration類

package com.xbz.learn.spring.autowire.javaConfig;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ComputerConfig.class,MobilePhoneConfig.class})
public class SpringJavaConfig {

}

測試類

package com.xbz.learn.spring.autowire.javaConfig;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xbz.learn.spring.autowire.Computer;
import com.xbz.learn.spring.autowire.MobilePhone;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SpringJavaConfig.class})
public class SpringJavaConfigTest {

    @Autowired
    private MobilePhone iphone;

    @Autowired
    private MobilePhone xiaomi;

    @Autowired
    private Computer lenovo;

    @Autowired
    private Computer mac;


    @Test
    public void testPhoneConfig(){
        System.out.println("iphone:"+iphone);
        System.out.println("xiaomi:"+xiaomi);
        iphone.callPerson("iphone");
        xiaomi.callPerson("xiaomi");
        System.out.println("is same?"+(iphone==xiaomi));
    }

    @Test
    public void testComputerConfig(){
        lenovo.surfInternet();
        mac.surfInternet();
    }
}

混合裝配方式:現在我們將MobilePhone的Beans配置在xml中,如何使用JavaConfig
xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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:c="http://www.springframework.org/schema/c"

    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!-- MobilePhone相關實現類 -->
    <bean id="iphone" class="com.xbz.learn.spring.autowire.Iphone"></bean>
    <bean id="xiaomi" class="com.xbz.learn.spring.autowire.XiaoMiPhone"></bean>

</beans>

在JavaConfig類中使用@ImportResource指示載入xml的路徑

package com.xbz.learn.spring.autowire.javaConfig;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

@Configuration
@Import({ComputerConfig.class})
@ImportResource(locations={"classpath:spring/spring-beans.xml"})
public class SpringJavaConfig {

}

如何在xml引入JavaConfig類呢?
再簡單不過了,JavaConfig首先他就是Java類,我們只要配置一個Bean不就可以了嗎!

<!-- 引入JavaConfig -->
<bean class="com.xbz.learn.spring.autowire.javaConfig.SpringJavaConfig"></bean>