具体文件组成
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>
<!--配置AOP:1切面,what 2切入位置,where,即aspectj语言 3切入时机:when-->
<aop:config>
<!--做什么增强即what,这里来源于事务管理器,增强事务管理方法-->
<aop:aspect ref="tx">
<!--在哪里切入,切入的位置,where:在哪些包下的哪些类中的哪些方法进行增强操作-->
<aop:pointcut id="asppointcut" expression="execution(* AOP.service.*service.*(..))"></aop:pointcut>
<!--在什么时候做增强操作,在进行service的方法前还是后,即when-->
<!--在执行service方法前进行:前置加强-->
<aop:before method="begin" pointcut-ref="asppointcut"></aop:before>
<!--后置加强-->
<aop:after-returning method="commit" pointcut-ref="asppointcut"></aop:after-returning>
<!--异常加强-->
<aop:after-throwing method="rollback" pointcut-ref="asppointcut"></aop:after-throwing>
</aop:aspect>
</aop:config>
<!-- 配置事务管理器 -->
<bean id="tx" class="AOP.tranction.tranctionmanager"></bean>
<!--配置dao-->
<bean id="employeedao" class="AOP.dao.impl.employeedaoimpl"></bean>
<!--配置service-->
<bean id="service" class="AOP.service.impl.employeeserviceimpl">
<property name="dao" ref="employeedao"></property>
</bean>
</beans>
employee类
package AOP.domain;
public class employee {
}
employeedao接口
package AOP.dao;
import AOP.domain.employee;
public interface employeedao {
//保存方法
void save(employee emp);
//更新方法
void update(employee emp);
}
employeedaoimpl实现类
package AOP.dao.impl;
import AOP.dao.employeedao;
import AOP.domain.employee;
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 AOP.service;
import AOP.domain.employee;
public interface employeeservice {
//保存方法
void save(employee emp);
//更新方法
void update(employee emp);
}
employeeserviceimpl实现类
package AOP.service.impl;
import AOP.dao.employeedao;
import AOP.domain.employee;
import AOP.service.employeeservice;
public class employeeserviceimpl implements employeeservice {
private employeedao dao;
public void setDao(employeedao dao) {
this.dao = dao;
}
@Override
public void save(employee emp) {
dao.save(emp);
}
@Override
public void update(employee emp) {
dao.update(emp);
}
}
tranction事务管理类
package AOP.tranction;
public class tranctionmanager {
public void begin(){
System.out.println(" 唯有身处卑微的人,最有机缘看到世态人情的真相");
}
public void commit(){
System.out.println(" 我只怕人不如书好对付。他们会看不起你,欺负你,或者就嫉妒你,或者又欺负又嫉妒。…");
}
public void rollback(){
System.out.println(" 据说,朋友的友情往往建立在互相误解的基础上。恋爱大概也是如此。");
}
}
test测试方法
package AOP.test;
import AOP.domain.employee;
import AOP.service.employeeservice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation。自动装配;
import org.springframework.beans.factory.annotation。资格赛;
import org.springframework.test.context。ContextConfiguration ;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith (SpringJUnit4ClassRunner .class)
@ContextConfiguration (“classpath:applicationContext.xml”)
public class test{
@Autowired
@Qualifier (“服务”)
private AOP.service.employeeservice service ;
@测试
public void testsave(){
service .save(new employee());
}
@测试
public void testupdate(){
service .update(new employee());
}
}
测试结果如下