Spring Learning Path (iii) Bean annotation Management AOP operations

Source: Internet
Author: User
Tags aop

Add annotations on the class above, on the method above, on the attributes, and with the bean;

Writing method: @ annotation Name (attribute name = value)

First step: Import the jar package

Import Spring-aop.jar (Spring annotations package);

Step two: Create an entity class (annotation object)

 PackageCOM.AOP;ImportOrg.springframework.context.annotation.Scope;Importorg.springframework.stereotype.Component;/*** value is a custom attribute value * @Component (value= "user") * @Controller Web Tier * @Service Business layer * @Repository persistence layer * * (value= "xx" equivalent to <bean id= "xx" class= "path" ></bean> * Four annotation methods, all functions are the same*/@Component (Value= "User") @Scope (value= "Prototype")//Create multiple cases or a single case Public classAopuser { Public voidAdd () {System.out.println ("Add---------------"); }}

Step Three: Configure the annotations file to introduce constraints

Core constraint: spring-beans.xsd

Annotation constraint: spring-context.xsd

<?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-con Text.xsd ">         <!--Turn on annotation scanning -        <!--Base-package: All annotations are scanned to the inside of the package -        <Context:component-scanBase-package= "COM.AOP"></Context:component-scan>      <!--<context:annotation-config></context:annotation-config> Annotations on scan properties -</Beans>

Fourth Step: Test

    @Test    publicvoid  Test () {        // Load Spring configuration file        New classpathxmlapplicationcontext ("Bean2.xml"); //         to get the value        of the annotation object of value Aopuser user =  (aopuser) context.getbean ("User");        User.add ();    }

Multi-Example Exercises

 PackageCom.ioc;ImportJavax.annotation.Resource;Importorg.junit.Test;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importorg.springframework.stereotype.Component;Importorg.springframework.stereotype.Service; @Service (Value= "User") Public classUser {//@Autowired Both of these methods can be@Resource (name= "Userdao")//get Userdao annotation name    PrivateUserdao Userdao; //get a DAO object that does not require a set method when using annotation mode     Public voidAdd () {System.out.println ("user=====--------");    Userdao.add (); }}//Userdao class@Component (Value= "Userdao") Public classUserdao { Public voidAdd () {System.out.println ("DAO---------------"); }}//test; config file ibid.@Test Public voidTest () {//load Spring configuration fileApplicationContext context =NewClasspathxmlapplicationcontext ("Bean2.xml");//to get the value of the annotation object of valueUser user = (user) Context.getbean ("user")); User.add ();}

AOP Operations

1. Import the jar package (see Spring Getting started)

2. Create an entity class

 PackageCom.ioc;ImportJavax.annotation.Resource; Public classUser { Public voidAdd () {System.out.println ("user=====--------"); }}//Userdao classImportOrg.aspectj.lang.ProceedingJoinPoint; Public classUserdao { Public voidAdd () {System.out.println ("Front-mounted enhancement---------------"); }     Public voidAfter () {System.out.println ("Post-Enhancement------------"); }    //Surround Notifications     Public voidArount (Proceedingjoinpoint proceedingjoinpoint)throwsthrowable{//method beforeSystem.out.println ("method before executing----------------");//implementing an enhanced approachproceedingjoinpoint.proceed ();//method AfterSystem.out.println ("Execute----------------after Method"); }}

3. Configure Bean file (import AOP constraints)

<?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"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"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-con Text.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.x SD ">           <!--Configuration Objects -         <BeanID= "User"class= "Com.ioc.User"></Bean>         <BeanID= "Userdao"class= "Com.ioc.UserDao"></Bean>         <!--Configuring AOP Operations -         <Aop:config>             <!--Configure Pointcuts -              <!--Execution (< access modifier;? < return type >< method name > (< parameters >) < exception >) -             <Aop:pointcutexpression= "Execution (* com.ioc.user.* (..))"ID= "POINTCUT1"/>             <!--to configure a plane, to put an enhancement on a method -             <Aop:aspectref= "Userdao">                 <!--configuration enhancement type; method: Set enhanced methods name in Userdao pointcut-ref: point to Pointcut ID value is to execute the method in the Userdao before executing the method in the user. -                 <Aop:beforeMethod= "Add"Pointcut-ref= "POINTCUT1"/>                 <!--Post enhancement Executes the user's method before the After method is executed -                 <aop:after-returningMethod= "after"Pointcut-ref= "POINTCUT1"/>                 <!--execute the method before and after executing the user method -                 <Aop:aroundMethod= "Arount"Pointcut-ref= "POINTCUT1"/>             </Aop:aspect>         </Aop:config></Beans>

4. Testing

 PackageCom.junit;Import Staticorg.junit.assert.*;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.ioc.User; Public classText {@Test Public voidTest () {//load Spring configuration fileApplicationContext context =NewClasspathxmlapplicationcontext ("Bean2.xml");//to get the value of the annotation object of valueUser user = (user) Context.getbean ("user"));    User.add (); }}

Spring Learning Path (iii) Bean annotation Management AOP operations

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.