JDK and Cglib Two Dynamic proxy mode difference
1, java dynamic proxy class is generated anonymous implement a proxy interface using reflection, InvokeHandler calls before calling a specific method to process. The dynamic proxy Cglib asm using open source packages, class files proxy object classes loaded in, processed by modifying a subclass bytecode.
2, JDK dynamic proxy can generate a proxy class that implements the interface, but not for the class; Cglib agent is achieved for the class, the main class is specified to generate a sub-class, a method wherein the cover, because it is inherited, so that class or method is best not to be declared as final.
3, Cglib a target class method generates two proxy method, a method of rewriting goal, and to achieve a proxy logic, there is a direct call to the target class method.
The principle of AOP
For this principle can be seen [Tutorial] Detailed Spring AOP realization of the principle of (dynamic proxy) , then we talk directly through examples how to use.
Introducing AOP dependence
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
AOP configuration
- Spring-based AOP wording
spring.aop.auto=true # 是否启用aop
spring.aop.proxy-target-class=false # 代理方式有接口使用jdk动态代理,如果没有接口使用cglib代理;设置为true时表示强制使用cglib代理
- Based SpringBoot approach @EnableAopProxyClass
exposeProxy属性表示如果使用true就可以使用AopContext对象获取当前代理对象,false则不能使用
proxyTargetClass true表示使用jdk的动态代理, false表示使用cglib代理
Example a (Cglib dynamic proxy, by default)
1、BookDaoImpl
@Repository
public class BookDaoImpl implements BookDao {
@Override
public void addBook(String name, String author) {
System.out.println("BookDaoImpl实现类:bookName:"+name+", bookAuthor:"+author);
}
}
2、BookAop
@Component
@Aspect
public class BookAop {
// 定义切入点
public static final String POINT_CUT = "execution(* com.example.bootaop.dao..*.*(..))";
@Before(POINT_CUT)
public void before() {
System.out.println("----------添加图书方法前[校验]-----------");
}
@After(POINT_CUT)
public void after(JoinPoint jp) {
System.out.println("----------添加图书成功后-----------");
System.out.println(jp.getTarget().getClass());
System.out.println(Arrays.asList(jp.getArgs()));
}
}
3、BootAopApplication
@SpringBootApplication
public class BootAopApplication {
public static void main(String[] args) {
ConfigurableApplicationContext
context = SpringApplication.run(BootAopApplication.class, args);
BookDaoImpl bookDao = context.getBean(BookDaoImpl.class);
System.out.println(bookDao.getClass());
bookDao.addBook("神雕侠侣", "金庸");
context.close();
}
}
Console output
class com.example.bootaop.dao.BookDaoImpl$$EnhancerBySpringCGLIB$$2d857802
----------添加图书方法前[校验]-----------
BookDaoImpl实现类:bookName:神雕侠侣, bookAuthor:金庸
----------添加图书成功后-----------
class com.example.bootaop.dao.BookDaoImpl
[神雕侠侣, 金庸]
Example Two (JDK dynamic proxy, only generates a proxy class that implements the interface for)
1, BookDao
@Repository
public class BookDaoImpl1 implements BookDao {
@Override
public void addBook(String name, String author) {
}
}
2、BookDaoImpl1
@Repository
public class BookDaoImpl1 implements BookDao {
@Override
public void addBook(String name, String author) {
}
}
3、BootAopApplication
@SpringBootApplication
public class BootAopApplication {
public static void main(String[] args) {
ConfigurableApplicationContext
context = SpringApplication.run(BootAopApplication.class, args);
BookDao bookDao = context.getBean("bookDaoImpl1", BookDao.class);//多个实现类时
System.out.println(bookDao.getClass());
bookDao.addBook("鹿鼎记", "金庸");
context.close();
}
}
Profiles increase: spring.aop.proxy-target-class = false
Console output
class com.sun.proxy.$Proxy61
----------添加图书方法前[校验]-----------
----------添加图书成功后-----------
class com.example.bootaop.dao.BookDaoImpl1
[鹿鼎记, 金庸]