7.Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配的方式
1.在xml中显示的配置
2.在java中显示配置
3.隐式的自动装配bean
7.1测试
环境搭建:一个人有两个宠物!
7.2 ByName自动装配
<!--
byName:会自动在容器上下文中查找和自己set方法后面的值对象的beanid
-->
<bean id="person" class="com.hedachun.pojo.Person" autowire="byName">
<property name="name" value="何大春"/>
</bean>
7.3 ByType自动装配
<bean id="cata" class="com.hedachun.pojo.Cat"/>
<bean id="dog" class="com.hedachun.pojo.Dog"/>
<!--
byName:会自动在容器上下文中查找和自己set方法后面的值对象的beanid!
byType:会自动在容器上下文中查找和自己对象属性类型相同的bean!
-->
<bean id="person" class="com.hedachun.pojo.Person" autowire="byType">
<property name="name" value="何大春"/>
</bean>
小结:
byName:需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
byType:需要保证bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
7.4使用注解自动装配
使用注解须知:
1.导入约束:contex约束
2.配置注解的支持:contex:annotation-config/
<?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:contex="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解支持-->
<contex:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可,也可以在set方式上使用!
使用**@Autowired**我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC容器中存在且符合byName的名字!
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个@Autowired注解完成的时候,我们可以使用
@Qualifier(value = "xxx")
去配合@Autowired的使用,指定一个唯一的bean对象id注入!
public class Person {
@Autowired
@Qualifier(value = "cat")
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
@Resource注解
public class Person {
@Resource(name = "cat2")
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
总结:@Autowired和@Resource的区别:
- 都是用来自动装配的,都可以放在属性字段上!
- @Autowired通过byType的方式实现,而且要求对象存在! 常用方式
- @Resource默认通过byName的方式实现,如果找不到名字,则通过byTyoe方式实现!如果两个都找不到就会报错
- 执行顺序不同:
@Resource默认通过byName的方式,@Autowired通过byType的方式实现。
8.使用注解开发
在Spring4之后,要使用注解开发,必须保证aop的包的导入了
使用注解需要导入contex约束,增加注解的支持!
<?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:contex="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解支持-->
<contex:annotation-config/>
</beans>
1.bean
2.属性如何注入
//等价于 <bean id="user" class="com.hedachun.pojo.User"/>
//@Component 组件
@Component
public class User {
//相当于 <property name="name" value="何小春"/>
@Value("何小春")
public String name;
public String getName() {
return name;
}
@Value("何小春")
public void setName(String name) {
this.name = name;
}
}
3.衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!!
- dao「@Repository」
- sercive「@Service」
- controller「@Controller」
这四个注解功能都是一样的,都是代表某个类注册到Spring中,装配Bean
4.自动装配
@Autowired
@Resource注解
5.作用域
@Scope(“singleton”)
@Scope(“prototype”)
@Component
@Scope("singleton")
public class User {
//相当于 <property name="name" value="何小春"/>
@Value("何小春")
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6.小结
- xml与注解:
- xml更加方便,适用于任何场合!维护更简单
- 注解不是自己的类使用不了,维护相对复杂!
- xml与注解最佳实战:
- xml用来管理bean
- 注解只负责完成属性的注入
- 在使用过程中,只需要注意一个问题:要让注解生效,就需要开启注解的支持
<!--指定要扫描的包,这个包下的注解就会生效-->
<contex:component-scan base-package="com.hedachun"/>
<contex:annotation-config/>
9.使用Java的方式配置Spring
实体类:
//这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中
@Component
public class User {
private String name;
public String getName() {
return name;
}
//属性注入值
@Value("何大春")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
配置文件
//这个也会被Spring容器托管,注册到容器中,因为他本来就是一个@Component,
//@Configuration代表这十一个配置类,就相当于之前看的beans.xml
@Configuration
@ComponentScan("com.hedachun.pojo")
public class MyConfig {
//注册一个bean,就相当于我我们之前写的一个bean标签
//这个方法的名字就相当于bean标签的id
//这个方法的返回值,就相当于bean标签中的class属性
@Bean
public User getUser(){
return new User(); //就是返回要注入到bean的对象!
}
}
测试类
public class MyTest {
public static void main(String[] args) {
//如果完成使用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = context.getBean("getUser",User.class);
System.out.println(getUser.getName());
}
}
项目结构: