Spring Aop之 注解实现

5 篇文章 0 订阅
3 篇文章 0 订阅
1,在Spring配置文件中开启注解aop的基本开关。注意使用注解时一定要在spring配置文件中打开相关配置,否则aop是不起作用的。相关重要的配置都已经在配置文件中说明了。
<?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"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 为了启用spring对@AspectJ切面配置的支持,并保证Spring容器中
的目标Bean被一个或多个 切面自动增强,必须在Spring配置文件中配置如下片段-->
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy/>
<!-- 下面的bean用来自动生成目标bean的增强代理bean -->
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
<!-- 设置被增强的bean(也就是目标bean),及aspect切面bean所在的包名字 -->
<context:component-scan base-package="com.supan.serviceimp,com.supan.aspect" >
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>

<bean id="personAction" class="com.supan.action.PersonAction">
<property name="ps" ref="personService"></property>
</bean>
<bean id="personService" class="com.supan.serviceimp.PersonServiceImp"></bean>
</beans>


2,strutx.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="json-default">
<action name="addPerson" class="personAction" method="addPerson">
<result name="success">/person/personResult.jsp</result>
</action>
</package>
</struts>


3,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>



4,Aspect切面Bean
package com.supan.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用@Aspect定义一个切面类
@Aspect
public class LogAspect {
//在目标方法调用之前写系统日志
/*
* 使用before增强处理只能在目标方法执行之前植入增强,使用before
* 增强处理无须理会目标方法的执行,所以before处理无法阻止目标
* 方法的执行,before增强处理执行时,目标方法还未获得执行的机会。
* 所以before增强处理无法访问目标方法的返回值。
*/
@Before(value="execution(* com.supan.serviceimp.*.add*(..))")
public void writeLog(){
System.out.println("在目标函数调用之前写系统日志");
}
/*
* AfterReturning增强处理在目标方法正常完成后被植入
* 含有两个属性pointcut/value returning
* 使用returning属性还有一个额外的作用:它可用于限定切入点只匹配具有
* 对应返回值类型的方法--假如在上面的log()方法中定义rvt的形参类型是String
* 则该切入点只匹配包下的返回值类型为String的所有方法。
*/
@AfterReturning(pointcut="execution(* com.supan.serviceimp.*.delete*(..))",returning="rvt")
public void afterReturning(Object rvt){
System.out.println("afterReturnning增强:目标函数的返回值是:" + rvt);
}
/*
* AfterReturning增强处理只有在目标方法成功完成后才被植入
* After增强处理不管目标方法如何介乎(包括正常结束也包括异常结束)它都会被植入
*/
@After(value="execution(* com.supan.serviceimp.*.select*(..))")
public void after(){
System.out.println("selectPerson方法已经查询完毕了,不管该方法是不是正常结束");
}
/*
* Around增强处理是功能比较强大的增强处理,
* Around = Before + AfterReturning
*/
@Around(value="execution(* com.supan.serviceimp.*.around*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable{
System.out.println("目标方法还没有执行,开启事务");
jp.proceed();
System.out.println("目标方法已经执行,关闭事务");
}
}


5,Action
package com.supan.action;
import com.opensymphony.xwork2.ActionSupport;
import com.supan.service.PersonService;
public class PersonAction extends ActionSupport {
private PersonService ps;
private String name;
private int age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

public PersonService getPs() {
return ps;
}
public void setPs(PersonService ps) {
this.ps = ps;
}
public String addPerson(){
ps.addPerson();

System.out.println("----------------------");
ps.deletePerson();
System.out.println("**********************");
ps.selectPerson();
ps.aroundTest();
return null;
}
}


5,service
package com.supan.service;
public interface PersonService {
public void addPerson();
public int deletePerson();
public void selectPerson();
public void aroundTest();
}
package com.supan.serviceimp;
import org.springframework.stereotype.Component;

import com.supan.service.PersonService;
@Component
public class PersonServiceImp implements PersonService {
public void addPerson() {
System.out.println("开始添加人员");
System.out.println("添加人员结束");
}
public int deletePerson() {
return 0;
}
public void selectPerson(){
System.out.println("人物已经查询完毕");
}
public void aroundTest() {
System.out.println("执行around方法结束");
}
}


6,jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="addPerson">
<s:textfield label="姓名" name="name"></s:textfield>
<s:textfield label="年龄" name="age"></s:textfield>
<s:textfield label="地址" name="address"></s:textfield>
<s:submit name="添加"></s:submit>
</s:form>
</body>
</html>


7,系统后台输出
在目标函数调用之前写系统日志
开始添加人员
添加人员结束
----------------------
afterReturnning增强:目标函数的返回值是:0
**********************
人物已经查询完毕
selectPerson方法已经查询完毕了,不管该方法是不是正常结束
目标方法还没有执行,开启事务
执行around方法结束
目标方法已经执行,关闭事务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值