Spring configuration file Way Implementation aop_spring configuration file AOC

Source: Internet
Author: User
Tags aop log4j

Spring Catalog File Overview:


List of jar packages required:


After the jar is imported, you will need to build path, select the jar right key build path-"Add Build Path"

First create Persion.java

Package Com.hpe.po;

public class Person {
	//requirement, the
	Print method ends public

	void Eat ()

		after the Print method starts//prints eating only after the eating is hit System.out.println ("Eat ...");
	}

	public void Drunk () {

		System.out.println ("Drunk ...");
	}
	
	public void Drunkbear () {

		System.out.println ("Drunkbear ...");
	}

	public void Play () {

		System.out.println ("Play ...");
	}


Then create the enhanced class: Myadvice.java

Package Com.hpe.po;

Import Org.aspectj.lang.ProceedingJoinPoint;

public class Myadvice {public

	void before () {
		System.out.println ("method Start");
	}

	public void End () {
		System.out.println ("method ended");
	}

	public void Final1 () {
		System.out.println ("Method End");
	}

	public void Around (Proceedingjoinpoint joinpoint) throws Throwable {
		System.out.println ("Method Bigin");
		Joinpoint.proceed ();
		System.out.println ("Method End");
	}


Configure Log4j.xml

# Global Logging Configuration
log4j.rootlogger=debug, stdout
# Console output ...
Log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.appender.stdout.layout= Org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionpattern=%5p [%t]-%m%n

Last configuration file:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:util = "Http://www.springframework.org/schema/util" xsi:schemalocation= "Http://www.springframework.org/schema/beans HT Tp://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/util http://www . springframework.org/schema/util/spring-util.xsd Http://www.springframework.org/schema/context Http://www.spring Framework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://
	Www.springframework.org/schema/aop/spring-aop.xsd "> <!--XML Way to implement AOP--> <!--parameterless construction method Bean Management--> <bean id= "Person" class= "Com.hpe.po.Person" ></bean> <bEan id= "Myadvice" class= "Com.hpe.po.MyAdvice" ></bean> <!--AOP configuration--> <aop:config> <!--configuration Pointcut ID: Identity expression: Expression execution (method modifier, method return value method name (parameter)) return value method Name parameter cannot be less * represents the point at which an arbitrary pointcut is executed in the execution of a expression expression, or the moment that the method is executed, because the
			Before and after that point we have to cut to the strengthening method--> <aop:pointcut expression= "Execution (* com.hpe.po.Person.eat (..))"
			Id= "Pointcut1"/> <aop:pointcut expression= "Execution (* com.hpe.po.person.drunk* (..))"
			Id= "Pointcut2"/> <aop:pointcut expression= "Execution (* com.hpe.po.Person.play (..))" Id= "Pointcut3"/> <!--configuration slice: will be enhanced (front-back). The process applied to the Pointcut--> <!--ref= "Myadvice" is the management class file that we use for the writing enhancement method of bean management--> <aop:aspect ref= "Myadvice" > <!--be Fore: After execution--> <aop:before method= "before" pointcut-ref= "Pointcut1"/> <!--execution order--> <aop:after 
			Method= "End" pointcut-ref= "pointcut1"/> <aop:after-returning method= "final1" pointcut-ref= "Pointcut2"/> <aop:around method= "Around" pointcut-ref= "Pointcut3"/> </aop:aspect> </aop:config> </beans> 

Write Test class:

Package com.hpe.test;

Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;


Import Com.hpe.po.Person;


public class Aoptest {
	@Test public
	void Test1 () {
		
		ApplicationContext context=new Classpathxmlapplicationcontext ("Applicationcontext.xml");
		Persion p= (persion) Context.getbean ("Persion1");
		Person person = Context.getbean ("person", person.class);
		Person.eat ();
		Person.drunk ();
	}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.