简单的Spring AOP_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 简单的Spring AOP

简单的Spring AOP

 2013/10/26 6:44:54  eric2500  程序员俱乐部  我要评论(0)
  • 摘要:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework
  • 标签:Spring
class="java">
<?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: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-2.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ">

	<bean id="greetingService"
		class="com.jesse.spring.simple.service.impl.GreetingServiceImpl">
		<property name="string" value="Hello, Jesse!" />
	</bean>
	
	<bean id="point" class="com.jesse.spring.simple.service.Point">
	</bean>
	
	<aop:config>
		<aop:aspect ref="point">	<!-- 创建切面 -->
			<aop:pointcut id="greetPointcut" expression="execution(* *.greet(..))"/> <!-- 创建切入点并绑定greet方法 -->
			<aop:before method="greetBefore" pointcut-ref="greetPointcut"/>
			<aop:after method="greetAfter" pointcut-ref="greetPointcut"/>
		</aop:aspect>
	</aop:config>


</beans>

package com.jesse.spring.simple.service.impl;

import org.apache.log4j.Logger;

import com.jesse.spring.simple.service.GreetingService;

public class GreetingServiceImpl implements GreetingService {

	private static final Logger logger = Logger
			.getLogger(GreetingServiceImpl.class);
	private String string;

	@Override
	public void greet() {
		logger.info(this);
		logger.info(string);
	}

	public String getString() {
		return string;
	}

	public void setString(String string) {
		this.string = string;
	}

}

package com.jesse.spring.simple.service;

import org.apache.log4j.Logger;

public class Point {
	
	private static final Logger logger = Logger.getLogger(Point.class);

	public void greetBefore() {
		logger.info("this is before method.....");
	}
	
	public void greetAfter() {
		logger.info("this is after method.....");
	}
	
}

[2013-10-26 03:32:05]  INFO (Point.java:10) -this is before method.....
[2013-10-26 03:32:05]  INFO (GreetingServiceImpl.java:15) -com.jesse.spring.simple.service.impl.GreetingServiceImpl@170eb6d
[2013-10-26 03:32:05]  INFO (GreetingServiceImpl.java:16) -Hello, Jesse!
[2013-10-26 03:32:05]  INFO (Point.java:14) -this is after method.....
[2013-10-26 03:32:05]  INFO (Point.java:10) -this is before method.....
[2013-10-26 03:32:05]  INFO (GreetingServiceImpl.java:15) -com.jesse.spring.simple.service.impl.GreetingServiceImpl@170eb6d
[2013-10-26 03:32:05]  INFO (GreetingServiceImpl.java:16) -Hello, Jesse!
[2013-10-26 03:32:05]  INFO (Point.java:14) -this is after method.....
上一篇: java 云秘通信小项目 下一篇: 没有下一篇了!
发表评论
用户名: 匿名