Spring-dependent injection--java projects are injected using spring annotations

Source: Internet
Author: User

annotation injection, as the name implies, is injected through annotations, and the common annotations associated with spring and injection are autowired, Resource, Qualifier, Service, Controller, Repository, Component.
    • Autowired is auto-injected and automatically finds the right bean from the spring context to inject
    • Resource used to specify name injection
    • Qualifier and Autowired with the name of the bean specified
    • Service,controller,repository are labeled as service layer classes, Controller layer classes, data storage layer classes, and Spring scan annotations are configured to mark these classes to generate beans.
    • component is a generic, tag class is a component that, when spring scans annotation configurations, flags these classes to generate beans.

The above autowired and resource are used to modify the field, constructor, or set method, and do inject. Instead, Service,controller,repository,component is used to modify classes to mark these classes to generate beans.

MAVEN's Pom.xml

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < Org.springframework.version>3.1.1.release</org.springframework.version> </properties> < dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</artifact id> <version>4.8.1</version> <scope>test</scope> </dependency> <de Pendency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</artifactid > <version>${org.springframework.version}</version></dependency><dependency> < Groupid>org.springframework</groupid> <artifactId>spring-orm</artifactId> <version>${ Org.springframework.version}</version></dependency><dependency> <groupId>    Org.springframework</groupid> <artifactId>spring-test</artifactId><version>${org.springframework.version}</version> <type>jar</type> <scope>test< /scope></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId> Aspectjweaver</artifactid> <version>1.8.2</version></dependency><dependency>< groupid>com.fasterxml.jackson.core</groupid><artifactid>jackson-core</artifactid>< Version>2.2.3</version></dependency><dependency><groupid>com.fasterxml.jackson.core </groupid><artifactid>jackson-databind</artifactid><version>2.2.3</version></ Dependency><dependency><groupid>com.fasterxml.jackson.core</groupid><artifactid> Jackson-annotations</artifactid><version>2.2.3</version></dependency> </dependencies >

1, establish the DAO layer, add @repository annotations to it (note: DAO class does not need to implement an interface, of course, the interface can also be implemented)

Package Cn.outofmemory.helloannotation;import org.springframework.stereotype.Repository; @Repositorypublic class Cardao {public    void Insertcar (string car) {        string insertmsg = String.Format ("Inserting car%s", car);        System.out.println (insertmsg);    }}

2, service layer, @Service annotations, define Cardao fields in this class, and decorate this field with autowired so that an instance of the Cardao class defined above is automatically injected into the Carservice instance.(Note: The service class does not need to implement an interface, of course, the implementation of the interface can also)

Package Cn.outofmemory.helloannotation;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.Service; @Servicepublic class Carservice {    @Autowired    private Cardao Cardao;    public void Addcar (String car) {        this.carDao.insertCar (car);    }}

3. Below we use the above test note injection in App.java:

Package Cn.outofmemory.helloannotation;import Org.springframework.context.applicationcontext;import Org.springframework.context.annotation.annotationconfigapplicationcontext;public class App {public    static void Main (string[] args) {        ApplicationContext appContext = new Annotationconfigapplicationcontext (" Cn.outofmemory.helloannotation ");        Carservice service = Appcontext.getbean (carservice.class);        Service.addcar ("BMW");}    }

In the main method above we first initialized the Appcontext, which is Annotationconfigapplicationcontext, whose constructor accepts the name of a package to qualify the package to be scanned. You can then get an example of Carservice using the Appcontext Getbean method.


The above example is very simple, the simple use of annotationconfigapplicationcontext, but in the actual project is often not so simple, still need the spring configuration file. The following configuration allows spring to automatically scan the annotation configuration in the spring configuration file.

4, ApplicationContext. XML configuration file (put the file under Classpath)

<?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-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd ">    <!--beans annotation driven-    <context:annotation-config/>    <context:component-scan base-package= "Cn.outofmemory.helloannotation" >    </context:component-scan > </beans>


5. Test class:

Package Cn.outofmemory.helloannotation;import Org.springframework.context.applicationcontext;import Org.springframework.context.annotation.annotationconfigapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class App {public    static void main ( String[] args) {        //applicationcontext appContext = new Annotationconfigapplicationcontext (" Cn.outofmemory.helloannotation "); <span style=" White-space:pre ">string[] fileUrl = new string[]{" classpath*:   Applicationcontext.xml "}; </span>        ApplicationContext appContext = new Classpathxmlapplicationcontext (FILEURL) <span style= " font-family: ' Comic Sans MS '; " >;</span>        Carservice service = Appcontext.getbean (carservice.class);        Service.addcar ("BMW");}    }

Note: Spring fetch beans can be obtained by type, by name, by type, or by the following methods

Carservice service = (carservice) appcontext.getbean ("Carservice");
The specification of the name for the hump


Reference: Http://outofmemory.cn/code-snippet/3670/spring-inject-by-annotation



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring-dependent injection--java projects are injected using spring annotations

Related Article

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.