spring(四)AOP相關概念

名詞解釋

AOP定義:面向切面編程

Joinpoint:連接點,即目標對象中,所有可以增強的方法

Pointcut:切入點,即目標對象中,已經增強的方法

Advice:通知,即需要增強的代碼

Target:目標對象,即被代理的對象

Weaving:織入,將通知應用到切入點的過程

Proxy:代理,將通知織入到目標對象之後,形成代理對象

aspect:切面,切入點+通知



AOP實現原理:

1.動態代理:被代理對象必須要實現接口

2.CGLib代理:可以對任何類生成代理,代理的原理是對目標對象進行繼承代理 


相關代碼:

用戶接口:

public interface UserService {

	void add();
	
	void delete();
	
	void update();
	
	void search();
	
}

用戶接口實現類:

public class UserServiceImpl implements UserService {

	@Override
	public void add() {
		System.out.println("add");
	}

	@Override
	public void delete() {
		System.out.println("delete");
	}

	@Override
	public void update() {
		System.out.println("update");
	}

	@Override
	public void search() {
		System.out.println("search");
	}

}

通知類:

public class UserAdvice {

	//前置通知:目標方法運行前調用
	public void before(){
		System.out.println("before");
	}
	
	//後置通知:方法出現異常不會調用此通知
	public void afterReturning(){
		System.out.println("afterReturning");
	}
	
	//環繞通知:目標方法執行前後都被調用
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("around before");
		Object proceed = joinPoint.proceed();
		System.out.println("around after");
		return proceed;
	}
	
	public void afterException(){
		System.out.println("afterException");
	}
	
	//後置通知:方法出現異常也調用此通知
	public void after(){
		System.out.println("after");
	}
	
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 導入aop約束 -->
<!-- 1.配置目標對象 -->
	<bean name="userService" class="com.milan.spring.service.impl.UserServiceImpl"></bean>
<!-- 2.配置通知對象 -->
	<bean name="myAdvice" class="com.milan.advice.UserAdvice"></bean>
<!-- 3.配置將通知織入目標對象 -->
	<aop:config>
		<!-- 配置切入點
			public void com.milan.spring.service.impl.UserServiceImpl.add()
			void com.milan.spring.service.impl.UserServiceImpl.add()
			* com.milan.spring.service.impl.UserServiceImpl.add()
			* com.milan.spring.service.impl.UserServiceImpl.*(..)
			* com.milan.spring.service.impl.*ServiceImpl.*(..)
		 -->
		<aop:pointcut expression="execution(public void com.milan.spring.service.impl.UserServiceImpl.add())" id="user_add"/>
		<aop:aspect ref="myAdvice">
			<aop:before method="before" pointcut-ref="user_add" />
			<aop:after-returning method="afterReturning" pointcut-ref="user_add"/>
			<aop:around method="around" pointcut-ref="user_add"/>
			<aop:after-throwing method="afterException" pointcut-ref="user_add"/>
			<aop:after method="after" pointcut-ref="user_add"/>
		</aop:aspect>
	</aop:config>

</beans>

測試:

@RunWith(SpringJUnit4ClassRunner.class)//創建容器
@ContextConfiguration("classpath:applicationContext-advice.xml")//指定配置文件位置
public class AdviceTest {
	
	@Resource(name="userService")
	private UserService service;
	
	@Test
	public void Test1(){
		service.add();
	}
	
}
輸出如下:

before
around before
add
after
around after
afterReturning



註釋配置:

配置切面:

@Aspect
//表示該類爲一個通知類
public class UserAdviceByAnnotation {

	@Pointcut("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void advice(){}
	
	
	//前置通知:目標方法運行前調用
	@Before("UserAdviceByAnnotation.advice()")
	public void before(){
		System.out.println("before");
	}
	
	//後置通知:方法出現異常不會調用此通知
	@AfterReturning("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void afterReturning(){
		System.out.println("afterReturning");
	}
	
	//環繞通知:目標方法執行前後都被調用
	@Around("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("around before");
		Object proceed = joinPoint.proceed();
		System.out.println("around after");
		return proceed;
	}
	
	@AfterReturning("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void afterException(){
		System.out.println("afterException");
	}
	
	//後置通知:方法出現異常也調用此通知
	@After("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void after(){
		System.out.println("after");
	}
	
}
配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 導入aop約束 -->
<!-- 1.配置目標對象 -->
	<bean name="userService" class="com.milan.spring.service.impl.UserServiceImpl"></bean>
<!-- 2.配置通知對象 -->
	<bean name="myAdvice" class="com.milan.advice.UserAdviceByAnnotation"></bean>
<!-- 3.配置將通知織入目標對象 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>	
測試:

@RunWith(SpringJUnit4ClassRunner.class)//創建容器
@ContextConfiguration("classpath:applicationContext-advice-annotation.xml")//指定配置文件位置
public class AdviceByAnnotationTest {
	
	@Resource(name="userService")
	private UserService service;
	
	@Test
	public void Test1(){
		service.add();
	}
	
}
輸出如下:

around before
before
add
around after
after
afterException
afterReturning

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章