静态代理
通过接口实现
动态代理
1 编写java代理类
public
class
SecurityHandler
implements
InvocationHandler
{
private Object targetObject;
//创建代理类
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
public Object newProxy(Object targetObject)
{
this.targetObject=targetObject;
return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
targetObject.getClass().getInterfaces(),
this);
}
//
public Object invoke(Object proxy, Method method, Object[] args)
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
throws Throwable
{
checkSecurity();
Object result=null;
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
try
{
result=method.invoke(this.targetObject, args);
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
} catch (RuntimeException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
private void checkSecurity()
{
System.out.println("------------checkSecurity-------------");
}
}
2 调用
SecurityHandler handler
=
new
SecurityHandler();
UserManager userManager
=
(UserManager)handler.newProxy(
new
UserManagerImpl());
userManager.addUser(
"
张三
"
,
"
123456
"
);
----------------------------------------------------------------------------------------------------------------------
spring对AOP的支持(采用Annotation的方式)注解方式
AOP:
* Cross cutting concern 横切性关注点
* Aspect 切面,是对横切性关注点的模块化
* Advice 横切性关注点的实现,切面的实现
* Piontcut Advice应用范围的表达式
* Joinpoint 执行的点,spring中是方法
* Weave Advice
1 spring的依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar(aspectjrt.jar/aspectjweaver.jar)
---applicationContext.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop ****************
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>
****
<
context:annotation-config
/>
//开启配置项
<
aop:aspectj-autoproxy
/>
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
<
bean
id
="personDao"
class
="org.spring.dao.impl.PersonDaoImpl"
/>
</
beans
>
2 采用Aspect定义切面,在Aspect定义Pointcut和Advice
示例:
@Aspect
//
切面
![](http://img.e-com-net.com/image/product/a1f351c222f2407bb0921afbb62a3c11.gif)
public
class
SecurityHandler
{
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
/** *//**
* Pointcut,Pointcut的名称就是allAddMethod,此方法不能有返回值和参数,该方法只一个标识
* Pointcut的内容是一个表达式,描述那些对象的那些方法(订阅Joinpoint)
* 定义
*/
@Pointcut("execution(* add*(..)) || execution(* del*(..))")
//@Pointcut("execution(* org.my.biz..*.*(..))")
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
private void allAddMethod()
{};
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
/** *//**
* 定义Advice,标识在那个切入点何处织入些方法
*/
@Before("allAddMethod() && args(name)")//前置通知
[@AfterReturning("allAddMethod()")后置通知,可以同时存在]
[@After("allAddMethod()")最终通知]
[AfterThrowing("allAddMethod()")例外通知,抛异常时]
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
private void checkSecurity(String name)
{
System.out.println("------------checkSecurity-------------");
System.out.println(name);
}
@Around("allAddMethod()")//环绕通知
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
public Object doBasicProfiling(ProceedingJoinPiont pjp) throws Throwable
{
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
if()
{//判断是否有权限
System.out.println("进入方法");
Object result=pjp.proceed();//如果不调用这个方法,后面的业务bean及切面将不会执行
}
return result;
}
}
3 启用Aspect对Annotation的支持并且将Aspect类和目标对象配置到Ioc容器中
<
aop:aspectj-autoproxy
/>
<
bean
id
="securityHandler"
class
="com.my.spring.SecurityHandler"
/>
<
bean
id
="userManager"
class
="com.my.spring.UserManagerImpl"
/>
4 调用
BeanFactory factory
=
new
ClassPathXmlApplicationContext(
"
applicationContext.xml
"
);
UserManager userManager
=
(UserManager)factory.getBean(
"
userManager
"
);
userManager.addUser(
"
张三
"
,
"
123456
"
);
注意:在这各方法定义中,切入点的方法是不被执行的,它存在的目的仅仅是为了重用切入点
即Advice中通过方法名引用这个切入点
手动添加schema文件,方法如下:
windows->preferences->myeclipse->files and editors->xml->xmlcatalog
--------------------------------------------------------------------------------------------------
spring对AOP的支持(采用配置文件的方式)
Aspect默认情况下不用实现接口,但对于目标对象(UserManagerImpl.java),在默认情况下必须实现接口,
如查没有实现接口必须引入CGLIB库
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得能数值、方法名等等
1 spring的依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar(aspectjrt.jar/aspectjweaver.jar)
2 定义切面类
public
class
SecurityHandler
{
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
private void checkSecurity(JoinPoint joinPoint)
{
Object[] args=joinPoint.getArgs();//得到参数集合
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
System.out.println(joinPoint.getSignature().getName());//得到方法名
System.out.println("------------checkSecurity-------------");
}
}
3 在配置文件中配置
* xml方式
<
bean
id
="securityHandler"
class
="com.my.spring.SecurityHandler"
/>
<
bean
id
="userManager"
class
="com.my.spring.UserManagerImpl"
/>
<
aop:config
>
<
aop:aspect
id
="security"
ref
="securityHandler"
>
<
aop:pointcut
id
="allAddMethod"
expression
="execution(* add*(..))"
/>
<
aop:before
method
="checkSecurity"
pointcut-ref
="allAddMethod"
/>
<!--
<aop:after method="checkSecurity" pointcut-ref="allAddMethod"/>
<aop:after-returning method="checkSecurity" pointcut-ref="allAddMethod"/>
<aop:after-throwing method="checkSecurity" pointcut-ref="allAddMethod"/>
<aop:around method="checkSecurity" pointcut-ref="allAddMethod"/>
-->
</
aop:aspect
>
</
aop:config
>
4 调用
BeanFactory factory
=
new
ClassPathXmlApplicationContext(
"
applicationContext.xml
"
);
UserManager userManager
=
(UserManager)factory.getBean(
"
userManager
"
);
userManager.addUser(
"
张三
"
,
"
123456
"
);
---------------------------------------------------------------------------------------------------
JDK动态代理和CGLIB
手动编写代码:
public
class
CGlibProxy
implements
MethodInterceptor
{
private Object targetObject;//代理目标对象
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
public Object createProxyIntance(Object targetObject)
{
this.targetObject=targetObject;
Enhancer enhancer=new Enhancer();//该类用于生成代理对象
//非final,继承目标类,并对非final方法进行复盖
enhancer.setSuperclass(this.targetObject.getClass());//设置父类
enhancer.setCallback(this);//设置回调用对象为本身
return enhancer.create();
}
@Override
public Object intercept(Object proxy, Method mothod, Object[] args,
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
MethodProxy methodProxy) throws Throwable
{
PersonServiceBean bean=(PersonServiceBean)this.targetObject;
Object result=null;
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
if(bean.getUser()!=null)
{//判断是否为空
result=methodProxy.invoke(targetObject, args);
}
return null;
}
}
//
test方法
CGlibProxy cglibProxy
=
new
CGlibProxy();
PersonServiceBean personServiceBean
=
(PersonServiceBean)cglibProxy.createProxyIntance(
new
PersonServiceBean());
personServiceBean.save(
"
dddddd
"
);
spring对AOP的支持
1 如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2 如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3 如查目标对象没有实现接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
如何强制使用CGLIB实现AOP?
* 添加CGLIB库,在SPRING_HOME/cglib/*.jar
* 在该spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
JDK动态代理和CGLIB字节码生成的区别:
* JDK动态代理只能对实现了接口的类生成代理,而不能针对类
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
因为是继承,所以该类或方法最好不要声明成final
-------------------------------------------------------------------------------------------------------
spring1.2 AOP实现方法
1 advice类
public
class
LogAdvice
implements
MethodBeforeAdvice
{
public void before(Method m, Object[] arg1, Object arg2)
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
throws Throwable
{
System.out.println(new Date()+"时间,调用了,"+m.getName()+"方法,参数为"+Arrays.toString(arg1));
}
}
![](http://img.e-com-net.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
![](http://img.e-com-net.com/image/product/a1f351c222f2407bb0921afbb62a3c11.gif)
public
class
JiaAdvice
implements
AfterReturningAdvice
{
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
Object arg3) throws Throwable
{
System.out.println("给qian了");
}
}
2 biz类
public
class
HelloBizImpl
implements
HelloBiz
{
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
public void say(String username)
{
System.out.println("欢迎你"+username);
}
![](http://img.e-com-net.com/image/product/abc43b71b9f0448bb8997370851368b9.gif)
public void sayHello(String word)
{
System.out.println(word);
}
}</spa
分享到:
评论