本文主要是介绍springAOP 学习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
maven引入
<properties><org.springframework.version>4.3.7.RELEASE</org.springframework.version></properties>
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${org.springframework.version}</version></dependency>
xml配置:
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><context:component-scan base-package="spring.aop"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
要实现的方法 忽略,
切入的方法:
@Aspect
@Component
public class LoggingAspect {@Before("execution(public int spring.aop.jiajian.jia())")public void beforeMethod(JoinPoint joinPoint){System.out.println("方法前打印:"+joinPoint.getSignature().getName());}public void afterMethod(){System.out.println("方法后打印");}
}
测试:
public static void main(String[] args) {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("bean-genne.xml");jiajian jj =applicationContext.getBean(jiajian.class);int result =jj.jia();System.out.println(result);}
这篇关于springAOP 学习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!