具体文件组成
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--组件注解解析器-->
<context:annotation-config></context:annotation-config>
<!--扫描包-->
<context:component-scan base-package="AOPregis"></context:component-scan>
<!--AOP自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
employee类
package AOPregis.domain;
public class employee {
}
employeedao接口
package AOPregis.dao;
import AOPregis.domain.employee;
public interface employeedao {
//保存方法
void save(employee emp);
//更新方法
void update(employee emp);
}
employeedaoimpl实现类
package AOPregis.dao.impl;
import AOPregis.dao.employeedao;
import AOPregis.domain.employee;
import org.springframework.stereotype.Repository;
@Repository("employeedao")
public class employeedaoimpl implements employeedao {
@Override
public void save(employee emp) {
System.out.println("我们从来不唱爱国调。非但不唱,还不爱听。但我们不愿意逃跑,不愿意去父母之邦,撇不开自家人。我国是国耻重重的弱国,跑出去仰人鼻息做二等公民,我们不愿意。我们是文化人,爱祖国的文化爱祖国的文字和语言。一句话,我们是倔强的中国老百姓。");
}
@Override
public void update(employee emp) {
System.out.println("人寿几何,顽铁能炼成的精金,能有多少?但不同程度的锻炼,必有不同程度的成绩;不同程度的纵欲放肆,必积下不同程度的顽劣。");
}
}
employeeservice接口
package AOPregis.service;
import AOPregis.domain.employee;
public interface employeeservice {
//保存方法
void save(employee emp);
//更新方法
void update(employee emp);
}
employeeserviceimpl实现类
package AOPregis.service.impl;
import AOPregis.dao.employeedao;
import AOPregis.domain.employee;
import AOPregis.service.employeeservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service("employeeservice")
public class employeeserviceimpl implements employeeservice {
@Autowired
private employeedao dao;
@Override
public void save(employee emp) {
dao.save(emp);
}
@Override
public void update(employee emp) {
dao.update(emp);
}
}
txmanager事务管理类
package AOPregis.transtion;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("txmanager")
@Aspect//what:做什么增强<aop:aspect ref="txmanager"/>
public class txmanager {
//切入点语法
// <!--在哪里切入,切入的位置,where:在哪些包下的哪些类中的哪些方法进行增强操作-->
//<aop:pointcut id="appointcut" expression="execution(* AOPregis.service.*service.*(..))"></aop:pointcut>
@Pointcut("execution(* AOPregis.service.*service.*(..))")
//<aop:pointcut 的id为方法名
public void appointcut(){}
//前置增强:<aop:before method="begin" pointcut-ref="asppointcut"></aop:before>
@Before("appointcut()")
public void begin(){
System.out.println("懒惰也是天生的,勤奋需自己努力,一放松就懒了。");
}
//后置增强:<aop:after-returning method="commit" pointcut-ref="asppointcut"></aop:after-returning>
@AfterReturning("appointcut()")
public void commit(){
System.out.println("丑人照镜子,总看不到自己多么丑,只看到别人所看不到的美。");
}
//异常增强:<aop:after-throwing method="rollback" pointcut-ref="asppointcut"></aop:after-throwing>
@AfterThrowing("appointcut()")
public void rollback(){
System.out.println("灵魂的美恶,不体现在肉体上。");
}
//最终增强:<aop:after method="close" pointcut-ref="asppointcut"></aop:after>
@After("appointcut()")
public void close(){
System.out.println("个人有所不足,就要自欺欺人。一句谎言说过三次就自己也信以为真的。这个世界好比一座大熔炉,烧炼出一批又一批品质不同而且和原先的品质也不相同的灵魂。");
}
//环绕增强:<aop:around method="circle" pointcut-ref="asppointcut"></aop:around>
@Around("appointcut()")
public Object around(ProceedingJoinPoint pro){
System.out.println("懒惰也是天生的,勤奋需自己努力,一放松就懒了。");
Object obj=null;
try {
obj=pro.proceed();
System.out.println("丑人照镜子,总看不到自己多么丑,只看到别人所看不到的美。");
}catch (Throwable e){
System.out.println("灵魂的美恶,不体现在肉体上。");
}finally {
System.out.println("个人有所不足,就要自欺欺人。一句谎言说过三次就自己也信以为真的。这个世界好比一座大熔炉,烧炼出一批又一批品质不同而且和原先的品质也不相同的灵魂。");
}
return obj;
}
}
test测试方法
package AOPregis.test;
import AOPregis.domain.employee;
import AOPregis.service.employeeservice;
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.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private employeeservice service;
@Test
public void testsave(){
service.save(new employee());
}
@Test
public void testupdate(){
service.update(new employee());
}
}
测试结果为:
总的来说:配置方面上使用xml配置的比较多,这种方法了解一下就好