This article demonstrates the implementation of AOP using AspectJ annotation and XML configuration in Spring
The following is the Java Project that implements AOP using AspectJ annotations
First is the applicationContext.xml file located under the classpath
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Enable AspectJ support for Annotation --> <aop:aspectj-autoproxy/> <bean id="userManager"/> <bean id="securityHandler"/> </beans>
Then there is the service layer interface and implementation class
package com.jadyer.annotation; 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); } /** * The above UserManager is the interface of the service layer* The following UserManagerImpl is the implementation class of the service layer interface*/ package com.jadyer.annotation; public class UserManagerImpl implements UserManager { public void addUser(String username, String password) { System.out.println("-------UserManagerImpl.addUser() is invoked-----"); } public void delUser(int userId) { System.out.println("--------UserManagerImpl.delUser() is invoked------"); } public String findUserById(int userId) { System.out.println("------UserManagerImpl.findUserById() is invoked-----"); return "Iron-faced life"; } public void modifyUser(int userId, String username, String password) { System.out.println("-------UserManagerImpl.modifyUser() is invoked------"); } } Next is the entry class marked with AspectJ
package com.jadyer.annotation; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class SecurityHandler { /** * Define Pointcut * @see Pointcut The name of Pointcut is addAddMethod(). This method has no return value and parameters* @see This method is an identifier and does not call it */ @Pointcut("execution(* add*(..))") //Match all methods starting with add private void addAddMethod(){}; /** * Define Advice * @see Indicates which Pointcut subscriptions our Advice applies to on Joinpoints*/ //@Before("addAddMethod()") @After("addAddMethod()") private void checkSecurity() { System.out.println("------【checkSecurity is invoked】------"); } } Finally, the client test class
package com.jadyer.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring's support for AOP: adopts the Annotation method* @see -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- But there is a prerequisite: since it is a JDK dynamic proxy, if you want to generate a proxy, the class must implement an interface* @see If the class does not have an implements interface and still uses Spring's default AOP implementation, an error will occur* @see The classes that usually need to generate a proxy are all service-level classes, so an interface will usually be drawn out. That is, develop the habit of interface-oriented programming* @see ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Added Annotation support * @see * SPRING_HOME//dist//spring.jar * @see * SPRING_HOME//lib//log4j//log4j-1.2.14.jar * @see * SPRING_HOME//lib//jakarta-commons//commons-logging.jar * @see * SPRING_HOME//lib//aspectj//*.jar * @see 2. Modularize the cross-slicing concerns and establish SecurityHandler.java * @see 3. Use annotations to specify SecurityHandler as Aspect * @see 4. Use annotations to define Advice and Pointcut * @see 5. Enable AspectJ support for Annotation, and configure the target class and Aspect class into the IoC container* @see 6. Development client* @see --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (UserManager)factory.getBean("userManager"); userManager.addUser("Zhang Qiling", "02200059"); } } The following is the Java Project that implements AOP using XML configuration files
First is the applicationContext-cglib.xml file located in the root directory of src
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Force use of CGLIB proxy --> <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> --> <bean id="userManager"/> <bean id="securityHandler"/> <aop:config> <aop:aspect id="securityAspect" ref="securityHandler"> <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/> </aop:aspect> </aop:config> </beans> <!-- Match all methods starting with add(* add*(..)) Match all methods of all classes under com.jadyer.servcices.impl package execution(* com.jadyer.servcices.impl.*.*(..)) Match all methods of add in com.jadyer.servcices.impl or del execution(* com.jadyer.services.impl.*.add*(..)) || execution(* com.jadyer.services.impl.*.del*(..)) -->
Then there is the service layer interface and implementation class
package com.jadyer.cglib; 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); } /** * The above UserManager is the service layer interface* The following UserManagerImpl is the implementation class of the service layer interface*/ package com.jadyer.cglib; public class UserManagerImpl { //implements UserManager { public void addUser(String username, String password) { System.out.println("-------UserManagerImpl.addUser() is invoked-----"); } public void delUser(int userId) { System.out.println("--------UserManagerImpl.delUser() is invoked-----"); } public String findUserById(int userId) { System.out.println("------UserManagerImpl.findUserById() is invoked-----"); return "Zhang San"; } public void modifyUser(int userId, String username, String password) { System.out.println("-------UserManagerImpl.modifyUser() is invoked-----"); } } Next is the entry class specified in applicationContext-cglib.xml
package com.jadyer.cglib; import org.aspectj.lang.JoinPoint; /** * Pass the client call information to the Advice* @see You can add a JoinPoint parameter to the Advice to obtain the method name and parameter value of the client call* @see In the future, there are fewer cases of purely using AOP to write things like this. We mainly use the transactions provided by Spring* @see You can know about this. The following is the sample code */ public class SecurityHandler { private void checkSecurity(JoinPoint joinPoint) { for (int i=0; i<joinPoint.getArgs().length; i++) { System.out.println(joinPoint.getArgs()[i]); //Get the parameter value of the method called by the client} System.out.println(joinPoint.getSignature().getName()); //Get the method name called by the client System.out.println("------[checkSecurity is invoked]------"); } } Finally, the client test class
package com.jadyer.cglib; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @see ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Since it is inheritance, it is best not to use final declaration for the target class* @see ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Because the targets we proxy are generally business objects* @see ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public class Client { public static void main(String[] args) { ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext-cglib.xml"); //When UserManagerImpl implements the UserManager interface, Spring will automatically use the JDK dynamic proxy//If the project has introduced the cglib library and forced the use of CGLIB proxy in the configuration file, Spring will only use the CGLIB proxy//UserManager userManager = (UserManager)factory.getBean("userManager"); //Because the UserManagerImpl does not implement the UserManager interface at this time, the UserManager interface cannot be used in the receiving type.//The cglib library has been introduced in the project. Although the CGLIB proxy is not forced to be used in the configuration file, Spring will automatically use the CGLIB proxy UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager"); userManager.addUser("Wu Sansheng", "02200059"); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.