Bean的自动装配
前提
beans.xml存在要引用的bean
概况
简化以下命令(换一种方式表现引用bean)变成自动引用
<bean id="user" class="com.minjian.projo.User">
<property name = "cat" ref="com.minjian.projo.Cat"></property>
</bean>
按类型装配
@Autowired
按Name装配
@Autowired
@Qualifier(value = “对应的beanId”)
备注:这里的beanId是在beans.xml中的class=“对应类”的id
混合装配
@Resource(name = “对应的beanId”)
备注:这里的beanId是在beans.xml中的class=“对应类”的id
说明
- @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
- 其次再进行默认的byName方式进行装配;
- 如果以上都不成功,则按byType的方式自动装配。
- 都不成功,则报异常
xml上的自动装配(主要上能用到到)
<!----byName根据类中的属性的name与id自动装配------>
<bean id="user" class="com.minjiang.projo.User" autowire="byName">
<property name="str" value="小黑"></property>
</bean>
<!----byName根据类中的属性的类型与class自动装配------>
<bean id="user" class="com.minjiang.projo.User" autowire="byType">
<property name="str" value="小黑"></property>
</bean>
注解的自动装配
public class User {
//按类型装配
@Autowired
@Qualifier("cat")
private Cat cat;
//按名字装配
@Autowired
private Dog dog;
。。。。get方法
}
public class User {
@Resource(name = "cat")
private Cat cat;
@Resource(name = "dog")
private Dog dog;
。。。。get方法
}
总结
@Autowired与@Resource最大的不同:
执行顺序不同。@Autowired先byType,@Resource先byName。
注解开发
目的
简化开发,不在beans.xml编写bean(ps:超级麻烦),直接使用注解完成
条件
需要依赖
<!-- Spring需要导入commons-logging进行日志记录-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- 在spring4之后,想要使用注解形式,必须得要引入aop的包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
配置文件需要添加约束
<?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/>
<!-- 注解要扫描的包-->
<context:component-scan base-package="com.minjiang.projo"></context:component-scan>
</beans>
代码实现
// 相当于配置文件中 <bean id="cat" class="当前注解的类"/>
@Component("cat")
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
// 相当于配置文件中 <bean id="dog" class="当前注解的类"/>
@Component("dog")
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component("user")
public class User {
@Autowired
@Qualifier("cat")
private Cat cat;
@Autowired
@Qualifier("dog")
private Dog dog;
@Value("小黑")
// 相当于配置文件中 <property name="name" value="小黑"/>
private String str;
public User() {
System.out.println("你不会喜欢我");
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getStr() {
return str;
}
@Override
public String toString() {
return "User{" +
"cat=" + cat +
", dog=" + dog +
", str='" + str + '\'' +
'}';
}
}
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其他三个注解,功能一样
- @Controller:web层
- @Service:service层
- @Repository:dao层
补充:作用域
-
singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
-
prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Controller("user")
@Scope("prototype")
public class User {
public String name;
}
建议
注解和xml混合使用(xml创建bean,注解注入值),xml适用于任何场景,注解开发简单方便