本文采用全注解方式进行spring的配置。
基于maven项目的pom.xml配置文件
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
bean
Employee.java
package com.bean;
public class Employee {
}
dao
EmployeeDao.java
package com.dao;
import com.bean.Employee;
public interface EmployeeDao {
public void save(Employee employee);
public void update(Employee employee);
}
dao实现类:
EmployeeDaoImpl.java
package com.dao;
import org.springframework.stereotype.Repository;
import com.bean.Employee;
@Repository("employeeDao")//用注解方式,需加此注解
public class EmployeeDaoImpl implements EmployeeDao {
@Override
public void save(Employee employee) {
System.out.println("保存员工");
}
@Override
public void update(Employee employee) {
System.out.println("修改员工");
}
}
service
EmployeeService.java
package com.service;
import com.bean.Employee;
public interface EmployeeService {
public void save(Employee emp);
public void update(Employee emp);
}
service实现类:
EmployeeServiceImpl.java
package com.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.bean.Employee;
import com.dao.EmployeeDao;
import com.service.EmployeeService;
@Service("employeeService")//此注解也为必须
public class EmployeeServiceImpl implements EmployeeService {
@Resource(name="employeeDao")
private EmployeeDao dao;
@Override
public void save(Employee emp) {
dao.save(emp);
}
@Override
public void update(Employee emp) {
dao.update(emp);
throw new RuntimeException("出错了。");
}
}
动态代理:
TransactionManager.java
package com.proxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
// 模拟事务管理器
// <bean id="aspect" class="com.proxy.TransactionManager" />
@Component // 加了此注解后,就相当于在Spring中配置了一个bean对象
@Aspect // 表示TransactionManager类是一个切面类
public class TransactionManager {
// 配置连接点
// <aop:pointcut expression="execution(* com.hpeu.service..*.*(..))" id="pt"/>
@Pointcut("execution(* com.service..*.*(..))")
public void pt() {
}
//@Before("pt()") // 前置增强
//@Before("execution(* com.service..*.*(..))")
public void begin() {
System.out.println("开启事务");
}
//@AfterReturning("pt()") // 后置增强
//@AfterReturning("execution(* com.service..*.*(..))")
public void commit() {
System.out.println("提交事务");
}
//@After("pt()") // 最终增强
public void close() {
System.out.println("释放资源");
}
//@AfterThrowing(value="pt()", throwing="ex") // 异常增强
public void rollback(Throwable ex) {
System.out.println("回滚事务," + ex.getMessage());
}
// 环绕增强方法
@Around("pt()")
public Object around(ProceedingJoinPoint pjp) {
Object proceed = null;
begin();
try {
/*
System.out.println("代理对象:" + pjp.getThis().getClass());
System.out.println("目标对象:" + pjp.getTarget().getClass());
System.out.println("被增强方法参数:" + Arrays.toString(pjp.getArgs()));
System.out.println("当前连接点签名:" + pjp.getSignature());
System.out.println("当前连接点类型:" + pjp.getKind());
*/
// 核心方法
proceed = pjp.proceed();
commit();
} catch (Throwable e) {
rollback(e);
} finally {
close();
}
return proceed;
}
}
spring配置类:
beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- DI注解解析器 -->
<context:annotation-config />
<!-- IoC注解解析器 -->
<context:component-scan base-package="com" />
<!-- AOP注解解析器 -->
<aop:aspectj-autoproxy />
</beans>
测试类:
AOPTest.java
package com.test.dyni.proxy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.bean.Employee;
import com.service.EmployeeService;
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class AOPTest {
@Autowired
private EmployeeService service;
@Test
public void testSave() {
service.save(new Employee());
}
@Test
public void testUpdate() {
service.update(new Employee());
}
}