Spring Aop Annotation

Posted popcorn丫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Aop Annotation相关的知识,希望对你有一定的参考价值。

实体:

技术分享
 1 package com.bxw.aop.vo;
 2 
 3 public class User {
 4     private String loginId;
 5     
 6     public User() {
 7     }
 8     
 9     public String getLoginId() {
10         return loginId;
11     }
12 
13     public void setLoginId(String loginId) {
14         this.loginId = loginId;
15     }
16     
17 }
User.java

dao:

技术分享
1 package com.bxw.aop.dao;
2 
3 public interface UserDao {
4     public void login(String id);
5 }
UserDao.java
技术分享
 1 package com.bxw.aop.daoimpl;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 import com.bxw.aop.dao.UserDao;
 6 @Repository("userDao")
 7 public class UserDaoImpl implements UserDao{
 8 
 9     public void login(String id) {
10         System.out.println("-----logining------"+id);
11     }
12 }
UserDaoImpl

service:

技术分享
1 package com.bxw.aop.service;
2 
3 public interface UserService {
4     public void login(String id);
5 }
UserService.java
技术分享
 1 package com.bxw.aop.serviceimpl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Qualifier;
 5 import org.springframework.stereotype.Service;
 6 
 7 import com.bxw.aop.dao.UserDao;
 8 import com.bxw.aop.service.UserService;
 9 @Service("userService")
10 public class UserServiceImpl implements UserService{
11     private UserDao userdao;
12     
13     public UserDao getUserdao() {
14         return userdao;
15     }
16     @Autowired
17     public void setUserdao(@Qualifier(value="userDao") UserDao userdao) {
18         this.userdao = userdao;
19     }
20 
21     public void login(String id) {
22         userdao.login(id);
23     }
24 
25 }
UserServiceImpl.java

技术分享
 1 package com.bxw.aop.serviceimpl;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.beans.factory.annotation.Qualifier;
 7 import org.springframework.stereotype.Service;
 8 
 9 import com.bxw.aop.dao.UserDao;
10 import com.bxw.aop.service.UserService;
11 @Service("userService")
12 public class UserServiceImpl implements UserService{
13     private UserDao userdao;
14     
15     public UserDao getUserdao() {
16         return userdao;
17     }
18     @Resource(name="userDao")
19     public void setUserdao(UserDao userdao) {
20         this.userdao = userdao;
21     }
22 
23     public void login(String id) {
24         userdao.login(id);
25     }
26 
27 }
UserServiceImpl.java

两份代码只是UserDao注入时,注解不同

interceptor:

技术分享
 1 package com.bxw.aop.interceptor;
 2 
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Aspect
 9 @Component
10 public class UserInterceptor {
11     @Before("execution(public void com.bxw.aop.dao.UserDao.login(String))")
12     public void loginBefore(){
13         System.out.println("loginStart");
14     }
15     @After("execution(public void com.bxw.aop.dao.UserDao.login(String))")
16     public void loginAfter(){
17         System.out.println("loginOver");
18     }
19 }
UserInterceptor.java

织入点语法:

@Before("execution(public void com.bxw.aop.dao.UserDao.login(String))")

Test:

技术分享
 1 package com.bxw.aop.Test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.bxw.aop.service.UserService;
 7 import com.bxw.aop.vo.User;
 8 
 9 public class Test {
10     public static void main(String[] args) {
11         User user = new User();
12         user.setLoginId("Bao");
13         
14         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
15         UserService userService = (UserService) ac.getBean("userService");
16         userService.login(user.getLoginId());
17     }
18 }
View Code

 

以上是关于Spring Aop Annotation的主要内容,如果未能解决你的问题,请参考以下文章

Spring AOP:@annotation(注释)

Spring Aop Annotation

spring aop annotation

spring aop

基于 Annotation 的 Spring AOP 权限验证方法的实现

spring-3-AOP