• 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
  • 相关阅读:
    python的os模块命令
    python的os模块
    albert1017 Linux下压缩某个文件夹(文件夹打包)
    装饰器加不加()
    json和jsonp解决跨域传输数据等
    wsgi
    flask建立数据模型数据类型
    爬虫中
    javaweb学习总结(四)——Http协议
    JavaWeb学习总结(三)——Tomcat服务器学习和使用(二)
  • 原文地址:https://www.cnblogs.com/popcornya/p/6918635.html
Copyright © 2020-2023  润新知