自动装配bean 的四种方式:
首先定义一个类,使用@Configuration 标注类成为配置类,然后有下面四种方式:
1.配置类使用@ComponentScan 注解扫描指定包下的类,默认是扫描同包下的类,使用@Component 标注实现类
2.配置类方法中使用@Bean 注解返回接口类类型的方法
3.使用配置文件,配置类使用@ImportResource("classpath:xxx.xml") 指定xml配置文件(resource路径下编译在classpath路径),xml 文件中使用<bean class="com.qhf.AOP.PerformanceImpl"/>
4.配置类使用@ImportResource("classpath:xxx.xml") 指定xml配置文件,实现类中使用@Component 注解,xml 文件中使用<context:component-scan base-package="com.qhf.bean.soundsystem01"/>
使用@Autowired 根据类型自动装配
实例:CD专辑放入播放机播放
常规定义实现:
1.定义CD接口
package com.qhf.soundsystem;
public interface CD {
void play();
}
2.定义艾薇儿的CD专辑
package com.qhf.soundsystem;
public class AvrilCD implements CD {
public void play() {
System.out.println("AvrilCD playing...");
}
}
3.播放CD
public class Test{
public static void main(String[] args) {
CD avrilCD = new AvrilCD();
avrilCD.play();
}
}
4.结果:
AvrilCD playing...
上面例子是我们正常的思路,需要CD的实例,才能去调用播放。
下面通过Spring的 组件扫描(component scanning),自动装配(autowiring)来实现自动装配。
第一种方式:配置类使用@ComponentScan 注解扫描指定包下的类,默认是扫描同包下的类,使用@Component 标注实现类
1.定义CD接口
package com.qhf.soundsystem;
public interface CD {
void play();
}
2.定义艾薇儿的CD专辑
@Component
public class AvrilCD implements CD {
public void play() {
System.out.println("AvrilCD");
}
}
3.定义配置类,注解@ComponentScan启动组件扫描
@Configuration //JavaConfig,相当于xml配置文件
@ComponentScan //默认扫描与配置类相同的包,即此类的同包下的带有@Component注解的类,自定义包使用@ComponentScan(basePackages={"com.qhf.soundsystem","xxx"})
public class CDPlayerConfig {
}
4.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)//指定配置文件
public class CDPlayerTest {
@Autowired//@Inject 也是可以的,是java依赖注入规范,CD子类实例化,Autowired自动注入符合条件的bean,多个子类有Component标注会报错
private CD cd;
@Test
public void cdShouldNotBeNull(){
assertNotNull(cd);
cd.play();
}
}
5.结果:
AvrilCD
第二种方式:配置类方法中使用@Bean 注解返回接口类类型的方法
1.接口
package com.qhf.bean.soundsystem01;
public interface CD {
void play();
}
2.实现CD 接口
package com.qhf.bean.soundsystem03;
public class AvrilCD implements CD {
public void play() {
System.out.println("AvrilCD");
}
}
3.配置类使用@Bean 注解
package com.qhf.bean.soundsystem03;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 当类是用外部引入的类时,难以在类中添加@Component标注,所以不能通过扫描标注来扫描,要下面这样做
*/
@Configuration
public class CDPlayerConfig {
@Bean
public CD avrilCD(){
return new AvrilCD();
}
}
4.测试
package com.qhf.bean.soundsystem01;
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 static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CD cd;
@Test
public void cdShouldNotBeNull(){
assertNotNull(cd);
cd.play();
}
}
结果:
AvrilCD
第三种方式:使用配置文件,配置类使用@ImportResource("classpath:xxx.xml") 指定xml配置文件(resource路径下编译在classpath路径),xml 文件中使用<bean class="com.qhf.AOP.PerformanceImpl"/>
1.接口
package com.qhf.bean.soundsystem01;
public interface CD {
void play();
}
2.实现接口
package com.qhf.bean.soundsystem01;
public class AvrilCD implements CD {
public void play() {
System.out.println("AvrilCD");
}
}
3.配置类
package com.qhf.bean.soundsystem01;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:soundsystem01/bean.xml")
public class CDPlayerConfig {
}
4.bean.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.xsd">
<bean class="com.qhf.bean.soundsystem01.AvrilCD"/>
</beans>
5.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CD cd;
@Test
public void cdShouldNotBeNull(){
assertNotNull(cd);
cd.play();
}
}
6.结果:
AvrilCD
第四种方式:
实现类:
@Component
public class AvrilCD implements CD {
public void play() {
System.out.println("AvrilCD");
}
}
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.xsd">
<context:component-scan base-package="com.qhf.bean.soundsystem01"/>
</beans>