Java spring-annotations for attribute injection

Source: Internet
Author: User
Tags mixed

2017-11-06 21:19:43

One, spring annotation assembly bean
Spring2.5 introducing the use of annotations to define beans

    • @Component describing beans in the spring framework

Three annotations equivalent to @component annotations are available in the spring framework

    • @Repository for labeling DAO Implementation classes (DAO layer)
    • @Service for labeling service implementation classes (service layer)
    • @Controller used to label a Controller implementation class (Web layer)
Because there is only one property value, it can be written directly. Generally need value= "..." @Component ("User") public class User {public    void SayHello () {        System.out.println ("Hello World .");    }}

Configuration file:

<?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: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.xsd "> <!--bean definitions here--    <context:component-scan Base-package= "Spring5"/></beans>

Second, annotation for attribute injection

Normal Properties: @Value (value= "..."), this time can not write setter method

Object Properties: @Resource (name = "....")

Because there is only one property value, it can be written directly. Generally need value= "..." @Component ("User") public class User {    @Value (value= "Spring")    private String s;    public void SayHello () {        System.out.println ("Hello World");    @Override public    String toString () {        return "user{" +                "s= '" + S + ' \ ' +                '} ';}    }

Iii. mixed use of XML and annotations

Two ways to combine: generally using XML to register beans, using annotations for attribute injection

Let's start with some additional annotation configurations:

(1) Configure the Bean initialization method and destroy method:
* Init-method and Destroy-method.

    • @PostConstruct Initialization
    • @PreDestroy Destruction
@PostConstructpublic void Setup () {System.out.println ("Initialize ...");} @PreDestroypublic void Teardown () {System.out.println ("destroy ...");

(2) Scope of configuration Bean:@Scope

@Component ("User") @Scope (value= "prototype") public class User {    @Value (value= "Spring")    private String s;    public void SayHello () {        System.out.println ("Hello World");    @Override public    String toString () {        return "user{" +                "s= '" + S + ' \ ' +                '} ';}    }

(3) Use Java class to configure information, scan in XML can

@Configurationpublic class Beanconfig {    @Bean (name = "Car") public    Car Showcar () {        car car = new car ();        Car.setname ("Changan");        Car.setprice (40000d);        return car;    }    @Bean (name = "Product") Public    product initproduct () {        Product Product = new product ();        Product.setname ("Air Conditioning");        Product.setprice (3000d);        return product;}    }

Examples of mixed use:

public class Person {    @Autowired    @Qualifier ("car")    private car car;    @Autowired    @Qualifier (value = "plane")    private plane plane;    @Override public    String toString () {        return "person{" +                "car=" + car +                ", plane=" + Plane +                '} '; c26/>}}

Configuration file:

<?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: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.xsd "> <!--Bean Definitions---    <!--make attribute injection annotations Effective--    < context:annotation-config/>    <bean id= "car" class= "Spring6. Car "/>    <bean id=" plane "class=" Spring6. Plane "/>    <bean id=" person "class=" spring6. Person ">    </bean></beans>

Iv. Integration of JUnit testing

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = "Classpath:spring-config.xml") public Class Spring6 {    @Autowired    @Qualifier (' person ')    private person p;    @Test Public    Void Demo () {        System.out.println (p);    }}

Configuration file:

<?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: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.xsd "> <!--Bean Definitions---    <!--make attribute injection annotations Effective--    < context:annotation-config/>    <bean id= "car" class= "Spring6. Car "/>    <bean id=" plane "class=" Spring6. Plane "/>    <bean id=" person "class=" spring6. Person ">    </bean></beans>

Java spring-annotations for attribute injection

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.