Spring中使用AOP的简单例子
今天刚刚学习了Spring,做个简单的例子分享一下。。。
1、首先,创建接口BookService(包com.Vy.spring.Biz)
package com.Vy.spring.Biz;
public interface BookService {
public boolean buy(String userName,String bookName,double price);
}
2、创建接口实现类BookServiceImpl(包com.Vy.spring.BizImpl)
package com.Vy.spring.BizImpl;
import com.Vy.spring.Biz.BookService;
public class BookServiceImpl implements BookService {
@Override
public boolean buy(String userName, String bookName, double price) {
// TODO Auto-generated method stub
System.out.println("业务方法buy开始执行");
System.out.println("·"+userName+"购买图书:"+bookName);
System.out.println("·"+userName+"增加积分"+(int)(price/10));
System.out.println("·"+"向物流系统下发货单");
System.out.println("业务方法buy结束");
return true;
}
}
3、添加spring支持(spring版本为2.5)
选择AOP库和Spring Core支持
下一步修改xml名字为aop.xml
4、编写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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bookServiceTarget" class="com.Vy.spring.BizImpl.BookServiceImpl"></bean>
<bean id="logAdvice" class="com.Vy.spring.BizImpl.LogAdvice"></bean>
<bean id="bookService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.Vy.spring.Biz.BookService</value>
</property>
<property name="interceptorNames">
<list><value>logAdvice</value></list>
</property>
<property name="target" ref="bookServiceTarget"></property>
</bean>
</beans>
5、创建测试类test(包Test)
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.Vy.spring.Biz.BookService;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
BookService bookService=(BookService)context.getBean("bookService");
bookService.buy("znLin", "spring in action", 20.00);
}
}
6、运行测试、显示结果
[系统日志][2011 年 03 月 21 日 06 时 21 分 28 秒]buy([znLin, spring in action, 20.0])
业务方法buy开始执行
·znLin购买图书:spring in action
·znLin增加积分2
·向物流系统下发货单
业务方法buy结束
------------------------------------------------------
第一次写博客,不会设置修改图片,以后学习,写的不好,看者见谅!!!
1、首先,创建接口BookService(包com.Vy.spring.Biz)
package com.Vy.spring.Biz;
public interface BookService {
public boolean buy(String userName,String bookName,double price);
}
2、创建接口实现类BookServiceImpl(包com.Vy.spring.BizImpl)
package com.Vy.spring.BizImpl;
import com.Vy.spring.Biz.BookService;
public class BookServiceImpl implements BookService {
@Override
public boolean buy(String userName, String bookName, double price) {
// TODO Auto-generated method stub
System.out.println("业务方法buy开始执行");
System.out.println("·"+userName+"购买图书:"+bookName);
System.out.println("·"+userName+"增加积分"+(int)(price/10));
System.out.println("·"+"向物流系统下发货单");
System.out.println("业务方法buy结束");
return true;
}
}
3、添加spring支持(spring版本为2.5)
选择AOP库和Spring Core支持
下一步修改xml名字为aop.xml
4、编写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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bookServiceTarget" class="com.Vy.spring.BizImpl.BookServiceImpl"></bean>
<bean id="logAdvice" class="com.Vy.spring.BizImpl.LogAdvice"></bean>
<bean id="bookService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.Vy.spring.Biz.BookService</value>
</property>
<property name="interceptorNames">
<list><value>logAdvice</value></list>
</property>
<property name="target" ref="bookServiceTarget"></property>
</bean>
</beans>
5、创建测试类test(包Test)
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.Vy.spring.Biz.BookService;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
BookService bookService=(BookService)context.getBean("bookService");
bookService.buy("znLin", "spring in action", 20.00);
}
}
6、运行测试、显示结果
[系统日志][2011 年 03 月 21 日 06 时 21 分 28 秒]buy([znLin, spring in action, 20.0])
业务方法buy开始执行
·znLin购买图书:spring in action
·znLin增加积分2
·向物流系统下发货单
业务方法buy结束
------------------------------------------------------
第一次写博客,不会设置修改图片,以后学习,写的不好,看者见谅!!!