To use annotations for dependency injection in spring, you need to configure the following:
<Beansxmlns= "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-3 .0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-cont Ext-3.0.xsd "> <Context:annotation-config/></Beans>
Spring Self-explanatory annotations with dependency injection
@Required, the annotation must be used as a setter method, the purpose is to force the request to provide the setter data required, or error.
For example, fields in Beana field, there is a SetField (T field) method. When @required is used on the method, the data required to set the field must be given when creating Beana in XML.
As shown below:
PackageO1.bean;Importorg.springframework.beans.factory.annotation.Required; Public classBeana {PrivateString message; PublicString getMessage () {returnmessage; } @Required //can only be placed on the setter, you must specify setter injection when the XML configuration Beana, otherwise the spring container will throw an exception when it starts Public voidsetmessage (String message) { This. Message =message; }}
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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-3.0.xsd "> <!--Turn on annotation support - <Context:annotation-config/> <Beanclass= "O1.bean.BeanA"> <!-- because of the @required, it must be provided here, otherwise error - - < Propertyname= "message"ref= "message"/> </Bean> <Beanname= "message"class= "Java.lang.String"> <Constructor-argIndex= "0"value= "Hello World"/> </Bean></Beans>
PackageO1;ImportO1.bean.BeanA;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classA {PrivateApplicationContext ApplicationContext; @Before Public voidsetUp () {ApplicationContext=NewClasspathxmlapplicationcontext ("Classpath:applicationContext.xml"); } @Test Public voidrun1 () {Beana bean= Applicationcontext.getbean (Beana.class); System.out.println (Bean.getmessage ()); }}
@Autowired (Required=true)
Automatic injection, the Required=true function is the same as the @required.
Available for constructors, fields, methods.
The default is automatically assembled according to the parameter type, but only one candidate must be available (Required=false can allow 0 candidates).
@Value (value= "Spel")
Available for fields, methods (@Autowired method).
Such as:
@Value (value= "#{message}")private String message;
@Autowired Public void initmessage (@Value (Value = "#{message}#{message}") String message { this. message = message;}
@Qualifier (value= "qualified identifier")
Available for methods, fields, parameters.
Used with @autowired, which can be used for multiple candidates.
<<< to Be Continued >>>
Annotations for Spring Dependency Injection (DI)