Spring AOP slicing programming before and after method execution in log __

Source: Internet
Author: User
Tags aop xmlns

1. Introduction of relevant JAR files: Aspectjrt.jar, Aspectweaver.jar, Cglib-nodep-2.1_3.jar, Commons-logging.jar, Spring.jar

2. Spring Core configuration file *.xml

<?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:context= "Http://www.springframework.org/schema/context"
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.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<bean id= "Usermanager" class= "Com.jzg.spring.UserManagerImpl"/>
<!--Facets--
<bean id= "Loginterceptor" class= "Com.jzg.spring.LogInterceptor"/>
<aop:config>
<!--first declare a tangent object, referencing <bean id= "Securityhandler" > The Java class pointed to--
<aop:aspect id = "Securityaspect" ref= "Loginterceptor" >
<!--claim stealing rules here is a way to match the parameterless add prefix of any return value--
<!--<aop:pointcut id= "Addallmethod" expression= "Execution (* add* (..))" />--
<!--declaration Cut-in method
<!--in accordance with the Allmethod entry point, the CheckSecurity () method will be executed once all the methods are executed under the spring package.
<!--Execution (* com.jzg.spring.*.* (..)), here are all the ways to match the spring package--
<aop:pointcut id= "Allmethod" expression= "Execution (* com.jzg.spring.*.* (..))" />
<aop:before method= "Mybeforemethod" pointcut-ref= "Allmethod"/>
<aop:after method= "Myaftermethod" pointcut-ref= "Allmethod"/>
</aop:aspect>
</aop:config>
</beans>

3. Related Java Classes

Package com.jzg.spring;
public class Loginterceptor {
 public void Mybeforemethod () {
System.out.println (" Mybeforemethod execution of pre-cut log-----------------");
 }
@SuppressWarnings ("unused")  
         private void Myaftermethod () { 
& nbsp   System.out.println ("Myaftermethod () after the completion of the execution of the log------------");  
 }  
}

Package com.jzg.spring;
public class usermanagerimpl{
public void Deluser (int userId) {
SYSTEM.OUT.PRINTLN ("----usermanagerimpl.deluser ()---");
}
Public String Finduserbyid (int userId) {
SYSTEM.OUT.PRINTLN ("----usermanagerimpl.finduserbyid ()---");
return "Zhangsan";
}
public void ModifyUser (int userId, string userName, string password) {
SYSTEM.OUT.PRINTLN ("----usermanagerimpl.modifyuser ()---");
}
public void AddUser (string userName, string password) {
System.out.println ("Usermanagerimpl.adduser () Add User execution-----");
}
}

Package test;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.jzg.spring.UserManagerImpl;
public class Test {
private static Usermanagerimpl Usermanager;
private static Beanfactory factory;
static{
Factory = new Classpathxmlapplicationcontext ("Beans.xml");
Usermanager = (Usermanagerimpl) factory.getbean ("Usermanager");
}
@org. junit.test
public void Testadd () {
Usermanager.adduser ("Zhang San", "123");
}

@org. junit.test
public void Testdel () {
Usermanager.deluser (0);
}
@org. junit.test
public void Testfind () {
Usermanager.finduserbyid (1);
}
@org. junit.test
public void Testmodify () {
Usermanager.modifyuser (1, "UserName", "password");
}



public static Usermanagerimpl Getusermanager () {
return usermanager;
}
public static void Setusermanager (Usermanagerimpl usermanager) {
Test.usermanager = Usermanager;
}
public static Beanfactory GetFactory () {
return factory;
}
public static void Setfactory (Beanfactory factory) {
Test.factory = Factory;
}
}

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.