使用CGLIB实现AOP功能与AOP概念解释
来源:互联网 发布:搜索引擎优化bwysj 编辑:程序博客网 时间:2024/06/02 10:43
PersonServiceBean.java
package com.allen.service.imp;public class PersonServiceBean {public String user = "";public String getUser() {return user;}public PersonServiceBean(){}public PersonServiceBean(String user){this.user=user;}public String getPersonName(Integer personid) {System.out.println("我是getPersonName()");return "AAAAA";}public void save(String name) {System.out.println("我是Save()");}public void update(String name) {System.out.println("我是update()");}}
PersonService.java
package com.allen.service;
public interface PersonService {
public void save (String name);
public void update(String name);
public String getPersonName(Integer personid);
}
CGLibFactory.java
package com.allen.aop;
import java.lang.reflect.Method;
import com.allen.service.imp.PersonServiceBean;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CGLibFactory implements MethodInterceptor{
private Object targetObject;
public Object createProxyIntance (Object tarObject){
this.targetObject = tarObject;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.targetObject.getClass());//添加非final修饰的类
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object arg0, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
PersonServiceBean bean = (PersonServiceBean) this.targetObject;
Object result = "";
if(bean.getUser()!=""){
result = method.invoke(targetObject, args);
}
return null;
}
}
AopTest.java
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import com.allen.aop.CGLibFactory;
import com.allen.aop.JDKProxyFactory;
import com.allen.service.PersonService;
import com.allen.service.imp.PersonServiceBean;
public class AopTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void proxyTest2(){
CGLibFactory factory = new CGLibFactory();
PersonServiceBean service = (PersonServiceBean) factory.createProxyIntance(new PersonServiceBean("asf"));
service.save("888");
}
}
- 使用CGLIB实现AOP功能与AOP概念解释
- 使用CGLIB实现AOP功能与AOP概念解释
- 使用CGLIB实现AOP功能与AOP概念解释
- 16_传智播客Spring2.5视频教程_使用CGLIB实现AOP功能与AOP概念解释
- 使用CGLIB实现AOP功能
- 使用CGLIB实现AOP功能
- 使用CGLIB实现AOP功能核心代码
- 使用 CGLIB 实现 AOP
- AOP之CGLIB实现AOP功能
- 使用jdk和cglib实现spring的aop功能
- Spring学习笔记(14)----使用CGLIB实现AOP功能
- spring aop与cglib aop
- 动态代理实现AOP与cglib实现AOP
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与CGLIB应用
- excel常用函数大全(一)
- 避免野指针铁的纪律
- 平平庸庸非本心
- android应用程序的安装和卸载
- LightOJ 1388 Trapezium Drawing
- 使用CGLIB实现AOP功能与AOP概念解释
- 快速排序,好像不是很懂
- error: expected class-name before ‘{’ token
- 关掉Ctrl+Alt+↓(方向键)转屏功能
- 透析WINCE的控制面板
- CF 254B(日期)
- source insight的配置
- 杭电OJ题 1040 As Easy As A+B 解题报告
- Tabular: 在 Vim 中对齐文本