一。配置服务类组件
@Component
public class UserService {
private UserDAO userDAO;
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="userDAOImpl")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
@Resource自动装配
二。配置applicationContext.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
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">
<!-- 配置自动装配 AutowiredAnnotationBeanPostProcessor等接口,实现注入等功能 -->
<context:annotation-config/>
<!-- 配置扫描包路径 -->
<context:component-scan base-package="com.job"/>
<!-- 配置aop自动代理 -->
<aop:aspectj-autoproxy/>
</beans>
三。编写切面类
@Aspect
@Component
public class LogInteceptor {
@Before("execution(public void com.job.dao.UserDAOImpl.save(com.job.model.User))")
public void beforeMethod(){
System.out.println("Method start!");
}
@Pointcut("execution (public * com.job.dao..*.save(..))")
public void method(){
};
@Before("method()")
public void beforeMethod(){
System.out.println("Method start!");
}
@Around("method()")
public void around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("around before!");
pjp.proceed();
System.out.println("around after");
}
}
注:首先一定要注册为组件@Component
其次注册为切面
@Before("execution(public void com.job.dao.UserDAOImpl.save(com.job.model.User))")
@Before表示调用方法前执行。
execution(public void com.job.dao.UserDAOImpl.save(com.job.model.User))表示执行特定的类名.方法名方法
"execution (public * com.job.dao..*.save(..))"
..表示包含子包
*表示任何类
四。测试
public class UserServiceTest {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService)ac.getBean("userService");
User u = new User();
u.setUsername("me haha ");
service.add(u);
}
}
五。测试结果
Method start!
me haha ---haha!
分享到:
相关推荐
spring-aop实践项目,基于annotation方式的面向切面实践小项目
NULL 博文链接:https://ysj5125094.iteye.com/blog/2055563
spring框架的主要知识点,含ioc,aop,annotation等关键值时点
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
NULL 博文链接:https://tianhei.iteye.com/blog/978969
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
│ Maven__org_springframework_spring_aop_5_2_5_RELEASE.xml │ Maven__org_springframework_spring_beans_5_2_5_RELEASE.xml │ Maven__org_springframework_spring_context_5_2_5_RELEASE.xml │ Maven__org_...
课程内容 面向接口(抽象)编程的概念与好处...Spring应用AOP(重要) xml annotation Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要) opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
14. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 15. 16. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 17. <bean class="org....
顾名思义是基于spring和memcached的一个插件,该插件目前实现了memcached的查询缓存,利用java的annotation方式和spring的AOP切面编程使得我们在业务层获得缓存数据非常的方便 ##开始使用 首先你必须实现CacheSupport...
aop入门。
spring-aop-4.0.4.RELEASE.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.1.2.RELEASE.jar ...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
自己动手做的一个spring aop 的小demo,主要是annotation标签的配置联系,比较简单.