Spring AOP (Annotation) to achieve the principle of simulation

2010-04-03  来源:本站原创  分类:Java  人气:180 

Simulation requires weaving behavior: log

package com.spring;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/ / Need to achieve InvocationHandler Interface
public class LogInterceptor implements InvocationHandler (
/ / Proxy object
private Object target;

public void beforeMethod (Method m) (
System.out.println (m.getName () + "start logger !!!!!");
)
public Object invoke (Object proxy, Method method, Object [] args)
throws Throwable (
this.beforeMethod (method);
method.invoke (target, args);
return null;
)
public Object getTarget () (
return target;
)
public void setTarget (Object target) (
this.target = target;
)

)

Test Class:

package com.test;

import java.lang.reflect.Proxy;

import com.dao.UserDao;
import com.dao.UserDaoImpl;
import com.model.User;
import com.spring.LogInterceptor;

public class InterceptorTest (

public static void main (String [] args) (
UserDao userDao = new UserDaoImpl ();
LogInterceptor log = new LogInterceptor ();
log.setTarget (userDao); / / set the proxy object

/ / Create a proxy object UserDao
UserDao userDaoProxy = (UserDao) Proxy.newProxyInstance (UserDao.class.getClassLoader (), new Class [] (UserDao.class), log);
System.out.println (userDaoProxy.getClass ());// userDaoProxy proxy object is a UserDao
userDaoProxy.save (new User ());
userDaoProxy.delete (new User ());
)

)

相关文章
  • Spring AOP (Annotation) to achieve the principle of simulation 2010-04-03

    Simulation requires weaving behavior: log package com.spring; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; / / Need to achieve InvocationHandler Interface public class LogInterceptor implements InvocationHandler ( / /

  • Spring AOP applications to achieve several ways (reprint) 2010-12-28

    Reserved Address: http://blog.csdn.net/mawujun1234/archive/2008/04/22/2314190.aspx spring aop application development in three ways: A: Spring 1.2 release achieved by ProxyFactoryBean aop, that is achieved through dynamic agents, Aspect must inherit

  • Spring AOP (Annotation) use 2010-04-03

    Configure spring configuration file: for the spring to join tagging, automated assembly and production of agency support <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = " http://www.springframework.org/schema/b

  • Spring AOP in the four kinds of ways to achieve 2010-09-19

    AOP first understand the relevant terms: 1. Notice (Advice): Notification section defines what and when to use. Section describes the work to be completed and when the need to perform the job. 2. Connection point (Joinpoint): Procedures can be used t

  • Reflective Dynamic AOP proxy mode (Spring AOP implementation of principle) 2009-03-15

    Reflective Dynamic AOP proxy mode (Spring AOP implementation of principle) Spring has not been used a long time. Suddenly picked up the book. I have found myself on the AOP are not familiar with. In fact, the meaning of AOP is the aspect-oriented pro

  • Using xml and annotation to achieve a similar spring dependency injection and automatic scanning function of class 2010-06-02

    As we all know, spring dependency injection can be achieved in two ways xml and annotation, but also provides automatic scanning function of the class, thereby greatly simplifying the development. Today nothing to do, also achieved a similar function

  • Spring AOP implementations of four commonly used 2010-12-09

    Spring AOP's four ways to achieve AOP-related terms to understand: 1. Notice (Advice): What is the definition of notification of the section and when to use. Section describes the work to be done and when to perform the job. 2. Connection point (Join

  • SPRING AOP concepts and examples of model resolution 2011-01-01

    SPRING AOP Analysis of concepts and examples of model First understand the relevant terms AOP : 1. Notice (Advice): What is the definition of notification of the section and when to use. Describes the section of work to be done and when to perform th

  • spring AOP 2010-03-29

    A, AOP concepts Joinpoint: It defines where to join your logic function, for the Spring AOP, Jointpoint refers to the Method. Advice: run a specific Jointpoint at the code for the Spring AOP is concerned, there is Before advice, AfterreturningAdvice,

  • Spring AOP Learning Summary 2009-02-17

    1, AOP in terms l aspect (Aspect): a modular concern, this concern may be crosscutting multiple objects. J2EE application transaction management is a crosscutting concern about a good example of this. In Spring AOP, the aspect you can use the generic

  • Today in the debug mistakes spring aop program summary 2010-04-11

    Today in the debug spring aop program, initially thought that context: component-scan part is scanned @ Component, so the base-page configured as @ Component of the package, how the results are also not get the results, the official forums to see peo

  • Aspect-oriented use of Spring (AOP) program 2010-07-22

    1.aop theory Cross-cutting concerns: on which methods to intercept, block after the how to deal with these concerns would call cross-cutting concerns. Aspect (section): cross-cutting concerns that the abstract shall section, with categories similar t

  • Spring Framework's design concept and design patterns (7)-Spring AOP features and implementation in 2010-08-31

    Keywords: Spring, AOP, Proxy, proxy, blocker the last chapter: Spring Framework's design concept and design patterns (6)-Ioc container and BeanFactory factory http://javapub.javaeye.com/blog/751635 Next Chapter: Spring Framework's design concept and

  • Summary of Spring AOP 2010-09-29

    Spring In conclusion AOP AOP(Aspect Oriented Programming) Aspect-oriented programming, in short, to achieve cross-section (crossing) Tool. In many parts of a program re-use of a piece of logic that we can be pulled out as an aspect , Reconstruction a

  • Spring AOP reference manual 2010-09-29

    Spring AOP reference manual Overview Spring 2.0 Introduces a simple but powerful mechanism-based models and @AspectJ Note implement custom aspects of the style. Spring currently supports only method execution join point does not support field interce

  • Spring AOP II study notes-Springle 2010-09-30

    Spring AOP II In the first generation of Spring AOP, AOP's Advice to achieve a specific interface, and configuration settings rely on the tedious XML settings. In Spring2.0 after the implementation of the AOP, and setting new two methods: one is base

  • Detailed features in Spring AOP 2010-11-02

    Implementation principle of dynamic agents To find Spring's AOP is necessary to understand the principles of dynamic agents, because the dynamic proxy-based AOP is implemented. Dynamic proxies have to start with from the JDK itself. Jdk the java.lang

  • Spring aop Discussion 2010-03-29

    This article made reference to the royzhou1985 Bo-wen, would like to thank him for his selfless sharing. 1 What is the aop? I understand it, aop Let us concentrate on doing what we are most concerned about functionality, but to many other classes nee

  • Chapter 5 Spring AOP: Spring's aspect-oriented programming 2010-03-29

    Chapter 5 Spring AOP: Spring's aspect-oriented programming 5.1. The concept of aspect-oriented programming (AOP) provides another perspective from the program structure in order to improve the object-oriented programming (OOP). Object-oriented will b

  • Using spring AOP and annotations to resolve logs 2010-03-31

    First, create a custom annotation @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface RecordLog { } Then uses spring Aop @Component("logAround") public class LogAroundInterceptor implements MethodInterceptor { @Au