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" &NBSP;&NBSP;&NBSP;&NBSP;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 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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>
<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
@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; } }
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 ()); } }
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 ());
}
}
The test success indicates that the Pojo class that is annotated by @component is automatically recognized by spring and registered in the spring container, and automatically supports automatic assembly.
@AspectJ-style facets can be identified by @compenent annotations as spring management beans, and @aspect annotations cannot be automatically identified and registered as beans by spring, and must be done through @component annotations, as shown in the following example:
Java code: Java code package cn.javass.spring.chapter12.aop; //omit import @Component @Aspect public class testaspect { @ Pointcut (value= "Execution (* * (..))") private void pointcut () {} @Before (value= "pointcut ()") public void before () { system.out.println ("=======before"); } }
Package CN.JAVASS.SPRING.CHAPTER12.AOP;
Omit import
@Component
@Aspect public
class Testaspect {
@Pointcut (value= "Execution (* * (..))")
private void Pointcut () {}
@Before (value= "pointcut ()") Public
Void before () {
System.out.println ("====== =before ");
}
}
defines a facet as a spring management bean through @component.
Second, @Repository: @Component extension, the @repository annotated Pojo class represents the DAO layer implementation, so as to see the note that the implementation of the DAO layer, using the same way and @component;
1. Define the Test Bean class:
Java code: Java code Package cn.javass.spring.chapter12.dao.hibernate; Import Org.springframework.stereotype.Repository; @Repository ("Testhibernatedao") public class Testhibernatedaoimpl {}
Package cn.javass.spring.chapter12.dao.hibernate;
Import org.springframework.stereotype.Repository;
@Repository ("Testhibernatedao") public
class Testhibernatedaoimpl {
}
2, spring configuration file using Chapter12/componentdefinitionwithannotation.xml can and without modification;
3. Define test methods:
Java code: Java code @Test public void Testdao () {Testhibernatedaoimpl DAO = Ctx.getbean ("Testhibernatedao", Testhibern Atedaoimpl.class); Assert.assertnotnull (DAO); }
@Test public
void Testdao () {
Testhibernatedaoimpl dao =
ctx.getbean ("Testhibernatedao", Testhibernatedaoimpl.class);
Assert.assertnotnull (dao);
}
The test success demonstrates that the Pojo class that is annotated by @repository is automatically recognized by spring and registered in the spring container, and automatically supports automatic assembly, and the class represented by the @repository annotation is implemented by the DAO layer.
Third, @Service: @Component extension, the @service annotated Pojo class represents the service layer implementation, so as to see the note to think of service layer implementation, using the same way and @component;
1. Define the Test Bean class:
Java code: Java code package cn.javass.spring.chapter12.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.service; import cn.javass.spring.chapter12.dao.hibernate.testhibernatedaoimpl; @Service ("Testservice") public class testserviceimpl { @Autowired @Qualifier ("Testhibernatedao") private TestHibernateDaoImpl dao; public testhibernatedaoimpl getdao () { return dao; } }
Package Cn.javass.spring.chapter12.service.impl;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import Org.springframework.stereotype.Service;
Import Cn.javass.spring.chapter12.dao.hibernate.TestHibernateDaoImpl;
@Service ("Testservice") public
class Testserviceimpl {
@Autowired
@Qualifier ("Testhibernatedao")
Private Testhibernatedaoimpl DAO;
Public Testhibernatedaoimpl Getdao () {
return dao;
}
}
2, spring configuration file using Chapter12/componentdefinitionwithannotation.xml can and without modification;
3. Define test methods:
Java code: Java code @Test public void Testservice () {Testserviceimpl service = Ctx.getbean ("Testservice", Testservice Impl.class); Assert.assertnotnull (Service.getdao ()); }
@Test public
void Testservice () {
Testserviceimpl service = Ctx.getbean ("Testservice", Testserviceimpl.class) ;
Assert.assertnotnull (Service.getdao ());
}
The test success demonstrates that the Pojo class that is annotated by @service is automatically recognized by spring and registered in the spring container, and automatically supports automatic assembly, and the class that is annotated by @service represents the service layer implementation.
Four, @Controller: @Component extension, the @controller annotated class represents the Web layer implementation, to see the annotations on the Web layer implementation, using the same way and @component;
1. Define the Test Bean class: