AOP Alliance notification Advice defines org.aopalliance.aop.Interface.Advice
Advice in accordance with the notice Spring connection point position of the target class methods can be divided into five categories
1, pre-notification org.springframework.aop.MethodBeforeAdvice
在目标方法执行前实施增强
2, after advice org.springframework.aop.AfterReturningAdvice
在目标方法执行后实施增强
3, around advice org.aopalliance.intercept.MethodInterceptor
在目标方法执行前后实施增强
4, an exception is thrown notice org.springframework.aop.ThrowsAdvice
在方法抛出异常后实施增强
Through case to explain
There are cases interface
- BeforeAdvice类
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强==================");
}
}
- StudentDao class
public interface StudentDao {
public void demo1();
public void demo2();
}
- StudentDaoImpl class
public class StudentDaoImpl implements StudentDao {
@Override
public void demo1() {
System.out.println("查询...");
}
@Override
public void demo2() {
System.out.println("保存...");
}
}
- Create a profile 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置目标类======================-->
<bean id="studentDao" class="com.aop.demo.StudentDaoImpl"></bean>
<!--前置通知类型=================-->
<bean id="myBeforeAdvice" class="com.aop.demo.BeforeAdvice"></bean>
<!--Spring的aop产生代理对象-->
<bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置的目标类-->
<property name="target" ref="studentDao"></property>
<!--实现的接口-->
<property name="proxyInterfaces" value="com.aop.demo.StudentDao"></property>
<!--采用拦截的名称-->
<property name="interceptorNames" value="myBeforeAdvice"></property>
<property name="optimize" value="true"></property>
</bean>
</beans>
- Test category
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class SpringDemo {
@Resource(name="studentDaoProxy")
private StudentDao studentDao;
@Test
public void demo(){
studentDao.demo1();
studentDao.demo2();
}
}
Use around advice
No interface Case
- CustomerDao 类
public class CustomerDao {
public void demo1() {
System.out.println("查询...");
}
public void demo2() {
System.out.println("保存...");
}
}
- AroundAdvice class
public class AroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("环绕前=======");
/*invocation.proceed()用来执行目标方法*/
Object obj=invocation.proceed();
System.out.println("环绕后===========");
return obj;
}
}
- AplicationContext profile
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="customerDao" class="com.aop.demo.CustomerDao"></bean>
<!--配置通知-->
<bean id="AroudAdive" class="com.aop.demo.AroundAdvice"></bean>
<!--一般的切面使用通知作为切面的,因为要对目标类的某个方法进行增强就需要配置一个带有切入点的切面-->
<bean id="Advisor1" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!--pattern中配置正则表单时:.任意字符,*任意次数-->
<property name="pattern" value=".*"></property>
<property name="advice" ref="AroudAdive"></property>
</bean>
<!--配产生代理-->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标-->
<property name="tar9get" ref="customerDao"></property>
<property name="proxyTargetClass" value="true"></property>
<property name="interceptorNames" value="Advisor1"></property>
</bean>
</beans>
- test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class SpringDemo {
@Resource(name="customerDaoProxy")
private CustomerDao customerDao;
@Test
public void demo1(){
customerDao.demo1();
customerDao.demo2();
}
}
BeanNameAutoProxyCreator automatically create proxy Case (Bean-based automatic agent names, the agent according to generate the name of the target class)
- CustomerDao 类
public class CustomerDao {
public void demo1() {
System.out.println("查询...");
}
public void demo2() {
System.out.println("保存...");
}
}
- StudentDao class
public interface StudentDao {
public void demo1() ;
public void demo2() ;
}
- StudentDaoImpl
public class StudentDaoImpl implements StudentDao {
public void demo1() {
System.out.println("查询...");
}
public void demo2() {
System.out.println("保存...");
}
}
- AroundAdvice class
public class AroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("环绕前=======");
/*invocation.proceed()用来执行目标方法*/
Object obj=invocation.proceed();
System.out.println("环绕后===========");
return obj;
}
}
- BeforeAdvice类
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强==================");
}
}
- AplicationContext profile
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置目标类-->
<bean id="studentDao" class="com.aop.demo.StudentDaoImpl"></bean>
<bean id="customerDao" class="com.aop.demo.CustomerDao"></bean>
<!--配置增强-->
<bean id="BeforeAdvice" class="com.aop.demo.BeforeAdvice"></bean>
<bean id="AroundAdvice" class="com.aop.demo.AroundAdvice"></bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Dao"></property>
<!-- value=""可以更改成AroundAdvice||BeforeAdvice,使用其中一种增强-->
<property name="interceptorNames" value="BeforeAdvice"></property>
</bean>
</beans>
</beans>
- Test category
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class SpringDemo {
@Resource(name = "studentDao")
private StudentDao studentDao;
@Resource(name = "customerDao")
private CustomerDao customerDao;
@Test
public void demo(){
studentDao.demo1();;
studentDao.demo2();
customerDao.demo1();
customerDao.demo2();
}
}
DefaultAdvisorAutoProxyCreator automatically create proxy case (based on section information is automatically created based on the agent or agents Advisor itself contains information)
The code is completely unchanged
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置目标类-->
<bean id="studentDao" class="com.aop.demo.StudentDaoImpl"></bean>
<bean id="customerDao" class="com.aop.demo.CustomerDao"></bean>
<!--配置增强-->
<bean id="myBeforeAdvice" class="com.aop.demo.MyBeforeAdvice"></bean>
<bean id="myAroundAdvice" class="com.aop.demo.MyAroundAdvice"></bean>
<!--配置切面-->
<bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!--value="添加方法"-->
<property name="patterns" value="com\.aop\.demo\.CustomerDao\.demo1,com\.aop\.demo\.StudenDao\.demo2"></property>
<property name="advice" ref="myAroundAdvice"></property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>