首页 > 代码库 > spring-引介通知
spring-引介通知
一、创建项目
项目名称:spring101001
二、添加jar包
1.在项目中创建lib目录
/lib
2.在lib目录下添加相关spring jar包
--用于切面编程
com.springsource.org.aopalliance-1.0.0.jar
commons-logging.jar
junit-4.10.jar
log4j.jar
--用于切面编程
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
三、添加配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加配置文件
配置文件名称:applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
四、创建业务bean
1.在src目录下创建业务bean包
包名:cn.jbit.spring101001.service
2.在包下创建业务bean
业务bean名称:BookStore.java
业务bean内容:
public class BookStore {
/**
* 进书
*/
public void addBook(){
System.out.println("添加图书信息");
}
/**
* 查找图书
*/
public void findBook(){
System.out.println("查找图书信息");
}
}
bean增强名称:TimeExecute.java
bean增强内容:
public interface TimeExecute {
public void isActive(boolean flage);
}
引介名称:TimeClass.java
引介内容:
public class TimeClass extends DelegatingIntroductionInterceptor implements
TimeExecute {
private boolean flage;
@Override
public void isActive(boolean flage) {
this.flage = flage;
}
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
if (flage) {
long begin = System.currentTimeMillis();
Object object = super.invoke(arg0);
long end = System.currentTimeMillis();
System.out.println("方法执行时间毫秒:"+(end-begin));
return object;
}else{
return super.invoke(arg0);
}
}
}
五、在核心配置文件中添加配置信息
<!-- 被代理对象 -->
<bean id="bookstore" class="cn.jbit.spring101001.service.BookStore"></bean>
<!-- 配置增强 -->
<bean id="timezengqiang" class="cn.jbit.spring101001.service.TimeClass"></bean>
<!-- 生成代理对象 -->
<bean id="proxyobject" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 接口 -->
<property name="proxyInterfaces" value="http://www.mamicode.com/cn.jbit.spring101001.service.TimeExecute"></property>
<!-- 针对类实现代理 -->
<property name="proxyTargetClass" value="http://www.mamicode.com/true"></property>
<!-- 配置增强 -->
<property name="interceptorNames" value="http://www.mamicode.com/timezengqiang"></property>
<!-- 代理目标 -->
<property name="target" ref="bookstore"></property>
</bean>
六、测试
1.在项目中创建test目录
/test
2.在test目录中创建测试包
包名:cn.jbit.spring101001.service
3.在测试包中创建测试类
测试类名:IntroTest.java
测试内容:
public class IntroTest {
@Test
public void testYinJie(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
BookStore bookStore = (BookStore) context.getBean("proxyobject");
TimeExecute timeExecute = (TimeExecute) bookStore;
timeExecute.isActive(true);
bookStore.addBook();
}
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1562120
spring-引介通知