1.自动装配
自动装配是使用spring满足bean依赖的一种方法
spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:
1.在xml中显式配置;
2.在java中显式配置;
3.隐式的bean发现机制和自动装配。
Spring的自动装配需要从两个角度来实现:
组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;
组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。
使用元素的autowire属性为一个bean定义中指定自动装配模式。
byName:去容器的上下文中去寻找,是否有和属性名相同的 bean id
byType:去容器的上下文中去寻找,是否有和自己对象属性类型相同的 bean class
搭建环境
1.创建实体类
package com.chen.pojo;
public class Person {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
package com.chen.pojo;
public class Dog {
public void getShout(){
System.out.println("汪汪汪。。。。。");
}
}
package com.chen.pojo;
public class Cat {
public void getShout(){
System.out.println("喵喵喵。。。。。");
}
}
2.配置beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog11" class="com.chen.pojo.Dog"/>
<bean id="cat11" class="com.chen.pojo.Cat"/>
<!--
byName:去容器的上下文中去寻找,是否有和属性名相同的 bean id
byType:去容器的上下文中去寻找,是否有和自己对象属性类型相同的 bean class
-->
<bean id="person" class="com.chen.pojo.Person" autowire="byName">
<property name="name" value="dada"/>
</bean>
</beans>
3.测试
package com.chen;
import com.chen.pojo.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person = context.getBean("person", Person.class);
person.getCat().getShout();
person.getDog().getShout();
}
}
2.注解开发
使用注解开发时,在配置文件当中,需要引入一个context约束
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解所依赖的约束 -->
<context:annotation-config/>
</beans>
对比使用bean标签的注入的方式,注解的方式更加流行
2.1.bean的实现
1、配置扫描哪些包下的注解
<!--指定注解扫描包-->
<context:component-scan base-package="com.chen.pojo"/>
2.在指定包下编写类,增加注解
package com.chen.pojo;
@Component("user")
//@Component("user") 等价于 <bean id="user" class="com.chen.pojo.User"/>
public class User {
public String name;
}
2.2 属性的注入
使用注解注入属性
1、可以不用提供set方法,直接在直接名上添加@value(“值”)
@Component("user")
//@Component("user") 等价于 <bean id="user" class="com.chen.pojo.User"/>
public class User {
@Value("chenshao")
// @Value("chenshao") 等价于<property name="name" value="chenshao"/>
public String name;
}
2.如果提供了set方法,在set方法上添加@value(“值”);
@Component("user")
//@Component("user") 等价于 <bean id="user" class="com.chen.pojo.User"/>
public class User {
public String name;
@Value("chenshao")
public void setName(String name) {
this.name = name;
}
}
2.3 衍生注解
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
@Controller:web层
@Service:service层
@Repository:dao层
2.4 自动装配注解
略
2.5 作用域
@scope
singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Component("user")
@Scope("prototype")
//@Component("user") 等价于 <bean id="user" class="com.chen.pojo.User"/>
public class User {
@Value("chenshao")
public String name;
3.小结
XML与注解比较
XML可以适用任何场景 ,结构清晰,维护方便
注解不是自己提供的类使用不了,开发简单方便
xml与注解整合开发 :推荐最佳实践
xml管理Bean
注解完成属性注入
使用过程中, 可以不用扫描,扫描是为了类上的注解
<context:annotation-config/>
作用:
进行注解驱动注册,从而使注解生效
用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册
如果不扫描包,就需要手动配置bean
如果不加注解驱动,则注入的值为null!