AOP——Annotation配置
1.实现AOP的Annotation配置,需要在配置文件中,配置如下:<aop:aspectj-autoproxy />,但是在配置文件中还需要加入aop的命名空间。
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
2.Aspectj是专门用于产生代理的框架
3.小实验如下:
a) 定义一个日志逻辑LogInterceptor
package com.zgy.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
//织入点语法,指定从何处织入
@Before("execution(public void com.zgy.impl.UserDAOImpl.save(com.zgy.model.User))")
public void before(){
System.out.println("method start ...");
}
}
b) 定义UserDAO
package com.zgy.dao;
import com.zgy.model.User;
public interface UserDAO {
public void save(User u);
}
c) 定义UserDAOImpl
package com.zgy.impl;
import org.springframework.stereotype.Component;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
@Component("u")
public class UserDAOImpl implements UserDAO {
public void save(User u) {
System.out.println("user saved");
}
}
d) 定义UserService
package com.zgy.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO;
public void init(){
System.out.println("init");
}
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destory(){
System.out.println("destroy");
}
}
e) 定义配置文件bean.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.zgy"/>
<aop:aspectj-autoproxy />
</beans>
f) 测试:
package com.zgy.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
service.add(new User());
}
}
g) 运行结果:
method start ...
user saved
4)相关概念:
A、JoinPoint:连接点。指定在哪些连接点上织入切面逻辑
B、Pointcut:joinpoint的集合,带有一个名字
C、Aspect:切面,加入的切面逻辑就是切面,即此处的LogInterceptor
D、Advice:简单的理解为方法或所加入的逻辑出的简易,即此处的@Before
E、Target:被代理对象
F、Weaver:织入。
5)如果一个类实现了接口,那么将由JDK产生代理。而Spring中的类可以不实现接口,所以也此时如果要产生代理,就要用到CGLIB这个jar包,通过直接操纵二进制码的形式产生代理。