第一種:注解配置aop
注解配置aop(使用 aspectj 類庫實作的),大緻分為三步:
1. 使用注解@aspect來定義一個切面,在切面中定義切入點(@pointcut),通知類型(@before, @afterreturning,@after,@afterthrowing,@around).
2. 開發需要被攔截的類。
3. 将切面配置到xml中,當然,我們也可以使用自動掃描bean的方式。這樣的話,那就交由spring aop容器管理。
另外需要引用 aspectj 的 jar 包: aspectjweaver.jar aspectjrt.jar
執行個體:
user.java
package com.bjsxt.model;
public class user {
private string username;
private string password;
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
public string getpassword() {
return password;
public void setpassword(string password) {
this.password = password;
}
/**
*接口類
*/
package com.bjsxt.dao;
import com.bjsxt.model.user;
public interface userdao {
public void save(user user);
}
實作接口:
package com.bjsxt.dao.impl;
import org.springframework.stereotype.component;
import com.bjsxt.dao.userdao;
@component("u")
public class userdaoimpl implements userdao {
public void save(user user) {
system.out.println("user save11d!");
/*throw new runtimeexception("exception");*/ //抛異常
操作類:
package com.bjsxt.service;
import javax.annotation.resource;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
@component("userservice")
public class userservice {
private userdao userdao;
public void init() {
system.out.println("init");
public void add(user user) {
userdao.save(user);
public userdao getuserdao() {
return userdao;
@resource(name="u")
public void setuserdao( userdao userdao) {
this.userdao = userdao;
public void destroy() {
system.out.println("destroy");
加入aop
package com.bjsxt.aop;
import org.aspectj.lang.annotation.after;
import org.aspectj.lang.annotation.afterreturning;
import org.aspectj.lang.annotation.afterthrowing;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.before;
import org.aspectj.lang.annotation.pointcut;
@aspect
@component
public class loginterceptor {
@pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void mymethod(){};
/*@before("execution(public void com.bjsxt.dao.impl.userdaoimpl.save(com.bjsxt.model.user))")*/
@before("mymethod()")
public void before() {
system.out.println("method staet");
}
@after("mymethod()")
public void after() {
system.out.println("method after");
@afterreturning("execution(public * com.bjsxt.dao..*.*(..))")
public void afterreturning() {
system.out.println("method afterreturning");
@afterthrowing("execution(public * com.bjsxt.dao..*.*(..))")
public void afterthrowing() {
system.out.println("method afterthrowing");
配置檔案
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.1.xsd
"><!-- 要添加最後2行 -->
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/> <!-- 自動掃描 -->
<aop:aspectj-autoproxy/> <!-- 要添加本行 -->
</beans>
測試類:
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
//dependency injection
//inverse of control
public class userservicetest {
@test
public void testadd() throws exception {
classpathxmlapplicationcontext ctx = new classpathxmlapplicationcontext("applicationcontext.xml");
userservice service = (userservice)ctx.getbean("userservice");
system.out.println(service.getclass());
service.add(new user());
system.out.println("###");
ctx.destroy();
結果:
class com.bjsxt.service.userservice$$enhancerbycglib$$7b201784
method staet
user save11d!
method afterreturning
method after
###
注意:
@aspect:意思是這個類為切面類
@componet:因為作為切面類需要 spring 管理起來,是以在初始化時就需要将這個類初始化加入 spring 的管理;
@befoe:切入點的邏輯(advice)
execution…:切入點文法
第二種:xml配置aop
執行個體同上:隻是配置檔案不同
<context:component-scan base-package="com.bjsxt"/>
<bean id="loginterceptor" class="com.bjsxt.aop.loginterceptor"></bean>
<aop:config>
<aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"
id="servicepointcut"/>
<aop:aspect id="logaspect" ref="loginterceptor">
<aop:before method="before" pointcut-ref="servicepointcut" />
</aop:aspect>
</aop:config>
下面的<beans>是spring的配置标簽,beans裡面幾個重要的屬性:
xmlns:
是預設的xml文檔解析格式,即spring的beans。位址是http://www.springframework.org/schema/beans。
通過設定這個屬性,所有在beans裡面聲明的屬性,可以直接通過<>來使用,比如<bean>等等。
xmlns:xsi:
是xml需要遵守的規範,通過url可以看到,是w3的統一規範,後面通過xsi:schemalocation來定位所有的解析檔案。
xmlns:aop:
這個是重點,是我們這裡需要使用到的一些語義規範,與面向切面aop相關。
xmlns:tx:
spring中與事務相關的配置内容。
一個xml檔案,隻能聲明一個預設的語義解析的規範。
例如上面的xml中就隻有beans一個是預設的,其他的都需要通過特定的标簽來使用,比如aop,它自己有很多的屬性,如果要使用,前面就必須加上aop:xxx才可以。比如上面的aop:config。
類似的,如果預設的xmlns配置的是aop相關的語義解析規範,那麼在xml中就可以直接寫config這種标簽了。
作者:傳回首頁 偶my耶
來源:51cto