这是我参与2022首次更文挑战的第18天,活动详情查看2022首次更文挑战
深入Spring源码专题
要为构造函数注入使用注解,还需要在目标bean的构造函数方法中使用@autowired注解,这是setter注入方法的替代方法,代码如下所示:
@Service("provider")
public class ConfigurableMessageProvider implements MessageProvider{
private String message;
public ConfigurableMessageProvider(@Value("Configurable message") String message){
this.message=message;
}
@Override
public String getMessage(){
return this.message;
}
}
使用另一个注解@Value来定义要注入构造函数的值,这是Spring中将值注入bean的方式。
在使用两个具有相同参数数量的构造函数并且参数所使用的类型以相同的方式表示时,Spring无法确定使用哪个构造函数注入。
public class ConstructorConfusion{
private String value;
public ConstructorConfusion(String value){
System.out.println("ConstructorConfusion (String) called");
this.value=value;
}
public ConstructorConfusion(int value){
System.out.println("ConstructorConfusion (int) called");
this.value="Number"+String.valueOf(value);
}
public static void main(String... args){
GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();
ctx.load("classpath:spring/app-context-xml.xml");
ctx.reload();
constructorConfusion cc=(ConstructorConfusion)ctx.getBean("constructorConfusion");
System.out.println(cc);
ctx.close();
}
}
从ApplicationContext中检索一个ConstructorConfusion类型的bean,配置文件如下:
<beans ...>
<bean id="povider" class="com.ozx.ConfiguarbleMessageProvider" c:message="message content"></bean>
<bean id="constructorConfusion" class="com.ozx.ConstructorConfusion">
<constructor-arg>
<value>90</value>
</constructor-arg>
</bean>
</beans>
在上面情况下调用第一个String类型的构造函数,控制台输出:
ConstructorConfusion (String) called
输出表明带有String参数的构造函数被调用,并不是想要的结果,因为想要通过使用构造函数注入为传入的每个任何整数值添加前缀Number,则使用int参数的构造函数。需要做以下改动:
<beans ...>
<bean id="povider" class="com.ozx.ConfiguarbleMessageProvider" c:message="message content"></bean>
<bean id="constructorConfusion" class="com.ozx.ConstructorConfusion">
<constructor-arg type="int">
<value>90</value>
</constructor-arg>
</bean>
</beans>
注意:
标记有一个额外的属性type,它指定了Spring查找的参数的类型,再次使用修改后的配置运行生成如下结果:
ConstructorConfusion (int) called
Number: 90