spring框架——bean的自动装配不需要手动指定 property 的 value 值

1 前言

bean 的自动装配是指:不需要手动指定 property 的 value 值,spring 自动将匹配的属性注入 bean。主要有如下2种方式:

bean 标签中 autowire 属性:bean 内所有未手动注入的属性将自动匹配并注入,有 byType 和 byName 2种匹配方式
@Autowire 注解:当 @Autowire 加在属性上时,该属性将自动匹配并注入;当 @Autowire 加在方法上时,该方法将自动调用,其入口参数为 IOC 容器中匹配的 bean
注意事项:

只有非字面量(非基本数据类型及其封装类)的属性才能自动装配,并且 byType 类型自动装配支持兼容性,即允许将子类对象赋值给 bean 的某属性(此属性为父类类型)
@Autowire 在匹配时,先采用 byType 方式,再采用 byName 方式
当 @Autowire 没有匹配到 bean 时,可以采用 @Autowire(required=false) 允许程序不匹配成功,避免报错
当 @Autowire 匹配到多个 bean 时,可以采用 @Qualifier(value="beanId") 指定需要注入的 bean
所有注解在容器初始化的时候自动匹配并注入
2 bean 标签中 autowire 属性

2.1 byType

按照属性类型进行匹配,需要注意:配置文件(xml文件)中匹配的 bean 要求具有唯一性,即此类型的 bean 仅有一个。

Car.java

package com.test;

public class Car {
private String cbrand;
private Engine engine;

public String getCbrand() {
return cbrand;
}

public void setCbrand(String cbrand) {
this.cbrand = cbrand;
}

public Engine getEngine() {
return engine;
}

public void setEngine(Engine engine) {
this.engine = engine;
}

@Override
public String toString() {
return "Car [cbrand=" + cbrand + ", engine=" + engine + "]";
}
}
Engine.java

package com.test;

public class Engine {
private String ebrand;

public String getEbrand() {
return ebrand;
}

public void setEbrand(String ebrand) {
this.ebrand = ebrand;
}

@Override
public String toString() {
return "Engine [ebrand=" + ebrand + "]";
}
}
applicationContext.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="car" class="com.test.Car" autowire="byType">
<property name="cbrand" value="奥迪"></property>
</bean>

<bean id="eng" class="com.test.Engine">
<property name="ebrand" value="雅阁"></property>
</bean>
</beans>
Engine 类型的 bean 具有唯一性。 

 Test.java

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Car car=ac.getBean("car",Car.class);
System.out.println(car);
}
}
运行结果:

Car [cbrand=奥迪, engine=Engine [ebrand=雅阁]]
2.2 byName

按照属性名称进行匹配,需要注意:匹配的 bean 的 id 与属性名要一致。

applicationContext.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="car" class="com.test.Car" autowire="byName">
<property name="cbrand" value="奥迪"></property>
</bean>

<bean id="engine" class="com.test.Engine">
<property name="ebrand" value="雅阁"></property>
</bean>
</beans>
3 @Autowire 注解

@Autowire 可以加在属性上,也可以加在方法上,并在容器初始化时自动匹配并注入。

首先需要导入如下  jar 包,并且 xml 文件需要导入 context 命名空间。

spring-aop-4.0.0.RELEASE.jar
本节主要介绍 Car.java 和 applicationContext.xml,对于 Engine.java 和 Test.java 同上。 

3.1 @Autowire 加在属性上

3.1.1 基本应用

Car.java

package com.test;

import org.springframework.beans.factory.annotation.Autowired;

public class Car {
private String cbrand;

@Autowired
private Engine engine;

public String getCbrand() {
return cbrand;
}

public void setCbrand(String cbrand) {
this.cbrand = cbrand;
}

@Override
public String toString() {
return "Car [cbrand=" + cbrand + ", engine=" + engine + "]";
}
}
注意:engine 属性添加了 @Autowired 注解,并且其没有 getter 方法和 setter 方法。 

applicationContext.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-4.0.xsd">
<context:component-scan base-package="com.test"></context:component-scan>

<bean id="car" class="com.test.Car">
<property name="cbrand" value="奥迪"></property>
</bean>

<bean id="engine" class="com.test.Engine">
<property name="ebrand" value="雅阁"></property>
</bean>
</beans>
注意:base-package属性指定了需要扫描包,扫描包中所有类,并为带 @Autowired 注解的属性和方法自动装配。

运行结果:

Car [cbrand=奥迪, engine=Engine [ebrand=雅阁]]
3.1.2 @Autowire(required=false)

当没有找到匹配的 bean 时,会报错,如下:

NoSuchBeanDefinitionException: No qualifying bean of type [com.test.Engine] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
此时,可以在属性前设置 @Autowire(required=false),允许属性为空,运行结果如下:

Car [cbrand=奥迪, engine=null]
3.1.3 @Qualifier(value="beanId")

当匹配的到多个 bean 时,会报错,如下:

NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Engine] is defined: expected single matching bean but found 2: engine1,engine2
此时,可以采用 @Qualifier(value="beanId") 注解指定需要注入的 bean。

Car.java

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Car {
private String cbrand;

@Autowired
@Qualifier(value="engine2")
private Engine engine;

public String getCbrand() {
return cbrand;
}

public void setCbrand(String cbrand) {
this.cbrand = cbrand;
}

@Override
public String toString() {
return "Car [cbrand=" + cbrand + ", engine=" + engine + "]";
}
}
applicationContext.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-4.0.xsd">
<context:component-scan base-package="com.test"></context:component-scan>

<bean id="car" class="com.test.Car">
<property name="cbrand" value="奥迪"></property>
</bean>

<bean id="engine1" class="com.test.Engine">
<property name="ebrand" value="雅阁"></property>
</bean>

<bean id="engine2" class="com.test.Engine">
<property name="ebrand" value="迈腾"></property>
</bean>
</beans>
运行结果:

Car [cbrand=奥迪, engine=Engine [ebrand=迈腾]]
3.2 @Autowire 加在方法上

3.2.1 基本应用

本节仅介绍 Car.java,其他类和配置文件同3.1.1节。

Car.java

package com.test;

import org.springframework.beans.factory.annotation.Autowired;

public class Car {
private String cbrand;
private Engine engine;

@Autowired
public void addEngine(Engine engine) {
System.out.println("调用了 addEngine 方法");
this.engine=engine;
}

public String getCbrand() {
return cbrand;
}

public void setCbrand(String cbrand) {
this.cbrand = cbrand;
}

@Override
public String toString() {
return "Car [cbrand=" + cbrand + ", engine=" + engine + "]";
}
}
注意:在 addEngine(Engine engine) 方法上加了 @Autowired 注解,方法名可以随意定,在 IOC 容器初始化时将自动调用此方法,并在容器中匹配 Engine 类型(或 id 为 engine)的 bean 传给该方法,作为入口参数。

3.2.2 @Autowire(required=false)

当没有找到匹配的 bean 时,可以在属性前设置 @Autowire(required=false),允许属性为空,运行结果如下:

Car [cbrand=奥迪, engine=null]
3.2.3 @Qualifier(value="beanId")

本节仅介绍 Car.java,其他类和配置文件同3.1.3节。

当匹配的到多个 bean 时,可以采用 @Qualifier(value="beanId") 注解指定需要注入的 bean。

Car.java

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Car {
private String cbrand;
private Engine engine;

@Autowired()
@Qualifier(value="engine2")
public void addEngine(Engine engine) {
System.out.println("调用了 addEngine 方法");
this.engine=engine;
}

public String getCbrand() {
return cbrand;
}

public void setCbrand(String cbrand) {
this.cbrand = cbrand;
}

@Override
public String toString() {
return "Car [cbrand=" + cbrand + ", engine=" + engine + "]";
}
}
运行结果:

调用了 addEngine 方法
Car [cbrand=奥迪, engine=Engine [ebrand=迈腾]]
 
————————————————
原文链接:https://blog.csdn.net/m0_37602827/article/details/106203010

http://blog.linkshop.com.cn/u/s_6586110358/
http://blog.linkshop.com.cn/u/s_6585719610/
http://blog.linkshop.com.cn/u/s_6585719750/
http://blog.linkshop.com.cn/u/s_6585719750/
http://blog.linkshop.com.cn/u/s_6586110878/
http://blog.linkshop.com.cn/u/s_6585720010/
http://blog.linkshop.com.cn/u/s_6585720112/
http://blog.linkshop.com.cn/u/s_6586111193/
http://blog.linkshop.com.cn/u/s_6586111400/
http://blog.linkshop.com.cn/u/15960606298/
http://www.sina.com.cn/mid/search.shtml?q=%E6%9D%8F%E5%BD%A9%E5%B9%B3%E5%8F%B0%E5%BC%80%E6%88%B7%E6%89%A31530667
http://www.sina.com.cn/mid/search.shtml?q=%E6%9D%8F%E9%91%AB%E5%B9%B3%E5%8F%B0%E5%BC%80%E6%88%B7%E6%89%A31530667
http://www.sina.com.cn/mid/search.shtml?q=%E6%9D%8F%E8%80%80%E5%B9%B3%E5%8F%B0%E5%BC%80%E6%88%B7%E6%89%A31530667
http://www.sina.com.cn/mid/search.shtml?q=%E6%9D%8F%E5%BD%A9%E6%B3%A8%E5%86%8C%E5%BC%80%E6%88%B7%E6%89%A31530667
http://www.sina.com.cn/mid/search.shtml?q=%E6%B2%90%E9%B8%A3%E5%A8%B1%E4%B9%90%E5%BC%80%E6%88%B7%E6%89%A31530667

猜你喜欢

转载自www.cnblogs.com/dasdfdfecvcx/p/12927113.html