AOP Manual, semi-automatic, fully automated

Source: Internet
Author: User

DAO layer

Package Com.yaorange.dao;

Public interface Studentdao {

public void savestudent ();

public void deletestudent ();
}

Implementation of the DAO layer

Package Com.yaorange.dao.impl;

Import Com.yaorange.dao.StudentDao;

public class Studentdaoimpl implements studentdao{

@Override
public void Savestudent () {
System.out.println ("Savestudent");
}

@Override
public void Deletestudent () {
System.out.println ("Deletestudent");
}

}

Tool Layer

The first one is factory-made Studentdao

Package com.yaorange.utils;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;

Import Com.yaorange.dao.StudentDao;
Import Com.yaorange.dao.impl.StudentDaoImpl;

public class MyFactory {

public static Studentdao Createstudentproxy () {
Final Studentdao Studentdaoimpl = new Studentdaoimpl ();
Final Studentaspect studentaspect = new Studentaspect ();

Studentdao Studentdao = (Studentdao) proxy.newproxyinstance (MyFactory.class.getClassLoader (),
Studentdaoimpl.getclass (). Getinterfaces (),
New Invocationhandler () {

public object invoke (object proxy, Method method, object[] args) throws Throwable {
Studentaspect.before ();
Object object = Method.invoke (Studentdaoimpl, args);
Studentaspect.after ();
return object;
}
});
return Studentdao;
}
}

Studentaspect is a aspect of the students

Package com.yaorange.utils;

Import Org.aopalliance.intercept.MethodInterceptor;
Import org.aopalliance.intercept.MethodInvocation;

public class Studentaspect implements methodinterceptor{
Start a transaction
public void before () {
System.out.println ("Start transaction");
}

Commit a transaction
public void after () {
System.out.println ("Submit a Transaction");
}

@Override
Public Object Invoke (Methodinvocation mi) throws Throwable {
Before ();
Object object = Mi.proceed ();
After ();
return object;
}
}

Manual is not to write Beans.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--***************************** Semi-automatic ****************************--
<!--Create a target class--
<bean id= "Studentdaoimpl" class= "Com.yaorange.dao.impl.StudentDaoImpl"/>

<!--to create a slice class--
<bean id= "Studentaspect" class= "Com.yaorange.utils.StudentAspect"/>

<!--3 Creating a proxy class
* Using Factory bean Factorybean, the underlying call GetObject () returns a special bean
* Proxyfactorybean for creating agent factory beans, generating special proxy objects
Interfaces: Make sure that the interfaces
Multiple values can be set through <array>
When there is only one value, value= ""
Target: Determine the destination class
Interceptornames: Notifies the name of the slice class, type string[], if a value is set value= ""
Optimize: Forcing the use of cglib
<property name= "Optimize" value= "true" ></property>
Underlying mechanism
If the target class has an interface, use the JDK dynamic proxy
If there is no interface, use cglib bytecode enhancement
If the declaration is optimize = True, the cglib is used regardless of the interface.

-


<!--proxy class--
<bean id= "Proxystudentdao" class= "Org.springframework.aop.framework.ProxyFactoryBean" >
<property name= "Optimize" value= "true"/>
<property name= "Interfaces" value= "Com.yaorange.dao.StudentDao"/>
<property name= "target" ref= "Studentdaoimpl"/>
<property name= "Interceptornames" value= "Studentaspect" ></property>

</bean>



<!--********************** Automatic ************************-->
<!--Target Class--
<bean id= "studentDaoImpl2" class= "Com.yaorange.dao.impl.StudentDaoImpl"/>

<!--******************* Slice class *************************--
<bean id= "StudentAspect2" class= "Com.yaorange.utils.StudentAspect"/>
<aop:config>

<!--* Represents the matching method in the class matching class matching this package matching package. Represents a parameter in a method--

<aop:pointcut expression= "excution (* com.yaorange.utils.*.* (..))" id= "Mypointcut"/>
<aop:advisor advice-ref= "StudentAspect2" pointcut-ref= "Mypointcut"/>
</aop:config>
</beans>

And finally, our test class.

Package com.yaorange.test;

Import Org.junit.Test;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import Com.yaorange.dao.StudentDao;
Import Com.yaorange.utils.MyFactory;

public class Aoptest {

Manual
@Test
public void Test1 () {
Studentdao Studentdao = Myfactory.createstudentproxy ();
Studentdao.savestudent ();
}
Semi - automatic
@Test
public void Test2 () {
Classpathxmlapplicationcontext CPX = new Classpathxmlapplicationcontext ("Beans.xml");
Studentdao Studentdao = (Studentdao) cpx.getbean ("Proxystudentdao");
Studentdao.savestudent ();
Studentdao.deletestudent ();
}
Automatic
@Test
public void Test3 () {
Studentdao Studentdao = Myfactory.createstudentproxy ();
Studentdao.savestudent ();
}
}

AOP Manual, semi-automatic, fully automated

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.