AOP implementation based on proxy class Proxyfactorybean---Pre-notification code implementation _ code

Source: Internet
Author: User

Code:

1. configuration file

Applicationcontext.xml

<?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" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd "> <!--configuration Create Userdaoimpl instance--> <bean id=" Userdao "class=" Com.springtest1.dao.UserDAOImpl "> </bean> <!--configuration Create Userbizimpl instance--> <bean id=" userbiz "class=" com . Springtest1.biz.UserBizImpl > <!--Dependency Injection data Access Layer component--> <property name= "Userdao" ref= "Userdao"/> </b Ean> <!--define--> <bean id= "Logadvice" class= "Com.springtest1.aop.LogAdvice" ></bean> <! --Defines a proxy class, named UB, that accesses methods in the business class through UB--> <bean id= "UB" class= "Org.springframework.aop.framework.ProxyFactoryBean" > <property name= "proxyinterfaces" > <value>com.springtest1.biz.userbiz</value> </property> <property name= "Interceptornames" > <list> <value>logadvice</val ue> </list> </property> <property name= "target" ref= "userbiz" ></property> </b Ean> </beans>


2. Writing the business logic layer

Userbiz.java

Package com.springtest1.biz;

Public interface Userbiz {
   //Add user public
	void addUser (String username,string password);
	Delete user public
	void deluser (int id);
}


Userbizimpl.java

Package com.springtest1.biz;

Import Com.springtest1.dao.UserDAO;
Import Com.springtest1.dao.UserDAOImpl;
The public class Userbizimpl implements Userbiz {//////
	with the Userdao interface declares an object
	//and adds a set method for it, which is used for dependency injection
	Userdao Userdao;
	public void Setuserdao (Userdao userdao) {
		This.userdao=userdao;
	}

	@Override public
	void AddUser (string username, string password) {
		//TODO auto-generated method stub
       Userdao.adduser (username, password);
	}

	@Override public
	void deluser (int id) {
		//TODO auto-generated method stub
       userdao.deluser (ID);
       throw new RuntimeException ("This is a specially thrown exception message.) ");
	}

}

3. Write Data Access Layer

Userdao.java

Package Com.springtest1.dao;

Public interface Userdao {
   //Add user public
	void addUser (String username,string password);
	Delete user public
	void deluser (int id);
}

Userdaoimpl.java

Package Com.springtest1.dao;

public class Userdaoimpl implements Userdao {

	@Override public
	void AddUser (string username, string password) {
  //TODO auto-generated Method Stub
       System.out.println (username+ "User add Success");

	@Override public
	void deluser (int id) {
		//TODO auto-generated method stub
       System.out.println ("numbered as" +id+ "The user is deleted");
	}


4. Programming AOP Facets

Logadvice.java

Package COM.SPRINGTEST1.AOP;

Import Java.lang.reflect.Method;
Import Org.springframework.aop.MethodBeforeAdvice;
Import Org.apache.log4j.Logger;

public class Logadvice implements Methodbeforeadvice {

	private Logger Logger=logger.getlogger (logadvice.class);
	public void before (method method, object[] args, Object target) throws Throwable {
		//get the called Class name
		String targetclassn Ame=target.getclass (). GetName ();
		Gets the called method name
		String targetmethodname=method.getname ();
		Log format string string
		loginfotext= "predecessor notification: the" +targetmethodname+ "Method of the +targetclassname+ class begins execution";
		Writes log information to the configured file
		logger.info (loginfotext);
	}

5. Test class

Aoptest.java

Package com.springtest1;

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

Import Com.springtest1.biz.UserBiz;

public class Aoptest {public
	static void Main (string[] args) {
		//TODO auto-generated Method stub
		//Load Applica Tioncontext.xml configuration
		applicationcontext context=new classpathxmlapplicationcontext ("Applicationcontext.xml");
		Gets the Userbizimpl instance in the configuration
		userbiz userbiz = (userbiz) Context.getbean ("UB");	
		Userbiz.adduser ("Zhangsan", "123");
		Userbiz.deluser (1);
	}

}

6. Screenshot



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.