Spring進行面向切面編程的一個簡單例子

1、eclipse新建java項目取名SpringTestjava

 

2、導入sping包到構建路徑spring

還須要aspectjweaver.jarexpress

3、建立java類(固然先要建立各類包)eclipse

IHelloService.javaide

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

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()執行結束......");
	}
}

4、配置文件.net

在src目錄下建立resources目錄,並在其下創建hello-beans.xml內容以下:code

<?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>

5、測試

測試程序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();

	}

}

 

運行結果以下:

6、利用配置文件實現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());

	}

}

運行

參考:

Spring AOP 詳解

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

最最簡單的spring及AOP實例

相關文章
相關標籤/搜索