1、配置所需要的maven依赖pom.xml文件(这里为spring核心依赖aop依赖和日志依赖)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aaa</groupId>
<artifactId>myaop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
</project>
2、配置文件ApplicationContext.xml文件的配置
<aop:aspectj-autoproxy>是aop使用所需要的,对于下面的datasource和factory是关于数据库的配置,这里没有添加依赖,于本例也没有明显的关系,所以注释不实用。
<?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.aaa"></context:component-scan>
<aop:aspectj-autoproxy />
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@172.16.130.35:1521:orcl"></property>
<property name="username" value="uuid"></property>
<property name="password" value="uuid"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="com.aaa.domain"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean> -->
</beans>
3、编写实体类和对应的service类
Employee类
package com.aaa.domain;
public class Employee {
private int id;
private String name;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
}
EmployeeService类(这里加@Service注解表示在Spring容器中添加一个小写字母的employeeService对象)
package com.aaa.service;
import org.springframework.stereotype.Service;
import com.aaa.domain.Employee;
@Service
public class EmployeeService {
public void addEmployeeSalary(Employee e,double salary){
e.setSalary(salary);
System.out.println("初始化工资");
}
public void updateEmployeeSalary(double salary){
System.out.println("更改工资");
}
public void findEmployeeSalary(double salary){
System.out.println("查询工资");
}
}
4、编写切入类,这里用注解配置(@Aspect标明本类是切面类,@Pointcut声明切点)
package com.aaa.service;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.aaa.domain.Employee;
@Aspect
@Component
public class MyInterceptor {
@Pointcut("execution (* com.aaa.service.EmployeeService.add*(..)) and args(param)")
// @Pointcut( "execution(* com.fishwasser.action.UserAction.*(..)) and args()" )
private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称
@Before("anyMethod()")
public void doAddBefore(){
System.out.println("初始化之前的工资:");
}
@After(value="anyMethod()")
public void doAddAfter(){
System.out.println("初始化之后的工资:");
}
}
5、测试类
package com.aaa.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aaa.domain.Employee;
import com.aaa.service.EmployeeService;
public class EmployeeTest {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeService employeeService = (EmployeeService) cxt.getBean("employeeService");
Employee employee=new Employee();
employeeService.addEmployeeSalary(employee, 3000.0);
}
}
可以看到结果在单独执行add方法的时候,分别执行的切入的before和after方法,说明aop切入成功