在Spring中,对象无需自己查找或创建与其关联的其他对象。相反,容器负责把需要相互协作的对象引用赋予各个对象。
创建应用对象之间的协作关系的行为通常称为装配,这也是依赖注入的本质。
Spring的三种装配Bean的方式
- 在xml中进行显式配置
- 在Java中进行显式配置
- 隐式的Bean发现机制和自动装配
1、自动化装配Bean
- Spring从两个角度来实现自动化装配
- 组件扫描:Spring会自动发现应用上下文中所创建的Bean
- 自动装配:Spring自动满足Bean之间的依赖
通过Component注解标记类
package com.wj.demo1;
import org.springframework.stereotype.Component;
@Component
public class FootballGamesImpl {
public void play() {
System.out.println("我爱踢足球");
}
}
配置启动组件扫描
配置启动组件扫描有两种方式,一种是Java代码配置启动组件扫描,一种是xml配置启动组件扫描
第一种方式:Java代码配置启动组件扫描
package com.wj.demo1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Java代码配置组件扫描
*ComponentScan 自定扫描组件的范围,不设置默认当前类的包以及子包,设置会扫描指d定类
*Configuration 指定为某个类配置
*/
@ComponentScan
@Configuration
public class Config {
}
第二种方式: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">
<!--Spring xml配置启动组件扫描-->
<context:component-scan base-package="com.wj.demo1"/>
</beans>
两种方式几乎功能差不多,一般情况下,推荐使用Java代码配置启动组件扫描
测试
package com.wj.demo1;
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 = Config.class)
public class TestDemo {
@Autowired//自动装配,查找容器中满足条件的Bean,注入
private FootballGamesImpl footballGames;
@Test
public void testDemo1(){
footballGames.play();
}
}
2、在Java中进行显式配置装配Bean
创建Football这个bean类
package com.wj.demo2;
public class Football {
public void play() {
System.out.println("我爱踢足球");
}
}
Java显示配置
package com.wj.demo2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public Football football(){
return new Football();
}
}
测试
package com.wj.demo2;
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 = Config.class)
public class TestDemo {
@Autowired
private Football football;
@Test
public void testDemo1(){
football.play();
}
}
3、通过XML进行显示配置装配Bean
实例
package com.wj.demo3;
public class Football {
public void play() {
System.out.println("我爱踢足球");
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="football" class="com.wj.demo3.Football"></bean>
</beans>
搭配Java配置
package com.wj.demo3;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("com/wj/demo3/xml-spring.xml")
public class Config {
}
测试
package com.wj.demo3;
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 = Config.class)
public class TestDemo {
@Autowired
private Football football;
@Test
public void testDemo1(){
football.play();
}
}