spring之使用注解自动装配bean属性

版权声明:文章编写不易,转载请注明原链接 https://blog.csdn.net/u012184539/article/details/84874771

一. 使用注解自动装配bean属性

在之前的内容中说到spring使用xml文件装配bean的属性,可以用autowire属性配置4个不同的值让spring实现自动装配。同时spring也可以使用Java代码中的注解来自动装配属性,这种方式默认是禁止的,需要在spring配置中启用它。

1.1 启用注解装配

在spring的context命名空间配置中的context: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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd"
       default-init-method="init" default-destroy-method="destroy">
    <context:annotation-config/>
</beans>

1.2 使用@AutoWired注解

在启用注解装配后,在Java类中可以对属性、方法和构造器进行注解装配。如下user类有一个属性为apple。

public class User{
	@AutoWired
	private Apple apple;
	//...
}
<bean id="user" class="xyz.totok.User"/>
<bean id="apple" class="xyz.totok.Apple"/>

spring会自动装配User的apple属性。

1.3 使用required设置自动装配为可选的

当使用@AutoWired注解后,如果没有找到apple这个bean,将无法装配成功,会报错。可以使用required属性设置bean的属性为可选的,当找不到匹配的bean时会设置为null值。

public class User{
	@AutoWired(required=false)
	private Apple apple;
	//...
}

1.4 多个bean符合装配条件@Qualifier

当自动装配时多个bean都符合条件时,会报错。可以使用@Qualifier注解添加更多限制条件。如下

<context:annotation-config/>
    <bean id="user" class="xyz.totok.User"/>
    <bean id="apple1" class="xyz.totok.Apple"/>
    <bean id="apple2" class="xyz.totok.Apple"/>

此时apple属性有两个bean都符合条件,会报错。解决方式如下

public class User{
	@AutoWired(required=false)
	@Qualifier("apple1")
	private Apple apple;
	//...
}

这时会把apple1装配成功。

1.5 自定义限定器Qualifier

自定义限定器Qualifier注解,使用@Qualifier作为自定义注解的元注解。

@Target(ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface RedAppleQualifier{}

如下这种情况可以使用自定义的@RedAppleQualifier来限定。

@RedAppleQualifier
public class RedApplel extends Apple{
}
@GreenAppleQualifier
public class GreenApplel extends Apple{
}
<context:annotation-config/>
    <bean id="user" class="xyz.totok.User"/>
    <bean id="apple1" class="xyz.totok.RedApplel"/>
    <bean id="apple2" class="xyz.totok.GreenApplel "/>
public class User{
	@AutoWired(required=false)
	@RedAppleQualifier
	private Apple apple;
	//...
}

这个时候会把红色的苹果装配成功,如果红色苹果还有多个,比如有2个红色的甜苹果,还可以添加@SweetAppleQualifier注解来进一步限定范围。

二. 使用@Inject实现基于标准的自动装配

Spring支持使用Java标准的依赖注入规范来自动装配bean。在大多数时候,@Inject几乎和@AutoWired一样的效果。

注解 @AutoWired @Inject
功能 装配属性、方法和构造器 装配属性、方法和构造器
required属性
限定器 @Qualifier @Named
自定义注解 元注解:org.springframework.beans.factory.annotation.Qualifier; 元注解:javax.inject.Qualifier

三. 在注解注入时使用表达式来装配

之前讲到在beans配置文件xml中可以使用#{}界定符来使用表达来装配bean。在Java代码的注解注入中一样可以使用这样的表达式来注入。

3.1 @Value装配基本数据类型和String类型

@Value("20")
private int age;
@Value("tony")
private String name;

3.2 @Value使用表达式注入

如下比弟弟的年龄大一岁

@Value("#{didi.age+1}")
private int age;

猜你喜欢

转载自blog.csdn.net/u012184539/article/details/84874771