Java – how to use annotations to perform constructor based dependency injection on spring?
•
Java
OK, so if I need to put some raw values in the constructor, what should I do?
@Autowired public CustomBean(String name,@Qualifier("SuperBean") SuperBean superBean) { super(); this.superBean = superBean; this.name = name; }
For example, I define the superbean finite qualifier "superbean" here, but I also want to know how to set the name value here using annotations?
I know it's possible to use XML configuration, but I want to know how to do this with annotations:
<bean id="CustomXmlBean" class="org.arturas.summerfav.beans.CustomXmlBean"> <constructor-arg name="name" type="String" value="The Big Custom XML Bean" /> <constructor-arg> <bean id="SuperBean" class="org.arturas.summerfav.beans.SuperBean" /> </constructor-arg> </bean>
So how do I enter values for string, int, and other generic types?
Solution
This is a method:
@Component public class YourBean { @Autowired public YourBean(@Value("${prop1}") String arg1,@Value("${prop2}") String arg2) { // rest of the code } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码