Spring AOP two kinds of injection method

2011-09-02  来源:本站原创  分类:Industry  人气:116 

The first is annotation-based approach into

(1) defines a Aspect Object LogBeforeAdvice

package org.aop;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@ Aspect
public class LogBeforeAdvice {
private Logger logger = Logger.getLogger (this.getClass (). getName ());
@ Pointcut ("execution (* org.aop.IHello .*(..))")
private void logging () {}

@ Before ("logging ()")
public void before (JoinPoint joinPoint) {
logger.log (Level.INFO, "method start ..." + joinPoint.getSignature (). getDeclaringTypeName () + "." + joinPoint.getSignature (). getName ());
}

@ AfterReturning (pointcut = "logging ()", returning = "retVal")
public void afterReturning (JoinPoint joinPoint, Object retVal) {
logger.log (Level.INFO, "method end ..." + joinPoint.getSignature (). getDeclaringTypeName () + "." + joinPoint.getSignature (). getName ());
}
}

(2) defines an interface class IHello

package org.aop;

public interface IHello {
public void hello (String hello);
}
(3) define an implementation class HelloSpeaker

package org.aop;

public class HelloSpeaker implements IHello {

public void hello (String hello) {
System.out.println ("Hello!" + Hello);
}

}
(4) Spring configuration file configuration

<? 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: aop = "http://www.springframework.org/schema/aop"
xsi: schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ">

<bean> </ bean>
<bean> </ bean>
<aop:aspectj-autoproxy> </ aop: aspectj-autoproxy>
</ Beans>

Finally, write test classes

package org.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {

/ **
* @ Param args
* /
public static void main (String [] args) {
ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml");
IHello hello = (IHello) context.getBean ("helloSpeaker");
hello.hello ("Sheng respect");
}

}
Test results

log4j: WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j: WARN Please initialize the log4j system properly.
2011-4-26 21:32:37 org.aop.LogBeforeAdvice before
Information: method start ... org.aop.IHello.hello
Hello! Sheng respect
2011-4-26 21:32:37 org.aop.LogBeforeAdvice afterReturning
Information: method end ... org.aop.IHello.hello

The second approach is based on XML to register

(1) modify the Aspect class

package org.aop;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;

public class LogBeforeAdvice {
private Logger logger = Logger.getLogger (this.getClass (). getName ());

public void before (JoinPoint joinPoint) {
logger.log (Level.INFO, "method start ..."
+ JoinPoint.getSignature (). GetDeclaringTypeName () + "."
+ JoinPoint.getSignature (). GetName ());
}

public void afterReturning (JoinPoint joinPoint) {
logger.log (Level.INFO, "method end ..."
+ JoinPoint.getSignature (). GetDeclaringTypeName () + "."
+ JoinPoint.getSignature (). GetName ());
}
}

(2) modify the configuration file

<aop:config>
<aop:aspect ref="logBeforeAdvice">
<aop:pointcut expression="execution(* org.aop.IHello.*(**))"/>
<Aop: before
pointcut-ref = "helloLog"
method = "before"
/>
<Aop: after-returning
pointcut-ref = "helloLog"
method = "afterReturning"
/>
</ Aop: aspect>
</ Aop: config>

Execution result is the same

相关文章
  • Spring AOP two kinds of injection method 2011-09-02

    The first is annotation-based approach into (1) defines a Aspect Object LogBeforeAdvice package org.aop; import java.util.logging.Level; import java.util.logging.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterRetur

  • Spring into three kinds of injection method IOC 2008-12-30

    Type1 interface injection We often with interface to the caller and implementation of those separated. Such as: public class ClassA ( private InterfaceB clzB; public doSomething () ( Ojbect obj = Class.forName (Config.BImplementation). NewInstance ()

  • 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 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

  • Spring's bean injection method 2010-04-21

    On the bean injection method, spring provides three methods: 1. Constructor injection 2.setter injection 3. Method Injection (omitted) Under normal circumstances, prior to use in two ways. Based on the DI by calling the constructor with a parameter o

  • Spring Source Code Analysis (5): Spring AOP for Proxy 2008-09-28

    Let us look at some of Spring's AOP-related code is how to get Proxy, let us first take a look at AOP and Spring AOP with some basic concepts: Advice: Notices, set the connection point of what to do, in the Sping, he will describe Spring around the m

  • 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

  • Spring AOP underlying implementation technology 2009-02-16

    Software programming language ultimate goal is to use more natural and more flexible way to simulate the world, from the original machine language to the process of language and then to object-oriented languages, we see programming language in a step

  • JAVA using Spring AOP with annotations for the system to increase logging 2010-04-20

    Spring Spring AOP has always been a more distinctive features, it can use existing code in any place, we would like to embed the logic function, and do not need to change our existing code structure. In view of this, the present system has completed

  • 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 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

  • 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

  • Spring AOP Study Notes 2010-04-22

    Spring AOP study notes need to clear a few concepts: notice (Advice): used to inform the system will be What are the new behavior. Entry point (Pointcut): defines the notification should be applied to those connection points. The target object (Targe

  • Spring AOP using the configuration description (3): automatically creates a proxy 2010-04-28

    The basis of (a), (b) the method described in the configuration aspect has been learned, but they are configured to use org.springframework.aop.framework.ProxyFactoryBean agent, the method can only be for a single class configuration agent. It is not

  • 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

  • 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

  • Some Thoughts on the spring aop 2008-06-01

    In considering the recent data flow compression problem, because do not want to change the present procedure, so choose to use to achieve these springaop deal. Through access to relevant information, types include "around", "before" an

  • Spring source code analysis (7): Spring AOP interceptors invoked against the realization of 2009-09-23

    In front of our analysis of the Spring AOP implementation Proxy objects to get the process, let's take a look at the Spring AOP interceptor chain is what is called, that is how the Proxy pattern is at work, or how Spring provides us with AOP function

  • Spring AOP (1): JDK dynamic proxy 2010-04-17

    Disclaimer: This article is based on the "master Spring2.x" the classic example. To find Spring AOP, design patterns proposed in the first familiarize yourself with the agent model (not based on programming language agent model, the key is to un