上篇講述了[Spring的AOP](http://blog.csdn.net/lovesummerforever/article/details/22664647)[原理](http://blog.csdn.net/lovesummerforever/article/details/22664647),本篇將上篇中使用的動態代理解決獨立服務的問題用SpirngAOP來實現。
### 采用配置文件的方式。
1、導入相應的Spring jar包。
2、[在SpringIOC中的步驟123中已經給出](http://blog.csdn.net/lovesummerforever/article/details/22646793)。
3、將橫切性關注的問題模塊化,建立安全處理類。在SecurityHandler類中寫我們的獨立方法,也就是定義Advice(具體實現),代碼如下。
~~~
public class SecurityHandler {
private void checkSecurity() {
System.out.println("-------checkSecurity-------");
}
}
~~~
4、在配置文件中進行相關配置。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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/>
<bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/>
<aop:config>
<aop:aspect id="secutityAspect" ref="securityHandler">
<!-- 以add開頭的方法 <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> -->
<!-- com.bjpowernode.spring.*包下的所有方法.
<aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.*(..))"/>
-->
<aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.add*(..)) || execution(* com.bjpowernode.spring.*.del*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>
</aop:aspect>
</aop:config>
</beans>
~~~
需要說明幾點
指定SecutityHander為aspect:????<aop:aspectid="secutityAspect" ref="securityHandler">
Pointcut(切入點)以及事務范圍,在這里用于所有的add和del方法上:<aop:pointcutid="addAddMethod" expression="execution(*com.bjpowernode.spring.*.add*(..)) || execution(*com.bjpowernode.spring.*.del*(..))"/>
設置通知類型并指向哪個切入點:
<aop:beforemethod="checkSecurity" pointcut-ref="addAddMethod"/>
將目標類UsemrManagerImpl和Aspect類SecurityHandler配置到Spring的IOC中:<beanid="userManager"class="com.bjpowernode.spring.UserManagerImpl"/>
<bean id="securityHandler"class="com.bjpowernode.spring.SecurityHandler"/>
5、客戶端調用代碼如下所示。
~~~
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)factory.getBean("userManager");
userManager.delUser(1);
}
}
~~~
上述基本上完成了一個SpringAOP的實例。
### 如果我們在advice中,也就是在我們的安全性方法中想要獲取客戶端的數據該如何獲取呢?
我們可以在這個方法中添加參數,也就是我們JoinPoint,通過傳遞這個對象,我們可以去得到參數值,SecurityHandler類的代碼如下所示。
~~~
public class SecurityHandler {
private void checkSecurity(JoinPoint joinPoint) {
//取得參數.
for(int i=0;i<joinPoint.getArgs().length;i++)
{
System.out.println(joinPoint.getArgs()[i]);
}
System.out.println(joinPoint.getSignature().getName());
System.out.println("-------checkSecurity-------");
}
}
~~~
### 當然我們也可以采用注解的方式來實現。
1、采用注解的方式首先要加入一些jar包,*Spring_home/spring-framework-2.0\lib\aspectj\aspectjrt.jar?aspectjweaver.jar
2、SecurityHandler類的代碼如下所示。和上面的通過配置文件方式進行對比,很快就會懂得不同的標簽不同的含義。
~~~
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SecurityHandler {
/**
* 定義Pointcut,Pointcut的名稱為addAddMethod(),此方法沒有返回值和參數
* 該方法就是一個標識,不進行調用
*/
@Pointcut("execution(* add*(..))")
private void addAddMethod(){};
/**
* 定義Advice,表示我們的Advice應用到哪些Pointcut訂閱的Joinpoint上
*/
@Before("addAddMethod()")
//@After("addAddMethod()")
private void checkSecurity() {
System.out.println("-------checkSecurity-------");
}
}
~~~
3、applicationContext.xml文件中配置aspect類和目標類UserManagerImpl,同時配置實用Annotation方式,代碼如下所示。
~~~
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 啟用AspectJ對Annotation的支持 -->
<aop:aspectj-autoproxy/>
<bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/>
<bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/>
</beans>
~~~
4、客戶端調用和配置XML方式一樣。
是不是關于SpirngAOP,漸漸的懂些了。
- 前言
- Struts旅程(一)Struts簡介和原理
- struts旅程(二)Struts登錄示例
- Struts旅程(三)Struts表單處理器ActionForm(靜態動態)
- Struts旅程(四)MVC向struts MVC框架演變過程
- Struts旅程(五)struts控制器DispatchAction
- Struts旅程(六)Struts頁面轉發控制ActionForward和ActionMapping
- Hibernate旅程(一)Hibernate架構概述
- Hibernate旅程(二)Hibernate實例
- Hibernate旅程(三)Hibernate持久化對象的三個狀態
- Hibernate旅程(四)Hibernate對數據庫刪除、查找、更新操作
- Hibernate旅程(五)Hibernate映射--基本類映射和對象關系映射
- Hibernate旅程(六)Hibernate映射--繼承映射
- Hibernate旅程(七)Hibernate緩存機制--一級緩存
- Hibernate旅程(八)Hibernate緩存機制--二級緩存
- Hibernate旅程(九)Hibernate緩存機制--查詢緩存
- Spring旅程(一)為什么使用Spring
- Spring旅程(二)非Spring向Spring過渡-- Spring IOC容器的使用
- Spring旅程(三) AOP--Spring AOP容器基礎
- Spring旅程(四) AOP--Spring AOP實例
- SSH旅程(五)Spring運用到Hibernate中
- SSH旅程(六)Spring和struts結合(方案一)