spring的aop实现案例

1、首先开发业务逻辑接口(因为动态代理只能针对接口所实现)IXiangQinInterface.java

package com.springtest.service;

//定义接口
public interface IXiangQinInterface {
	public void meet();
}
2、开发实现业务逻辑接口的类 BoyXiangQin.java

package com.springtest.service.impl;

import com.springtest.service.IXiangQinInterface;

public class BoyXiangQin implements IXiangQinInterface {

	private String name;
	
	
	@Override
	public void meet() {
		System.out.println("姓名是【"+this.name+"】正在相亲");
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}

}
3、编写切面类(通用服务类):几种增强 HunJieAspect.java

package com.springtest.util;

import org.aspectj.lang.ProceedingJoinPoint;

//通用服务类(切面类)
public class HunJieAspect {
	//前置增强,意思就是在业务逻辑方法调用前所植入的通用服务方法
	//一般这个方法用在权限的检查
	public void before() {
		System.out.println("婚戒所开始进行相亲会员的资格审查");
	}
	//后置增强,一般就是在业务逻辑方法执行完毕之后所植入的通用方法
	//一般是用于资源的释放,如数据库连接对象的资源的释放
	public void after() {
		System.out.println("这次婚戒所所举办的相亲结束");
	}
	//异常处理,因为一旦业务逻辑出现异常,那么就调用该方法
	//一般主要就是用于日志记录异常
	public void afterException(Throwable th) {
		System.out.println("相亲出现了异常"+th.getMessage());
	}
	//返回值的增强,一般就是当执行一个业务逻辑方法的时候,有返回值的时候,那么就会植入该方法
	//一般可以用作日志记录
	public void afterReturn(Object result) {
		if(result==null) {
			System.out.println("相亲失败");
		}else {
			System.out.println("相亲成功");
		}
	}
	//环绕增强,就是在业务逻辑方法执行期间所植入的方法。一般用以事务处理。
	//一般可以用作日志记录
	public Object around(ProceedingJoinPoint joinpoint) throws Throwable{
		System.out.println("开始约会了!");
		Object result = joinpoint.proceed();
		return result;
	}
}
4、在spring的配置文件里,编写切面类和业务逻辑类的bean application.xml
<!-- 配置切面类的bean -->
	<bean id="myPointCut" class="com.springtest.util.HunJieAspect"></bean>
	<!-- 配置业务逻辑类的bean对象 -->
	<bean p:name="张三" id="boyXiangQin" class="com.springtest.service.impl.BoyXiangQin"></bean>
5、修改applicationContext.xml里的xml的验证,加入aop的命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
6、配置切面及切入点,还有增强

<!-- 配置AOP -->
	<!-- aop:config proxy-target-class="false"默认采用jdk的动态代理,只能针对接口的实现 -->
	<!-- aop:config proxy-target-class="true"采用cglib的动态代理,可以直接针对普通的类来实现 -->
	<aop:config>
		<!-- 配置切面 -->
		<aop:aspect id="myaspect" ref="myPointCut">
			<!-- 配置增强和表达式 -->
			<!-- 配置切面表达式:表达式表示的是项目中哪些类或者哪些方法要被执行增强 -->
			<aop:pointcut expression="execution(* com.springtest.service.impl.*.*(..))" id="mypointexpress"/>
			<aop:before method="before" pointcut-ref="mypointexpress"/>
			<aop:after method="after" pointcut-ref="mypointexpress"/>
			<aop:around method="around" pointcut-ref="mypointexpress"/>
			<!-- afterRunning增强 -->
			<aop:after-returning method="afterReturn" returning="result" pointcut-ref="mypointexpress"/>
			<!-- 配置after-throwing增强 -->
			<aop:after-throwing method="afterException" throwing="th" pointcut-ref="mypointexpress"/>
		</aop:aspect>
	</aop:config>
7、编写测试类 TestUI.java
package com.springtest.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springtest.service.impl.BoyXiangQin;

public class TestUI {
	@Test
	public void testUI() {
		//首先要获取spring的配置
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		//动态代理只能针对接口实现
		IXiangQinInterface boyXiang = ctx.getBean("boyXiangQin",IXiangQinInterface.class);
		boyXiang.meet();
	}
}
8、程序执行结果:

婚戒所开始进行相亲会员的资格审查
开始约会了!
姓名是【张三】正在相亲
相亲失败
这次婚戒所所举办的相亲结束




完整的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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	<!-- 配置AOP -->
	<!-- aop:config proxy-target-class="false"默认采用jdk的动态代理,只能针对接口的实现 -->
	<!-- aop:config proxy-target-class="true"采用cglib的动态代理,可以直接针对普通的类来实现 -->
	<aop:config>
		<!-- 配置切面 -->
		<aop:aspect id="myaspect" ref="myPointCut">
			<!-- 配置增强和表达式 -->
			<!-- 配置切面表达式:表达式表示的是项目中哪些类或者哪些方法要被执行增强 -->
			<aop:pointcut expression="execution(* com.springtest.service.impl.*.*(..))" id="mypointexpress"/>
			<aop:before method="before" pointcut-ref="mypointexpress"/>
			<aop:after method="after" pointcut-ref="mypointexpress"/>
			<aop:around method="around" pointcut-ref="mypointexpress"/>
			<!-- afterRunning增强 -->
			<aop:after-returning method="afterReturn" returning="result" pointcut-ref="mypointexpress"/>
			<!-- 配置after-throwing增强 -->
			<aop:after-throwing method="afterException" throwing="th" pointcut-ref="mypointexpress"/>
		</aop:aspect>
	</aop:config>

	<!-- 配置切面类的bean -->
	<bean id="myPointCut" class="com.springtest.util.HunJieAspect"></bean>
	<!-- 配置业务逻辑类的bean对象 -->
	<bean p:name="张三" id="boyXiangQin" class="com.springtest.service.impl.BoyXiangQin"></bean>
	
</beans>








猜你喜欢

转载自blog.csdn.net/qq_28562411/article/details/78888388