spring framework入门(7):spring framework面向切面编程示例(xml配置)

参照:https://www.cnblogs.com/kuotian/p/8863257.html

示例代码: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.java

package bean;

public interface Animate {
	public String show() throws Exception;
}

Cat.java

 

package bean;

public class Cat implements Animate {

	public String show() {
		return "i'm cat";
	}

}

Dog.java

 

package bean;

public class Dog implements Animate{

	public String show() throws Exception {
//		throw new Exception("ko");
		return "i'm dog";
	}

}

Person.java

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.java

 

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());
	}

}

4.测试调用

输出:

 

 

 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackletter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值