混合配置

还存留一个很重要的问题:如果根配置是xml配置,在测试的时候如果用@Autowired自动注入,对象是null,用getbean()方法就可以拿到,有可能是 不是同一个上下文,这就不明白了,先存疑吧。?????

看Spring实战(第4版)笔记

在实际的程序中,可能会有 自动配置,JavaConfig配置 和XML 配置一起使用的情况。

那最好 建一个根配置,(JavaConfig配置类或者XML配置文件),在根配置中加载所有的 JavaConfig配置类 和 xml配置文件,并且启用组件扫描(自动配置),让Spring同时加载 基于Java的配置 基于xml的配置 和自动配置

比如 我有一个自动扫描的类   两个配置类 两个XML配置文件   

    MyCompactDis类采用自动扫描配置

    CompactDis类 配置在了CDConfig配置类中  , CDPlayer类 配置在了CDPlayerConfig配置类中,

   BlankCompactDisc配置在了类路径下的bean-config.xml配置文件中  BlankCompactDisc配置在了类路径下的bean-config-1.xml配置文件中

根配置是JavaConfig类   ---@ComponentScan注解 @Import注解  @ImportRes注ource解   

  定义根配置类 @ComponentScan注解启用 自动扫描 并且 @Import  注解 引入 CDConfig配置类 和 CDPlayerConfig配置类,@ImportResource引入bean-config.xml配置文件

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

@Configuration
@ComponentScan
@Import({ CDPlayerConfig.class, CDConfig.class })
@ImportResource({"classpath:bean-config.xml","classpath:bean-config-1.xml"})
public class SystemConfig {

}

测试 

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SystemConfig.class)
public class CDPlayerTest {

	@Autowired
	private MyCompactDisc myCompactDisc;
	@Autowired
	private CompactDisc compactDisc;
	@Autowired
	private CDPlayer cdPlayer;
	 @Autowired
	 private BlankCompactDisc blankCompactDisc;
	 @Autowired
	 private NoBlankCompactDisc noBlankCompactDisc;
	@Test
	public void test() {
		myCompactDisc.play();
		compactDisc.play();
		cdPlayer.play();
		blankCompactDisc.play();
		noBlankCompactDisc.play(); 
	}
}

 

根配置是XML配置----<context:component-scan>元素 <bean>元素 <import />元素

使用<context:component-scan>元素开始自动包扫描,使用<bean>元素引入 JavaConfig配置类,使用<import />元素引入其他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:context="http://www.springframework.org/schema/context"
	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-4.2.xsd">

  <!--开启自动扫描-->
  <context:component-scan base-package="com.demo.SpringBeanDemo"/> 
  <!--引入其它JavaConfig配置类 class属性指定配置类-->
  <bean class="com.demo.SpringBeanDemo.CDConfig"></bean>
  <bean class="com.demo.SpringBeanDemo.CDPlayerConfig"></bean>
  <!--加载其他xml配置文件 resource属性指定在类路径下的配置文件 -->
  <import resource="bean-config.xml"></import>
  <import resource="bean-config-1.xml"></import>
</beans>

测试

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CDPlayerTest2 {

	static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("system-config.xml");
	 
	private MyCompactDisc myCompactDisc = (MyCompactDisc) applicationContext.getBean("myCompactDisc");
	private CompactDisc compactDisc = (CompactDisc) applicationContext.getBean("compactDisc");
	private CDPlayer cdPlayer = (CDPlayer) applicationContext.getBean("cdPlayer");

	private BlankCompactDisc blankCompactDisc = (BlankCompactDisc) applicationContext.getBean("blankCompactDisc");
	private NoBlankCompactDisc noBlankCompactDisc = (NoBlankCompactDisc) applicationContext.getBean("noBlankCompactDisc");

	@Test
	public void test() {
		myCompactDisc.play();
		compactDisc.play();
		cdPlayer.play();
		blankCompactDisc.play();
		noBlankCompactDisc.play();

	}

}

以下附上 这5个类的具体的定义和配置

定义MyCompactDisc类 并使用自动配置

import org.springframework.stereotype.Component;

@Component
public class MyCompactDisc {

	public void play() {
		System.out.println("this is auto config playing hahah by dream_s");
	}
}

 

定义CompactDisc类 并把它配置在CDConfig配置类

public class CompactDisc {
	
	private String artist = "dream_s";
	private String music = "hahah";
	
	public void play(){
		System.out.println("playing "+music +"  by  "+artist);
	}

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

@Configuration
public class CDConfig {

	@Bean
	public CompactDisc compactDisc(){
		return new CompactDisc();
	}
}

定义CDPlayer类 并把它配置在CDPlayerConfig配置类

public class CDPlayer { 
	private CompactDisc compactDisc;
	public CDPlayer(CompactDisc compactDisc) {
		this.compactDisc = compactDisc;
	}
	public void play() {
		compactDisc.play();
	}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CDPlayerConfig {
	@Bean
	public CDPlayer cdPlayer(CompactDisc compactDisc){
		return new CDPlayer(compactDisc);
	}
}

定义BlankCompactDisc类 并配置在 根路径下 的 bean-config.xml文件

public class BlankCompactDisc {
	private String artist = "dream_s";
	private String music = "hahah";

	public void play() {
		System.out.println("this is BlankCompactDisc playing " + music + "by  " + artist);
	}

}
<?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 = "blankCompactDisc" class = "com.demo.SpringBeanDemo.BlankCompactDisc"></bean> 
</beans>

定义NoBlankCompactDisc类 并配置在 类路径下 的 bean-config-1.xml文件

public class NoBlankCompactDisc {
	
public void play(){
	System.out.println("this is xml config for NoBlankCompactDisc playing hahah by dream_s ");
}

}
<?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="noBlankCompactDisc" class="com.demo.SpringBeanDemo.NoBlankCompactDisc"></bean>
</beans>

 

posted on 2019-04-16 11:41  dreamstar  阅读(66)  评论(0编辑  收藏  举报