Spring AOP面向切面编程基础(二)

基于xml配置切面和通知
定义需要增强的基础功能:

package com.giser.springframework.aop;

public interface Performance {
	void perform();
}
package com.giser.springframework.aop;

public class GISPerformance implements Performance {

	@Override
	public void perform() {
		System.out.println("gis performance");
	}
}

定义切面类

package com.giser.springframework.aop;

public class Audience {
	public void silenceCellPhones() {
		System.out.println("Silencing cell phones");
	}

	public void takeSeats() {
		System.out.println("takeSeats");
	}

	public void applause() {
		System.out.println("applause");
	}

	public void demandRefund() {
		System.out.println("demandRefund");
	}
}

在spring-aop.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/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	
	<context:component-scan base-package="com.giser.springframework.aop" />

	
	<bean id="audience" class="com.giser.springframework.aop.Audience" />
	
	
	<bean id="gisPerformance" class="com.giser.springframework.aop.GISPerformance" />
	
	
	<aop:aspectj-autoproxy />
	
	
	<aop:config>
		
		<aop:aspect ref="audience">
			
			<aop:pointcut 
				id="performance" 
				expression="execution(* com.giser.springframework.aop.Performance.*(..))"  />
				
			<aop:before method="silenceCellPhones" pointcut-ref="performance" />
			<aop:before method="takeSeats" pointcut-ref="performance" />
			<aop:after-returning method="applause" pointcut-ref="performance" />
			<aop:after-throwing method="demandRefund" pointcut-ref="performance" />
		aop:aspect>
	aop:config>
	
beans>

注意:Spring AOP只支持方法级别的连接点,必须将切面类声明为bean,交由Spring管理,不然是不会被拦截的!因为Spring只能拦截其管理范围内的bean的方法调用。

测试:

package com.giser.springframework.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:spring-aop.xml"})
public class AopTest {
	
	@Autowired
	private Performance performance;
	
	@Test
	public void testPerformance() {
		performance.perform();
	}
}

结果:

Silencing cell phones
takeSeats
gis performance
applause

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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0modelVersion>
	<groupId>com.gisergroupId>
	<artifactId>giser-java-springartifactId>
	<packaging>warpackaging>
	<version>0.0.1-SNAPSHOTversion>
	<name>giser-java-spring Maven Webappname>
	<url>http://maven.apache.orgurl>

	<properties>
		<spring.version>4.3.9.RELEASEspring.version>
		<maven.compiler.source>1.8maven.compiler.source>
		<maven.compiler.target>1.8maven.compiler.target>
		<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
	properties>

	<dependencies>
		<dependency>
			<groupId>junitgroupId>
			<artifactId>junitartifactId>
			<version>3.8.1version>
			<scope>testscope>
		dependency>
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-testartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aopartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aspectsartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-beansartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-contextartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-context-supportartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-coreartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-expressionartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webartifactId>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webmvcartifactId>
		dependency>
		
		
		
		<dependency>
			<groupId>org.aspectjgroupId>
			<artifactId>aspectjrtartifactId>
			<version>1.9.5version>
		dependency>
		<dependency>
			<groupId>org.aspectjgroupId>
			<artifactId>aspectjweaverartifactId>
			<version>1.9.5version>
		dependency>
		
		<dependency>
		    <groupId>aopalliancegroupId>
		    <artifactId>aopallianceartifactId>
		    <version>1.0version>
		dependency>
		
		
	dependencies>

	<build>
		<finalName>giser-java-springfinalName>
	build>

	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-aopartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-aspectsartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-beansartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-contextartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-context-supportartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-coreartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-expressionartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-instrumentartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-instrument-tomcatartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-jdbcartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-jmsartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-messagingartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-ormartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-oxmartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-testartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-txartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-webartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-webmvcartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-webmvc-portletartifactId>
				<version>${spring.version}version>
			dependency>
			<dependency>
				<groupId>org.springframeworkgroupId>
				<artifactId>spring-websocketartifactId>
				<version>${spring.version}version>
			dependency>
		dependencies>
	dependencyManagement>

project>

几个核心的jar:

		
		<dependency>
			<groupId>org.aspectjgroupId>
			<artifactId>aspectjrtartifactId>
			<version>1.9.5version>
		dependency>
		<dependency>
			<groupId>org.aspectjgroupId>
			<artifactId>aspectjweaverartifactId>
			<version>1.9.5version>
		dependency>
		
		<dependency>
		    <groupId>aopalliancegroupId>
		    <artifactId>aopallianceartifactId>
		    <version>1.0version>
		dependency>
		

你可能感兴趣的:(#,Spring)