Cainiao learns Spring -- get the parameter value and method name by using JoinPoint for 60 s

Source: Internet
Author: User

Cainiao learns Spring -- get the parameter value and method name by using JoinPoint for 60 s

I. Overview
The implementation method of AOP has already used two methods in the previous two blogs to implement the current problem. Although we use AOP, how does the client transmit information? The JoinPoint interface is used to transmit parameters from the client to a specific implementation class.
Ii. Code demonstration.

Directory structure:


SecurityHandler. java

package com.tgb.spring;import org.aspectj.lang.JoinPoint;public class SecurityHandler{private void checkSecurity(JoinPoint joinPoint){for (int i = 0; i < joinPoint.getArgs().length; i++) {System.out.println(joinPoint.getArgs()[i]);}System.out.println(joinPoint.getSignature().getName());System.out.println("=====checkSecurity====");}}


Client. java

Package com. tgb. spring; import org. springframework. beans. factory. beanFactory; import org. springframework. context. support. classPathXmlApplicationContext; import com. tgb. spring. userManager; public class Client {public static void main (String [] args) {BeanFactory factory = new ClassPathXmlApplicationContext ("applicationContext. xml "); UserManager userManager = (UserManager) factory. getBean ("userManager"); userManager. addUser ("Zhang San", "123"); // userManager. delUser (1 );}}


UserManager. java

package com.tgb.spring;public interface UserManager {public void addUser(String username,String password);public void delUser(int userId);public String findUserById(int userId);public void modifyUser(int userId,String username,String password);}


UserManagerImpl. java

Package com. tgb. spring; public class UserManagerImpl implements UserManager {public void addUser (String username, String password) {// checkSecurity (); System. out. println ("= UserManager. addUser = ");} public void delUser (int userId) {// checkSecurity (); System. out. println ("= UserManager. delUser = ");} public String findUserById (int userId) {// checkSecurity (); System. out. println ("= UserManager. findUserById = "); return" Zhang San ";} public void modifyUser (int userId, String username, String password) {// checkSecurity (); System. out. println ("= UserManager. modifyUser = ");} // private void checkSecurity () {// System. out. println ("checkSecurity ");////}}


ApplicationContext. xml

   
   
    
     
   


:


Iii. Summary.

We can add a JoinPoint parameter in Advice. This value is automatically passed in by spring and can be obtained from JoinPoint.

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.