SpringAop log management
Spring AOP
1.Understanding of AOP-the idea of division of labor for each part and integration during runtime
2.Understanding process-oriented, object-oriented, and Aspect-oriented ideas
1)Process-oriented: when the room is decorated, prepare a lamp and pull a wire to connect the lamp.
2)Object-oriented: design the locations in the room where the wire interface needs to be used, and then set the wire interface at the corresponding location for future use.
3)Face-to-Face: When decorating a house, first design the areas where the wire interface needs to be installed. Then, set the wire interface and do not open the interface. This is the connection point, when the electrical cutting is required, open the interface and the plug-in is the starting point.
Aspect: function (LOGIN log)
Objectives: Major Aspects (LOGIN)
Aspect: Integration of entry point implant notification
Connection point: where secondary functions (logs) can be inserted
Entry Point: To insert a secondary function
Notification: encapsulate objects of sub-functions
Insert: insert notification into the entry point
3.Implement login and log management (using Spring AOP)
1)LoginService LogService TestMain
2)Use Spring to manage LoginService and LogService objects
3)Determine which connection points are the starting point. In the configuration file
4)Encapsulate LogService as notification
5)Implant notifications to the entry point
6)Client call Target
- Aspect id = "dd" ref = "logService">
-
Execution (** cn.com. spring. service. impl .*.*(..))
1) * All Modifiers
2) * all return types
3) * all class names
4) * All method names
5) *. All parameter names
1. ILoginService. java
- Package cn.com. spring. service;
- Public interface ILoginService {
- Public boolean login (String userName, String password );
- } 2. LoginServiceImpl. java
- Package cn.com. spring. service. impl;
- Import cn.com. spring. service. ILoginService;
- Public class LoginServiceImpl implements ILoginService {
- Public boolean login (String userName, String password ){
- System. out. println ("login:" + userName + "," + password );
- Return true;
- }
- }
3. ILogService. java
- Package cn.com. spring. service;
- Import org. aspectj. lang. JoinPoint;
- Public interface ILogService {
- // LOG method without Parameters
- Public void log ();
- // LOG method with Parameters
- Public void logArg (JoinPoint point );
- // Method with parameters and returned values
- Public void logArgAndReturn (JoinPoint point, Object returnObj );
- }
4. LogServiceImpl. java
- Package cn.com. spring. service. impl;
- Import org. aspectj. lang. JoinPoint;
- Import cn.com. spring. service. ILogService;
- Public class LogServiceImpl implements ILogService {
- @ Override
- Public void log (){
- System. out. println ("*************** Log *******************");
- }
- // Method with parameters without return values
- Public void logArg (JoinPoint point ){
- // This method returns an array containing the request, ActionCofig, and other class objects.
- Object [] args = point. getArgs ();
- System. out. println ("target parameter list :");
- If (args! = Null ){
- For (Object obj: args ){
- System. out. println (obj + ",");
- }
- System. out. println ();
- }
- }
- // Method with parameters and return values
- Public void logArgAndReturn (JoinPoint point, Object returnObj ){
- // This method returns an array containing the request, ActionCofig, and other class objects.
- Object [] args = point. getArgs ();
- System. out. println ("target parameter list :");
- If (args! = Null ){
- For (Object obj: args ){
- System. out. println (obj + ",");
- }
- System. out. println ();
- System. out. println ("execution result:" + returnObj );
- }
- }
- } 5. applicationContext. java
- 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.springframework.org/schema/aop/spring-aop-2.5.xsd>
- Expression = "execution (* cn.com. spring. service. impl. LoginServiceImpl .*(..))"
- Id = "myPointcut"/>
- 6. TestMain. java
- Public class TestMain {
- Public static void testSpringAOP (){
- ApplicationContext ctx = new ClassPathXmlApplicationContext ("app *. xml ");
- ILoginService loginService = (ILoginService) ctx. getBean ("loginService ");
- LoginService. login ("zhangsan", "12344 ");
- }
- Public static void main (String [] args ){
- TestSpringAOP ();
- }
- } 7. output result:
- Log in: zhangsan, 12344
- Target parameter list:
- Zhangsan,
- 12344,
- The execution result is: true resolution: 1. the login () method System. out. println ("login:" + userName + "," + password) is called first );
2. Then, the logArgAndReturn () method is called to output logs, and whether the login () method is successful is returned.
- System. out. println ("target parameter list :");
- If (args! = Null ){
- For (Object obj: args ){
- System. out. println (obj + ",");
- }
- System. out. println ();
- System. out. println ("execution result:" + returnObj );
- }