使用Maven管理项目,pom文件为:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dzg</groupId>
<artifactId>spring_study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency> </dependencies>
</project>
pom.xml
建立User类:
package com.dzg.spring.model; public class User {
private int id;
private String password;
private String username;
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setId(int id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
User.java
建立UserService类:
package com.dzg.spring.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dzg.spring.dao.UserDAO;
import com.dzg.spring.model.User; @Component
public class UserService { private UserDAO userDAO; public void add(User user) {
userDAO.save(user);
} public UserDAO getUserDAO() {
return userDAO;
} public UserService() { } @Resource(name = "u")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} }
UserService.java
建立UserDAO接口类:
package com.dzg.spring.dao; import com.dzg.spring.model.User; public interface UserDAO {
public void save(User user);
}
UserDAO.java
建立实现UserDAO接口的实体类UserDAOImpl类:
package com.dzg.spring.dao.impl; import org.springframework.stereotype.Component; import com.dzg.spring.dao.UserDAO;
import com.dzg.spring.model.User; @Component("u")
public class UserDAOImpl implements UserDAO { @Override
public void save(User user) {
System.out.println("user saved!");
} }
UserDAOImpl.java
建立LogInterceptor类:
package com.dzg.spring.aop; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Aspect
@Component("log")
public class LogInterceptor { @Before("execution(public void com.dzg.spring.dao.impl.UserDAOImpl.save(com.dzg.spring.model.User))")
public void beforeMethod() {
System.out.println("save start!");
} }
LogInterceptor.java
beans.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"> <context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.dzg.spring"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- <bean id="userDAO" class="com.dzg.spring.dao.impl.UserDAOImpl"></bean>
<bean id="logInterceptor" class="com.dzg.spring.aop.LogInterceptor"></bean>
<bean id="userService" class="com.dzg.spring.service.UserService"> <property
name="UserDAO" ref="userDAO" /> </bean> -->
</beans>
beans.xml
测试代码(使用的是junit测试)
package com.dzg.spring.service; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dzg.spring.model.User; public class UserServiceTest { @Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService) ctx.getBean("userService");
System.out.println(service.getClass());
User u = new User();
u.setUsername("zhangsan");
u.setPassword("zhangsan");
service.add(u);
ctx.close();
} }
UserServiceTest.java
控制台输出:
class com.dzg.spring.service.UserService$$EnhancerBySpringCGLIB$$21c652e7
Method Around Start!
Method start!
user saved!
Method Around Complete!
Method Complete!