首页 > 代码库 > Spring Annotation注解进行aop的学习
Spring Annotation注解进行aop的学习
使用Maven管理项目,pom文件为:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.dzg</groupId> 5 <artifactId>spring_study</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <properties> 8 <maven.compiler.source>1.8</maven.compiler.source> 9 <maven.compiler.target>1.8</maven.compiler.target>10 <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>11 </properties>12 13 <dependencies>14 <dependency>15 <groupId>org.springframework</groupId>16 <artifactId>spring-context</artifactId>17 <version>4.3.2.RELEASE</version>18 </dependency>19 <!-- https://mvnrepository.com/artifact/junit/junit -->20 <dependency>21 <groupId>junit</groupId>22 <artifactId>junit</artifactId>23 <version>4.12</version>24 </dependency>25 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->26 <dependency>27 <groupId>org.aspectj</groupId>28 <artifactId>aspectjrt</artifactId>29 <version>1.8.9</version>30 </dependency>31 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->32 <dependency>33 <groupId>org.aspectj</groupId>34 <artifactId>aspectjweaver</artifactId>35 <version>1.8.9</version>36 </dependency>37 <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->38 <dependency>39 <groupId>aopalliance</groupId>40 <artifactId>aopalliance</artifactId>41 <version>1.0</version>42 </dependency>43 44 </dependencies>45 </project>
建立User类:
1 package com.dzg.spring.model; 2 3 public class User { 4 private int id; 5 private String password; 6 private String username; 7 public int getId() { 8 return id; 9 }10 public String getPassword() {11 return password;12 }13 public String getUsername() {14 return username;15 }16 public void setId(int id) {17 this.id = id;18 }19 public void setPassword(String password) {20 this.password = password;21 }22 public void setUsername(String username) {23 this.username = username;24 }25 }
建立UserService类:
1 package com.dzg.spring.service; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Component; 6 7 import com.dzg.spring.dao.UserDAO; 8 import com.dzg.spring.model.User; 9 10 @Component11 public class UserService {12 13 private UserDAO userDAO;14 15 public void add(User user) {16 userDAO.save(user);17 }18 19 public UserDAO getUserDAO() {20 return userDAO;21 }22 23 public UserService() {24 25 }26 27 @Resource(name = "u")28 public void setUserDAO(UserDAO userDAO) {29 this.userDAO = userDAO;30 }31 32 }
建立UserDAO接口类:
1 package com.dzg.spring.dao;2 3 import com.dzg.spring.model.User;4 5 public interface UserDAO {6 public void save(User user);7 }
建立实现UserDAO接口的实体类UserDAOImpl类:
1 package com.dzg.spring.dao.impl; 2 3 import org.springframework.stereotype.Component; 4 5 import com.dzg.spring.dao.UserDAO; 6 import com.dzg.spring.model.User; 7 8 @Component("u") 9 public class UserDAOImpl implements UserDAO {10 11 @Override12 public void save(User user) {13 System.out.println("user saved!");14 }15 16 }
建立LogInterceptor类:
1 package com.dzg.spring.aop; 2 3 import org.aspectj.lang.annotation.Aspect; 4 import org.aspectj.lang.annotation.Before; 5 import org.springframework.stereotype.Component; 6 7 @Aspect 8 @Component("log") 9 public class LogInterceptor {10 11 @Before("execution(public void com.dzg.spring.dao.impl.UserDAOImpl.save(com.dzg.spring.model.User))")12 public void beforeMethod() {13 System.out.println("save start!");14 }15 16 }
beans.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/aop10 http://www.springframework.org/schema/aop/spring-aop.xsd">11 12 <context:annotation-config></context:annotation-config>13 <context:component-scan base-package="com.dzg.spring"></context:component-scan>14 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>15 <!-- <bean id="userDAO" class="com.dzg.spring.dao.impl.UserDAOImpl"></bean> 16 <bean id="logInterceptor" class="com.dzg.spring.aop.LogInterceptor"></bean> 17 <bean id="userService" class="com.dzg.spring.service.UserService"> <property 18 name="UserDAO" ref="userDAO" /> </bean> -->19 </beans>
测试代码(使用的是junit测试)
1 package com.dzg.spring.service; 2 3 import org.junit.Test; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.dzg.spring.model.User; 7 8 public class UserServiceTest { 9 10 @Test11 public void testAdd() throws Exception {12 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");13 14 UserService service = (UserService) ctx.getBean("userService");15 System.out.println(service.getClass());16 User u = new User();17 u.setUsername("zhangsan");18 u.setPassword("zhangsan");19 service.add(u);20 ctx.close();21 }22 23 }
控制台输出:
class com.dzg.spring.service.UserService$$EnhancerBySpringCGLIB$$21c652e7Method Around Start!Method start!user saved!Method Around Complete!Method Complete!
Spring Annotation注解进行aop的学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。