演示的spring版本为4.2.4
首先配置文件中引入名称空间:
<?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 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">
引入jar包:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.4.RELEASE</version> </dependency>
仍然是之前的案例代码:
IUserService:
package com.itheima.service; public interface IUserService { void save(); }
UserServiceImpl:
package com.itheima.service.impl; import com.itheima.dao.IUserDao; import com.itheima.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements IUserService { @Autowired private IUserDao userDao; public void save() { System.out.println(this.getClass().getName()+" 执行..."); userDao.save(); } }
IUserDao:
package com.itheima.dao; public interface IUserDao { void save(); }
UserDaoImpl:
package com.itheima.dao.impl; import com.itheima.dao.IUserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements IUserDao { @Override public void save() { System.out.println(this.getClass().getName()+" 执行..."); System.out.println("保存用户..."); } }
test类:
package com.itheima.test; import com.itheima.domain.Address; import com.itheima.service.IUserService; 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; //junit中的注解,表示在SpringJUnit4ClassRunner中运行测试方法 @RunWith(SpringJUnit4ClassRunner.class) //ContextConfiguration指定配置文件,相对路径的applicationContext.xml @ContextConfiguration("classpath:applicationContext.xml") public class AnnotationTest1 { @Autowired//给对象赋值 private IUserService userService; @Test public void test1(){ userService.save(); } }
配置文件:
<?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 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:component-scan base-package="com.itheima"></context:component-scan> <!-- AOP配置 --> <aop:config> <!-- 设置一个切入点:告诉spring框架哪些方法需要增强 --> <aop:pointcut id="userDaoImpl_save" expression="execution(* com.itheima.dao.impl.UserDaoImpl.save(..))"></aop:pointcut> <!-- 设置一个切面:告诉spring框架调用哪个切面类的哪个方法--> <aop:aspect ref="myAspectXml"> <!-- before表示在userDaoImpl_save中的save方法前面切一刀,加入myAspectXml中的writeLog --> <aop:before pointcut-ref="userDaoImpl_save" method="writeLog"></aop:before> </aop:aspect> <aop:aspect ref="myAspectXml"> <!-- after表示在userDaoImpl_save中的save方法后面切一刀,加入myAspectXml中的writeLog2 --> <aop:after method="writeLog2" pointcut-ref="userDaoImpl_save"></aop:after> </aop:aspect> </aop:config> </beans>
切面类:
package com.itheima.util; import org.springframework.stereotype.Component; @Component public class MyAspectXml { public void writeLog(){ System.out.println("开始写日志...."); } public void writeLog2(){ System.out.println("写日志结束...."); } }
执行test1方法,结果:
将applicationContext.xml中的配置注释,再运行:
在原来的类与测试类都不变的情况下,修改配置文件与添加切面类,可以改变原来执行的方法。
下一篇会讲aop配置文件中更多的配置