spring framework面向切面编程示例(xml配置):spring-aop
2017-08-06 00:25
671 查看
示例代码:http://download.csdn.net/detail/u010476739/9922458
工具:spring framework 4.2.4、maven
使用xml配置实现spring-aop
工具:spring framework 4.2.4、maven
使用xml配置实现spring-aop
1.pom依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jackletter</groupId> <artifactId>springaopxml</artifactId> <version>0.0.1-SNAPSHOT</version> <name>this is name</name> <description>this is desc</description> <properties> <spring.version>4.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Aop --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-aop/4.3.4.RELEASE --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> </dependencies> </project>
2.spring配置文件(applicationContext.xml)
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="person" class="bean.Person" /> <bean id="dog" class="bean.Dog" /> <bean id="cat" class="bean.Cat" /> <aop:config> <aop:aspect ref="person"> <aop:pointcut expression="execution(* bean.Animate.show())" id="show"/> <!-- <aop:before method="showBefore" pointcut="execution(* bean.Animate.show())"/> --> <aop:before method="showBefore" pointcut-ref="show"/> <aop:after-returning method="showAfter" pointcut-ref="show"/> <aop:after-throwing method="showThrow" pointcut-ref="show"/> <aop:around method="showArround" pointcut-ref="show"/> </aop:aspect> </aop:config> </beans>
3.代码
Animate
package bean; public interface Animate { public String show() throws Exception; }
Cat
package bean; public class Cat implements Animate { public String show() { return "i'm cat"; } }
Dog
package bean; public class Dog implements Animate{ public String show() throws Exception { // throw new Exception("ko"); return "i'm dog"; } }
Person
package bean; import org.aspectj.lang.ProceedingJoinPoint; public class Person { public void showBefore(){ System.out.println("before run..."); } public void showAfter(){ System.out.println("after run..."); } public Object showArround(ProceedingJoinPoint point) throws Throwable{ System.out.println("arround start..."); Object obj=point.proceed(); System.out.println("arround end..."); return obj; } public void showThrow(){ System.out.println("throw run..."); } }
App
package springaopxml; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import bean.Animate; public class App { public static void main(String[] args) throws Exception { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Animate ani=(Animate) context.getBean("dog"); System.out.print(ani.show()); } }
相关文章推荐
- Spring之旅、装配Bean、最小化Spring XML配置、面向切面的Spring、征服数据库、事务管理示例源代码
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- 深入探索spring技术内幕(七): 配置Spring AOP面向切面编程
- Spring aop 简单示例——基于XML配置和基于注解配置
- 配置Spring AOP面向切面编程
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- spring-06 之AOP 面向切面编程二 基于注解和基于xml配置文件的aop操作
- spring framework面向切面编程示例(基于注解):spring-aop
- Spring基础、IOC(控制反转)、AOP(面向切面编程)、Log4j、注解配置
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- Spring AOP(3)基于XML配置实现的示例
- Spring基础、IOC(控制反转)、AOP(面向切面编程)、Log4j、注解配置
- 深入探索spring技术内幕(七): 配置Spring AOP面向切面编程
- 《Spring In action》学习笔记——AOP(面向切面编程)
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
- Spring Aop(七)——基于XML配置的Spring Aop
- spring(五)---aspectj aop编程(xml配置)
- Spring Aop实例之xml配置
- Intellij IDEA创建Spring项目之基于Aspectj的AOP操作(XML配置方式)
- Spring之AOP(面向切面编程)