使用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="h ...
使用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>pom.xml
建立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 }User.java
建立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 @Component 11 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 }UserService.java
建立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.java
建立實現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 @Override 12 public void save(User user) { 13 System.out.println("user saved!"); 14 } 15 16 }UserDAOImpl.java
建立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 }LogInterceptor.java
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/aop 10 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>beans.xml
測試代碼(使用的是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 @Test 11 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 }UserServiceTest.java
控制台輸出:
class com.dzg.spring.service.UserService$$EnhancerBySpringCGLIB$$21c652e7 Method Around Start! Method start! user saved! Method Around Complete! Method Complete!