spring aop Annotation Configuration Code Example
- 2021-07-13 05:28:40
- OfStack
In this article, we share the specific code of spring aop annotation configuration for your reference. The specific contents are as follows
Demo.java
package cn.itcast.e_annotation;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.bean.User;
import cn.itcast.service.ISUserService;
@RunWith(SpringJUnit4ClassRunner.class)// Help us create the container
// Specify which configuration file to use when creating the container
@ContextConfiguration("classpath:cn/itcast/e_annotation/applicationContext.xml")
public class Demo {
/*
* @Test public void fun1() { //1 Create a container object ClassPathXmlApplicationContext ac=new
* ClassPathXmlApplicationContext("applicationContext.xml"); //2 Ask the container for "want" user Object
* User u=(User)ac.getBean("user"); User u2=(User)ac.getBean("user");
*
* System.out.println(u==u2); //3 Print user Object System.out.println(u);
*
* ac.close(); }
*/
@Resource(name="userServiceTarget")
private ISUserService us;
@Test
public void fun1() {
us.save();
}
}
applicationContext.xml
<?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 ">
<!-- Preparation: Import aop (Constraint) Namespace -->
<!-- 1. Configure the target object -->
<bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean>
<!-- 2. Configure notification objects -->
<bean name="myAdvice" class="cn.itcast.e_annotation.MyAdvice"></bean>
<!-- 3. Open the use of annotations to complete the implantation -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
MyAdvice.java
package cn.itcast.e_annotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
// Notification class
@Aspect// When representing this class 1 Notification classes
public class MyAdvice {
// Pre-notification - "Called before the target method runs
// Post notification (not called if an exception occurs) - "Called after the target method runs
// Surround notification - "Called before and after the target method
// Exception interception notification - "If an exception occurs, a call is made to the
// Post notification (called regardless of exception) - "Called after the target method runs
@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void pc() {}
// Pre-notification
@Before("MyAdvice.pc()")// Specifies that the method is a pre-tangent point
public void before() {
System.out.println(" This is a pre-notification ");
}
// Post notification
public void afterReturning() {
System.out.println(" This is a post notification (it will not be called if an exception occurs!!) ");
}
// Surround notification
public Object around( ProceedingJoinPoint pjp) throws Throwable {
System.out.println(" This is the part before wrapping around the notification ");
Object procees=pjp.proceed();// Invoke the target method
System.out.println(" This is the part after the wrap notification ");
return procees;
}
// Exception notification
public void afterException() {
System.out.println(" Something happened, something unusual happened ");
}
// Post notification
public void after() {
System.out.println(" This is a post notification (called if an exception occurs) ");
}
}