个人技术分享

一、demo

 1、spring项目

(1)pom

<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.13.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.13.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.13.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.10</version>
		</dependency>

		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.10</version>
		</dependency>



		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>

(2)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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="com.demo"></context:component-scan>
	
	<aop:aspectj-autoproxy />
</beans>

(3)业务代码 

@Service
public class EmployeeService {
	
   public int addEmployee(Employee emp) {
      System.out.println("添加员工成功");
      return 0;
   }
   public int delEmployee(Employee emp) {
      System.out.println("删除员工成功");
      return 0;
   }
   public int updateEmployee(Employee emp) {
      System.out.println("修改员工成功");
      return 0;
   }
   public Employee findEmployee(String empno) {
      System.out.println("查询员工成功");
      return new Employee();
   }
}
@Service
public class PersonService{
	
	public void save(String name) {
        System.out.println("我是save()方法");
    }

    public void update(String name, Integer id) {
        System.out.println("我是update()方法");
    }

    public String getPersonName(Integer id) {
        System.out.println("我是getPersonName()方法");
        return "xxx";
    }

}

 (4)AOP:

@Aspect
@Component
public class MyInterceptor {
    @Pointcut("execution (* com.demo.service.PersonService.add*(..))")
    private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称
    
    @Pointcut("execution (* com.demo.service.EmployeeService.update*(..))")
    private void anotherMethod() {}
    
    // 声明该方法是一个前置通知:在目标方法开始之前执行 
    @Before("anyMethod()")
    public void doAccessCheck() {
        System.out.println("前置通知");
    }
    
    @After(value = "anyMethod()")
    public void closeResource() {
    	System.out.println("关闭数据库连接");
    }
    
    @Before("anotherMethod()")
    public void openSession() {
    	System.out.println("开启session");
    }
}

(5)测试:

public class SpringAOPTest {
	@Test
	public void interceptorTest() {
		ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmployeeService employeeService = (EmployeeService) cxt.getBean("employeeService");
//		personService.save("xxx");
//		personService.update("zhangsan", 1);
//		personService.getPersonName(1);
		
		employeeService.updateEmployee(new Employee());
	}
}

2、springboot项目