静态代理:
public interface Subject {
public String add();
}
public class ResouSubject implements Subject {
public String add() {
System.out.println("service add ");
return "";
}
}事务已开启!!public class ProxySubject implements Subject { //对象间交互 private Subject subject; public String add() { System.out.println("事务已开启!!"); return subject.add(); } public Subject getSubject() { return subject; } public void setSubject(Subject subject) { this.subject = subject; }}//静态代理,测试类public class Subjectscatic { @Test public void stctic(){ //真实主题对象 Subject subject=new ResouSubject(); //代理对象 ProxySubject proxySubject=new ProxySubject(); proxySubject.setSubject(subject); proxySubject.add(); }}结果:
service add
---------------------------------------------------------------
JDK动态代理:
public interface Stuer {
public String add();
public String edit();
}
public class StuerImpl implements Stuer {
public String add() {
System.out.println("===add===");
return "add";
}
public String edit() {
System.out.println("===edit===");
return "edit";
}
}测试类:public class JDKTestdong { //动态JDK @Test public void testjdk(){ final Stuer use=new StuerImpl(); Stuer pro= (Stuer) Proxy.newProxyInstance(use.getClass().getClassLoader(), use.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("事务已开启了耶!!!"); method.invoke(use,args); return null; } }); pro.add(); pro.edit(); }}JDK动态测试结果:事务已开启了耶!!!
===add===
事务已开启了耶!!!
===edit===
-------------------------------------------------------------------------
//Cglib动态代理
public interface UserDao {
public String add();
}public class IserDaoinpl implements UserDao { public String add() { System.out.println("add123456"); return "add"; }}//Cglib动态测试类public class CliinTest { @Test public void fun(){ final UserDao udo=new IserDaoinpl(); //ENhancer对象 Enhancer enhancer=new Enhancer(); //在内存中构建业务类的子类 enhancer.setSuperclass(udo.getClass()); enhancer.setCallback(new MethodInterceptor() { public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("事务已经开启了哦"); methodProxy.invoke(udo,objects); return null; } }); UserDao pro= (UserDao) enhancer.create(); pro.add(); }}//cglib动态测试结果:事务已经开启了哦
add123456
------------------------------------------------------------------------
经典AOP----异常:
public interface TherService {
public void rend();
}public class SpringExction implements ThrowsAdvice { public void afterThrowing(Exception ex){ System.out.println("错误"); }}public class stuthrow implements TherService {//核心业务 public void rend() { int i=5/0; System.out.println("人生若是无误,铅笔何须橡皮!"); }}applicationContestSpringThrowing.xml
<!--目标对象--> <bean id="stuthrow" class="cn.happy.SpringThrowing.stuthrow"></bean> <!--增强--> <bean id="SpringExction" class="cn.happy.SpringThrowing.SpringExction"></bean> <!--aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="stuthrow"></property> <property name="interceptorNames" value="SpringExction"></property> </bean></beans>AOP异常测试类:
public class SpringThrow { //异常 @Test public void throwing(){ ApplicationContext ctt=new ClassPathXmlApplicationContext("applicationContestSpringThrowing.xml"); TherService stu= (TherService) ctt.getBean("proxyService"); stu.rend(); }}---------------------------------------------------------经典AOP前置
public class SomeService { public void servicr(){ System.out.println("service------------111"); }}public class SAdaoimpl implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("=====前置====="); }}applicationContestSpringAop011.xml<!--目标对象--> <bean id="SAdaoimpl" class="cn.happy.SpringAdopBefore01.SomeService"></bean> <!--增强--> <bean id="sAdaoimpl" class="cn.happy.SpringAdopBefore01.SAdaoimpl"></bean> <!--aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="SAdaoimpl"></property> <property name="interceptorNames" value="sAdaoimpl"></property> </bean>前置测试类:
public class SpringAdopTest011 {
//前置
@Test
public void testaop(){
ApplicationContext cts=new ClassPathXmlApplicationContext("applicationContestSpringAop011.xml");
SomeService service= (SomeService) cts.getBean("proxyService");
service.servicr();
}
}
------------------------------------------------------------
经典AOP后置
public class afer {
public void ter(){
System.out.println("+++++after++++");
}
}
public class afteimpl implements AfterReturningAdvice {applicationContestSpringAfter011.xml
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("--------后置--------");
}
}
<!--目标对象-->
<bean id="afer" class="cn.happy.SpringAopAfter02.afer"></bean>
<!--增强-->
<bean id="afteimpl" class="cn.happy.SpringAopAfter02.afteimpl"></bean>
<!--aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="afer"></property>
<property name="interceptorNames" value="afteimpl"></property>
</bean>后置测试类:
public class SpringAopAfterTest {------------------------------------------------------------------------
//后置
@Test
public void testafter(){
ApplicationContext cts=new ClassPathXmlApplicationContext("applicationContestSpringAfter011.xml");
afer sf= (afer) cts.getBean("proxyService");
sf.ter();
}
}
经典AOP环绕
public interface SomeService {
public void doservice();
}
public class methservice implements SomeService {
public void doservice() {
System.out.println("人生若是无误,铅笔何须橡皮!");
}
}public class MyMethodterpter implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("=======before======="); methodInvocation.proceed(); System.out.println("========after======="); return null; }}applicationContestSpringhuanrao.xml
<!--目标对象--> <bean id="methservice" class="cn.happy.huanrao.methservice"></bean> <!--增强--> <bean id="MyMethodterpter" class="cn.happy.huanrao.MyMethodterpter"></bean> <!--aop--> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="methservice"></property> <property name="interceptorNames" value="MyMethodterpter"></property> </bean>环绕测试类:public class HuanRaotest { //环绕 @Test public void huanrao(){ ApplicationContext app=new ClassPathXmlApplicationContext("applicationContestSpringhuanrao.xml"); SomeService som= (SomeService) app.getBean("proxyService"); som.doservice(); }}