Spring Annotations Implement Bean definitions

Source: Internet
Author: User
12.3 Annotation Implementation Bean definition 12.3.1 Overview

The bean definitions described in the front are all XML-based definitions of configuration metadata, and the use of annotations to reduce the number of configurations is described in the "12.2 annotation Implementation Bean Dependency Injection" section, but it does not completely eliminate the bean definitions in the XML configuration file. So there is no way to completely eliminate the XML configuration bean definition.

Spring provides automatic registration of bean definitions by scanning special annotation classes in the classpath. As with annotation-driven transactions, you need to turn on automatic scanning and register bean definition support in the following ways (Resources/chapter12/componentdefinitionwithannotation.xml):

  Java code: Java code    <beans xmlns= "Http://www.springframework.org/schema/beans"        xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"         xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"        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/aop           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd          http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd ">           <aop:aspectj-autoproxy />           <context:component-scan base-package= "Cn.javass.spring.chapter12"/>      </beans>  

The <context:component-scan> tag is used to indicate that the bean definition needs to be automatically registered, and the classpath location of the scan is specified through the Base-package property.

The <context:component-scan> tag will automatically turn on "Annotation implementation bean Dependency Injection " support.

Here we also use <aop:aspectj-autoproxy/> to open Spring support for @aspectj-style facets.

Spring's implementation of the bean definition based on annotations supports the following three annotations: Spring's @component annotations and extended @repository, @Service, @Controller, as shown in Figure 12-1; The @managedbean annotations defined in the JSR-250 1.1 release are one of the Java EE 6 standard specifications, not included in the JDK, and need to be used in an application server environment (such as JBoss), as shown in Figure 12-2; JSR-330 's @named annotation , as shown in Figure 12-3.

Figure 12-1 Spring comes with @component annotations and extensions

Figure 12-2 @managedbean annotations and custom extensions defined in JSR-250

Figure 12-3 @named annotations and custom extensions for JSR-330

The custom extensions in Figure 12-2 and figure 12-3 are in part to complement the spring-brought pattern annotation extension customization, not included in the Java EE 6 specification, and the corresponding service layer, DAO layer functionality in Java EE 6 is done by EJBS.

In Java EE, some annotations run in multiple places, such as @named allow placement in type, field, method parameters, so generally placed on the type of the definition, placed on the parameters, methods and other top of the general representative use (such as dependency injection, etc.). 12.3.2 Spring 's own @component annotations and extensions

One, @Component: Define Spring Management beans, using the following methods :

Java code: Java code @Component ("identifier") Pojo class

Use the @component annotation on the class to indicate that the class is defined as a spring management bean, using the default value (optional) attribute to represent the bean identifier.

 

1. Define the Test Bean class:

Java code: Java code Package cn.javass.spring.chapter12;   Import org.springframework.beans.factory.annotation.Autowired;   Import Org.springframework.context.ApplicationContext;   Import org.springframework.stereotype.Component;       @Component ("Component") public class Testcompoment {@Autowired private applicationcontext ctx;       Public ApplicationContext Getctx () {return ctx; }   }


2, spring configuration file using Chapter12/componentdefinitionwithannotation.xml can and without modification;

3. Define test classes and test methods:

  Java code: Java code    package cn.javass.spring.chapter12;  //Omit import   public class componentdefinitionwithannotationtest {       private  static String configLocation =  "classpath:chapter12/ Componentdefinitionwithannotation.xml ";       private static  Applicationcontext ctx = new classpathxmlapplicationcontext (configLocation);        @Test        public void testcomponent ()  {            TestCompoment component =  Ctx.getbean ("component",  testcompoment.class);            assert.assertnotnull (Component.getctx ());       }  }  

    Test success indicates that the Pojo class annotated by @component will automatically be recognized by spring and registered in the spring container, and automatically supports automatic assembly.

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.