Spring AOP log management and Spring AOP log

Source: Internet
Author: User

Spring AOP log management and Spring AOP log
Introduction to AOP

AOP (Aspect-OrientedProgramming, Aspect-Oriented Programming)

Several Concepts of AOP

1. Aspect (Aspect): Aspect is a modular focus, such as transaction management, log management, and permission management;

2. Joinpoint: the execution of a method in Spring;

3. Advice: a notification is an operation performed on a connection point of a plane, that is, transaction management and log management;

4. Pointcut: The Pointcut describes a type of selected connection points, that is, the method that specifies a type of notifications to be woven;

5. Target: the Target object dynamically proxies by AOP;

Here we use the logon function for log management.

Summary

1) LoginService LogService TestMain

2) use Spring to manage LoginService and LogService objects

3) determine which connection points are the starting points. In the configuration file

4) encapsulate LogService as notification

5) implant the notification into the cut-in

6) client call target point

Step 1 create a login service interface and implementation class
Package com. aaron. log;/*** @ Author Aaron * @ Date Creation Time: 2016-1-13 * @ Version 1.0 ** @ Project_Package_Description SpringQuartzDemo | com. aaron. log * @ Function_Description login business logic */public interface ILoginService {public boolean login (String name, String password );}

 

Package com. aaron. log;/*** @ Author Aaron * @ Date Creation Time: 2016-1-13 * @ Version 1.0 ** @ Project_Package_Description SpringQuartzDemo | com. aaron. log * @ Function_Description login business logic interface implementation class **/public class LoginServiceImpl implements ILoginService {public boolean login (String userName, String password) {System. out. println ("User Logon Information:" + userName + "," + password); return true ;}}
Step 2 create a log management interface and implementation class

 

Package com. aaron. log; import org. aspectj. lang. joinPoint;/*** @ Author Aaron * @ Date Creation Time: * @ Version 1.0 ** @ Project_Package_Description SpringQuartzDemo | com. aaron. log * @ Function_Description Log interface **/public interface ILogService {// The log method without parameters public void beforeLog (); // The Log method with parameters public void log (JoinPoint point ); // public void afterLog (JoinPoint point, Object returnObj );}
Package com. aaron. log; import org. aspectj. lang. joinPoint;/*** @ Author Aaron * @ Date Creation Time: * @ Version 1.0 ** @ Project_Package_Description SpringQuartzDemo | com. aaron. log * @ Function_Description the logging interface implementation class **/public class LogServiceImpl implements ILogService {// public void beforeLog () {System. out. println *******************") ;} // The public void Log (JoinPoint point) method with no return value {// This method returns an array containing the request, ActionCofig, and other class objects [] args = point. getArgs (); System. out. print ("target parameter list:"); for (Object obj: args) {System. out. print (obj + ",");} System. out. println () ;}// public void afterLog (JoinPoint point, Object returnObj) method with parameters and return values {// This method returns an array, the array includes the request, ActionCofig, and other class objects. Object [] args = point. getArgs (); System. out. print ("target parameter list:"); for (Object obj: args) {System. out. print (obj + ",");} System. out. println (); System. out. println ("execution result:" + returnObj); System. out. println *******************") ;}}
Step 3 configure aop in the applicationContext. xml file <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.s Pringframework.org/schema/aop/spring-aop-2.5.xsd "> <! -- Start trigger configuration start --> <bean name = "startQuertz" lazy-init = "false" autowire = "no" class = "org. springframework. scheduling. quartz. schedulerFactoryBean "> <property name =" triggers "> <list> <ref bean =" myJobTrigger "/> </list> </property> </bean> <! -- The trigger configuration ends --> <! -- Scheduling configuration starts --> <! -- Quartz-1.8 previous configuration <bean id = "myJobTrigger" class = "org. springframework. scheduling. quartz. cronTriggerBean "> <property name =" jobDetail "> <ref bean =" myJobDetail "/> </property> <property name =" cronExpression "> <value> 0/1 ****? </Value> </property> </bean> --> <! -- Quartz-2.x configuration --> <bean id = "myJobTrigger" class = "org. springframework. scheduling. quartz. cronTriggerFactoryBean "> <property name =" jobDetail "> <ref bean =" myJobDetail "/> </property> <property name =" cronExpression "> <value> 0 0/1 ***? </Value> </property> </bean> <! -- Scheduling configuration ended --> <! -- Start job configuration --> <bean id = "myJobDetail" class = "org. springframework. scheduling. quartz. methodInvokingJobDetailFactoryBean "> <property name =" targetObject "> <ref bean =" myJob "/> </property> <property name =" targetMethod "> <value> work </value> </property> </bean> <! -- Job configuration ends --> <! -- Working bean --> <bean id = "myJob" class = "com. tgb. lk. demo. quartz. myJob "/> <bean id =" logService "class =" com. aaron. log. logServiceImpl "> </bean> <bean id =" loginService "class =" com. aaron. log. loginServiceImpl "> </bean> <aop: config> <! -- Entry point --> <aop: pointcut expression = "execution (* com. aaron. log. LoginServiceImpl. * (..)" id = "myPointcut"/> <! -- Aspect: the method of the object to which the image is imported and the starting point to which the image is woven --> <aop: aspect id = "dd" ref = "logService"> <! -- Pre-notification --> <aop: before method = "beforeLog" pointcut-ref = "myPointcut"/> <aop: after method = "Log" pointcut-ref = "myPointcut"/> <aop: after-returning method = "afterLog" returning = "returnObj" pointcut-ref = "myPointcut"/> </aop: aspect> </aop: config> </beans>ApplicationContext. xmlStep4 Test Method
Package com. aaron. log; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext;/*** @ Author Aaron * @ Date Creation Time: 2016-1-13 * @ Version 1.0 ** @ Project_Package_Description SpringQuartzDemo | com. aaron. log * @ Function_Description test **/public class TestMain {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("classpath *: config/spring/applicationContext. xml "); ILoginService loginService = (ILoginService) ctx. getBean ("loginService"); loginService. login ("aaron", "123456 ");}}
Step 5 run. Check the background

 

PS: Using annotations can refer to the blog http://blog.csdn.net/oathevil/article/details/7288867 written by others

 

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.