1.Spring Bean装配的方式
1.1.Spring有显式Java配置装配
1.2.Spring有显式的xml配置装配
1.3.Spring自动装配(主要涉及的)
2.基于Java配置自动装配
2.1.唱片接口:
package com.jack.bean.chatpter2.autowiredBeanJava;
public interface CompactDisc {
void play();
}
2.2.创建一个歌手:
package com.jack.bean.chatpter2.autowiredBeanJava;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component//(注意这里注解)
public class MrXue implements CompactDisc {
Logger logger = LoggerFactory.getLogger(MrXue.class);
private String player="薛之谦";
private String music = "暧昧";
public void play() {
logger.info("歌手为:" + player + " 曲目: " + music);
}
}
2.3.配置Java配置文件类,启动自动扫描注解
package com.jack.bean.chatpter2;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.jack.bean.chatpter2.autowiredBeanJava.MrXue;
@Configuration //表示是一个配置类
@ComponentScan //方式1:如果没有参数表示该配置类下所在包,及以下包下扫描
//@ComponentScan(basePackages={"com.jack.bean.chatpter2.autowiredBeanJava","com.jack"})//方式2:表示基于某个包下注意是全路径。也就是classPath,可以写多个包名以逗号隔开
//方式3:表示具体某个类所在包下其及子包下进行@Component注解扫描 //@ComponentScan(basePackageClasses={MrXue.class}) public class CompactDiscConfig { }
2.4测试
package com.jack.bean.chatpter2; import static org.junit.Assert.assertNotNull; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.jack.bean.chatpter2.autowiredBeanJava.CompactDisc; //在运行前自动装配bean @RunWith(SpringJUnit4ClassRunner.class) //对应配置的java类 @ContextConfiguration(classes=CompactDiscConfig.class) public class CompactDiscTest { @Autowired //当有多个候选者的时候需要指定名称,不写一般是类名第一个字母小写 @Qualifier("mrXue") private CompactDisc cd; @Test public void cdShouldNotBeNull(){ //断言是否为null assertNotNull(cd); cd.play(); } }
2.5.效果
3.基于xml配置
3.1.配置文件
<?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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"> <context:component-scan base-package="com.jack.bean.chatpter2.autowiredBeanJava"></context:component-scan> </beans>
3.2.测试类
package com.jack.bean.chatpter2; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jack.bean.chatpter2.autowiredBeanJava.CompactDisc; public class AutoAnnatationXmlTest { Logger logger = LoggerFactory.getLogger(AutoAnnatationXmlTest.class); //这里使用注解是有问题的,cd为null,原因是在没有执行加载xml配置之前就进行注入
/*@Autowired //当有多个候选者的时候需要指定名称,不写一般是类名第一个字母小写 @Qualifier("mrXue") private CompactDisc cd;*/ @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("/spring/chapter2/autoAnnatation.xml"); logger.info("========================================================================="); CompactDisc compact = (CompactDisc) context.getBean("mrXue"); compact.play(); } }
3.3.效果