Spring进行面向切面编程的一个简单例子

一、eclipse新建java项目取名SpringTest

 

二、导入sping包到构建路径

还需要aspectjweaver.jar

三、创建java类(当然先要创建各种包)

IHelloService.java

package com.zjptcc.wxw.spring.hello;

public interface IHelloService {
	public void sayHello();
	public void sayChinaHello();

}

HelloServiceImpl.java

package com.zjptcc.wxw.spring.hello;

public class HelloServiceImpl implements IHelloService {
	private String Hello;
	private String ChinaHello;

	@Override
	public void sayHello() {
		// TODO 自动生成的方法存根
		System.out.println(Hello);
	}

	@Override
	public void sayChinaHello() {
		// TODO 自动生成的方法存根
		System.out.println(ChinaHello);
	}

	public String getHello() {
		return Hello;
	}

	public void setHello(String hello) {
		Hello = hello;
	}

	public String getChinaHello() {
		return ChinaHello;
	}

	public void setChinaHello(String chinaHello) {
		ChinaHello = chinaHello;
	}

}

SimpleHelloBean.java

package com.zjptcc.wxw.spring.hello;

public class SimpleHelloBean {
	public void sayHello(){
		System.out.println("Hello World!");
	}
}

 

AopTest.java

package com.zjptcc.wxw.spring.hello;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AopTest {
	
	/*Pointcut for sayHello*/
	@Pointcut("execution(* *.sayHello())")
	public void hellopoint() {
	}

	@Before("hellopoint()")
	public void beforehello() {
		System.out.println("接下去调用sayHello()......");
	}
	
	@AfterReturning("hellopoint()")
	public void afterhello() {
		System.out.println("函数sayHello()执行结束......");
	}
	
	
	/*Pointcut for sayChinaHello*/
	@Pointcut("execution(* *.sayChinaHello())")
	public void helloChinapoint() {
	}
	
	@Before("helloChinapoint()")
	public void beforehelloChina() {
		System.out.println("接下去调用sayChinaHello()......");
	}
	
	@AfterReturning("helloChinapoint()")
	public void afterhelloChina() {
		System.out.println("函数sayChinaHello()执行结束......");
	}
}

四、配置文件

在src目录下创建resources目录,并在其下建立hello-beans.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <context:annotation-config/> 
	<aop:aspectj-autoproxy />
	

	 <bean id="helloService" class="com.zjptcc.wxw.spring.hello.HelloServiceImpl">
		<property name="Hello">
			<value>Hello,china!</value>
		</property>
			<property name="ChinaHello">
			<value>你好,中国欢迎您!</value>
		</property>
	</bean>
	 <bean id="SimpleHelloBean" class="com.zjptcc.wxw.spring.hello.SimpleHelloBean">
	</bean>
	<bean id="aopTest" class="com.zjptcc.wxw.spring.hello.AopTest" />


</beans>

五、测试

测试程序TestHelloService.java如下:

package com.zjptcc.wxw.spring.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zjptcc.wxw.spring.hello.IHelloService;
import com.zjptcc.wxw.spring.hello.SimpleHelloBean;

public class TestHelloService {

	/**
	 * @param args
	 */
	private static ApplicationContext ctx;

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		ctx = new ClassPathXmlApplicationContext("resources/hello-beans.xml");

		//用接口
		IHelloService helloWorld = (IHelloService) ctx
				.getBean("helloService");
		helloWorld.sayHello();
		helloWorld.sayChinaHello();
		
		System.out.println("------------------------------------------------------------------------------------");

		//用类
		SimpleHelloBean SimpleHello = (SimpleHelloBean) ctx
				.getBean("SimpleHelloBean");
		SimpleHello.sayHello();

	}

}

 

运行结果如下:

235038_WQRU_2245781.png

六、利用配置文件实现AOP

java类

package com.zjptcc.wxw.spring.person;
public class PersonService {
	private String name;

	public void setName(String name) {
		this.name = name;
	}
	
	public void info(){
		System.out.println("此人姓名为:"+name);
	}
	
	public String getName(){
		return name;
	}
}
package com.zjptcc.wxw.spring.person;

public class PersonAop {
	public void before_info() {
		System.out.println("接下去,调用info()显示人名......");
	}
	public void after_info() {
		System.out.println("调用info()结束......");
	}
	public void after_get() {
		System.out.println("调用getName()结束......");
	}
	
}

resources/person-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<context:annotation-config />
	<aop:aspectj-autoproxy />

	<bean id="personService" class="com.zjptcc.wxw.spring.person.PersonService">
		<property name="name">
			<value>露茜</value>
		</property>
	</bean>

	<!--定义切面 -->
	<bean id="personaop" class="com.zjptcc.wxw.spring.person.PersonAop" />
	<aop:config>
		<aop:aspect ref="personaop">
			<aop:pointcut id="infopoint" expression="execution(* *.info(..))" />
			<aop:before pointcut-ref="infopoint" method="before_info" />
			<aop:after pointcut-ref="infopoint" method="after_info" />
		</aop:aspect>
		<aop:aspect ref="personaop">
			<aop:pointcut id="getpoint" expression="execution(* *.getName(..))" />
			<aop:after pointcut-ref="getpoint" method="after_get" />
		</aop:aspect>
	</aop:config>

</beans>

测试

package com.zjptcc.wxw.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zjptcc.wxw.spring.person.PersonService;

public class TestPersonService {

	/**
	 * @param args
	 */
	private static ApplicationContext ctx;

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		ctx = new ClassPathXmlApplicationContext("resources/person-beans.xml");

		PersonService p = (PersonService) ctx
				.getBean("personService");
		p.info();
		System.out.println(p.getName());

	}

}

运行

101707_Ao7F_2245781.png

参考:

Spring AOP 详解

spring的JavaConfig方式及xml配置文件混用的例子

最最简单的spring及AOP实例

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/2245781/blog/597209

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值