spring framework面向切面编程示例(xml配置):spring-aop
来源:互联网 发布:矩阵正规化 编辑:程序博客网 时间:2024/06/05 19:59
示例代码:http://download.csdn.net/detail/u010476739/9922458
工具: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());}}
阅读全文
0 0
- spring framework面向切面编程示例(xml配置):spring-aop
- spring framework面向切面编程示例(基于注解):spring-aop
- Spring AOP( 面向切面编程)配置
- 配置Spring AOP面向切面编程
- Spring配置过程 (二)面向切面编程AOP
- Spring AOP 面向切面编程(一)
- Spring实践:面向切面编程(AOP)
- Spring 的面向切面编程(AOP)
- Spring 面向切面(AOP)编程,注解
- Spring面向切面编程AOP(around)
- Spring AOP(面向切面编程)
- Spring核心AOP(面向切面编程)
- Spring---面向切面编程(aop)实例
- Spring之AOP(面向切面编程)
- Spring自定义面向切面编程(AOP)
- Spring之AOP(面向切面编程)
- spring面向切面编程(aop)
- Spring AOP 面向切面编程
- Longest Common Prefix
- POJ 3660 Cow Contest (Floyd运用到传递闭包中~)
- aria2 for windows
- linux切换用户
- raid 磁盘阵列
- spring framework面向切面编程示例(xml配置):spring-aop
- Lightoj 1132 Summing up Powers(矩阵快速幂)
- 3Sum
- spring配置quartz入门案例
- Zero_is_start
- 3Sum Closest
- 【图论】hdu6073 Matching In Multiplication
- 指针的学习
- python 编写规范 pep8 的问题摘录