Annotations for Spring Dependency Injection (DI)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.