- 浏览: 867802 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (687)
- java (127)
- servlet (38)
- struts (16)
- spring (22)
- hibernate (40)
- javascript (58)
- jquery (18)
- tomcat (51)
- 设计模式 (6)
- EJB (13)
- jsp (3)
- oracle (29)
- RUP (2)
- ajax (3)
- java内存管理 (4)
- java线程 (12)
- socket (13)
- path (5)
- XML (10)
- swing (2)
- UML (1)
- JBPM (2)
- 开发笔记 (45)
- Note参考 (15)
- JAXB (4)
- Quartz (2)
- 乱码 (2)
- CSS (2)
- Exception (4)
- Tools (7)
- sqlserver (3)
- DWR (7)
- Struts2 (47)
- WebService (2)
- 问题解决收藏 (7)
- JBOSS (7)
- cache (10)
- easyUI (19)
- jQuery Plugin (11)
- FreeMarker (6)
- Eclipse (2)
- Compass (2)
- JPA (1)
- WebLogic (1)
- powerdesigner (1)
- mybatis (1)
最新评论
-
bugyun:
受教了,谢谢
java 正则表达式 过滤html标签 -
xiongxingxing_123:
学习了,感谢了
java 正则表达式 过滤html标签 -
wanmeinange:
那如果无状态的。对同一个任务并发控制怎么做?比如继承Quart ...
quartz中参数misfireThreshold的详解 -
fanjieshanghai:
...
XPath 元素及属性查找 -
tianhandigeng:
还是没明白
quartz中参数misfireThreshold的详解
所谓Spring静态切入点,相对于动态切入点来说,具有良好的性能,因为静态切入点只在代理创建时候执行一次,而不是在运行期间,每次目标方法执行前都进行执行,下面,以实例说明如何定义静态切入点
看我我前一篇blog的朋友都知道,如果不定义切入点,通知方法是会对整个目标类的所有方法均进行切入的
但实际需求中,我们可能对其中的几个方法执行A通知,对其他的方法执行B通知,这时候,就需要通过定义不同的切入点来进行区分
目标接口:
package StaticAdvisorTest;
public interface Shopping ...{
public String buySomething(String type);
public String buyAnything(String type);
public String sellSomething(String type);
public String sellAnything(String type);
}
public interface Shopping ...{
public String buySomething(String type);
public String buyAnything(String type);
public String sellSomething(String type);
public String sellAnything(String type);
}
javabean:
package StaticAdvisorTest;
public class Customer ...{
private String name;
private String age;
public Customer()...{
}
public Customer(String name,String age)...{
this.name=name;
this.age=age;
}
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
public class Customer ...{
private String name;
private String age;
public Customer()...{
}
public Customer(String name,String age)...{
this.name=name;
this.age=age;
}
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
业务目标实现类:
package StaticAdvisorTest;
public class ShoppingImpl implements Shopping ...{
private Customer customer;
public Customer getCustomer() ...{
return customer;
}
public void setCustomer(Customer customer) ...{
this.customer = customer;
}
public String buySomething(String type) ...{
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
return null;
}
public String buyAnything(String type) ...{
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
return null;
}
public String sellAnything(String type) ...{
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
return null;
}
public String sellSomething(String type) ...{
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
return null;
}
}
public class ShoppingImpl implements Shopping ...{
private Customer customer;
public Customer getCustomer() ...{
return customer;
}
public void setCustomer(Customer customer) ...{
this.customer = customer;
}
public String buySomething(String type) ...{
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
return null;
}
public String buyAnything(String type) ...{
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
return null;
}
public String sellAnything(String type) ...{
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
return null;
}
public String sellSomething(String type) ...{
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
return null;
}
}
通知(切面)方法:
package StaticAdvisorTest;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
//前置通知
public class WelcomeAdvice implements MethodBeforeAdvice ...{
public void before(Method method, Object[] args, Object obj)
throws Throwable ...{
String type=(String)args[0];
System.out.println("Hello welcome to buy "+type);
}
}
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
//前置通知
public class WelcomeAdvice implements MethodBeforeAdvice ...{
public void before(Method method, Object[] args, Object obj)
throws Throwable ...{
String type=(String)args[0];
System.out.println("Hello welcome to buy "+type);
}
}
下面是重点,我们想对所有的buy方法进行通知处理,也就是在所有的buy方法上定义切面
spring为我们创建了静态切入点的父类 StaticMethodMatcherPointCut ,如果我们想实现自制的静态切入点,只要继承这个类就可以了,不过一般情况下,我们使用spring提供的静态切入点NameMatchMethodPointCut就足够了
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id="customer" class="StaticAdvisorTest.Customer">
<constructor-arg index="0">
<value>gaoxiang</value>
</constructor-arg>
<constructor-arg index="1">
<value>26</value>
</constructor-arg>
</bean>
<bean id="shoppingImpl" class="StaticAdvisorTest.ShoppingImpl">
<property name="customer">
<ref local="customer"/>
</property>
</bean>
<!-- 定义通知 -->
<bean id="shoppingAdvise" class="StaticAdvisorTest.WelcomeAdvice"></bean>
<!-- 定义切入点 -->
<bean id="shoppingPointCutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName">
<value>sell*</value>
</property>
<property name="advice">
<ref bean="shoppingAdvise"/>
</property>
</bean>
<!-- 定义代理 -->
<bean id="StaticAdvisorTest" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>StaticAdvisorTest.Shopping</value>
</property>
<property name="interceptorNames">
<list>
<value>shoppingPointCutAdvisor</value>
</list>
</property>
<property name="target">
<ref bean="shoppingImpl"/>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id="customer" class="StaticAdvisorTest.Customer">
<constructor-arg index="0">
<value>gaoxiang</value>
</constructor-arg>
<constructor-arg index="1">
<value>26</value>
</constructor-arg>
</bean>
<bean id="shoppingImpl" class="StaticAdvisorTest.ShoppingImpl">
<property name="customer">
<ref local="customer"/>
</property>
</bean>
<!-- 定义通知 -->
<bean id="shoppingAdvise" class="StaticAdvisorTest.WelcomeAdvice"></bean>
<!-- 定义切入点 -->
<bean id="shoppingPointCutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName">
<value>sell*</value>
</property>
<property name="advice">
<ref bean="shoppingAdvise"/>
</property>
</bean>
<!-- 定义代理 -->
<bean id="StaticAdvisorTest" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>StaticAdvisorTest.Shopping</value>
</property>
<property name="interceptorNames">
<list>
<value>shoppingPointCutAdvisor</value>
</list>
</property>
<property name="target">
<ref bean="shoppingImpl"/>
</property>
</bean>
</beans>
<!-- 如果不使用通配符,则用以下表达
<property name="mappedNames">
<list>
<value>sellSomething</value>
<value>sellAnything</value>
</list>
</property>
-->
测试程序:
package StaticAdvisorTest;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestAdvisor ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"StaticAdvisorTest"+File.separator+"hello.xml";
BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));
Shopping shopping=null;
shopping=(Shopping)factory.getBean("StaticAdvisorTest");
shopping.buySomething("something");
shopping.buyAnything("anything");
shopping.sellAnything("anything");
shopping.sellSomething("something");
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestAdvisor ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"StaticAdvisorTest"+File.separator+"hello.xml";
BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));
Shopping shopping=null;
shopping=(Shopping)factory.getBean("StaticAdvisorTest");
shopping.buySomething("something");
shopping.buyAnything("anything");
shopping.sellAnything("anything");
shopping.sellSomething("something");
发表评论
文章已被作者锁定,不允许评论。
-
spring 2.5 注释驱动的 IoC 功能
2012-06-15 23:35 899概述 注释配置相对于 XML 配置具有很多的优势: 它可 ... -
在Spring BeanFactory容器中管理两种bean
2012-06-08 22:45 2976在Spring BeanFactory容器 ... -
Spring-security 1
2011-05-15 20:20 1629有没有发现一个问题 ... -
容器背后的秘密
2011-04-28 00:26 874核心提示:4.4 容器背 ... -
spring 监听器 IntrospectorCleanupListener简介
2011-04-27 15:49 1252其中JavaBeans Introspector是一个类,位置 ... -
通过ApplicationContextAware获取bean
2011-04-25 17:30 1623通过ApplicationContextAware获取bean ... -
详解 Spring 3.0 基于 Annotation 的依赖注入实现
2011-03-14 17:18 1060简介: Spring 的依赖配置方式与 Spring 框架的内 ... -
spring+hibernate操控LOB对象
2011-03-12 00:27 950spring为hibernate提供了对LOB对像的支持, ... -
Spring AOP
2011-03-11 10:32 828AOP是Aspect Oriented Programming ... -
Spring AOP配置选项
2011-03-11 08:59 1561Spring实现动态代理配置是有两种配置文件:1、 xml ... -
spring 事务传播属性和隔离级别
2011-02-13 14:53 1038一、Propagation (事务的 ... -
spring学习,实例化bean的方式及Bean的作用域
2010-10-26 10:09 1124今天继续学习Spring,黎老师对Spring中比较重要的部分 ... -
Spring学习 xml配置依赖注入
2010-10-26 09:39 957最近项目中也配了几遍ssh框架了,不过老出问题,还是Sprin ... -
IOC控制反转和DI依赖注入区别
2010-10-12 15:20 1147IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IO ... -
Quartz cron表达式详解
2010-09-27 15:13 920字段 允许 ... -
Spring Bean的作用域
2010-09-27 15:12 912XML代码 <bean id="role ... -
spring配置中调用properties文件
2010-09-27 14:59 1547system.propertiesdatabase.url=j ... -
DataSourceUtils.getConnection()要与其对称方法配合使用
2010-09-15 16:56 1310DataSourceUtils.getConnection() ... -
使用BeanNameAutoProxyCreator实现spring的自动代理
2010-09-06 15:05 985提到代理,我们可以使用ProxyBeanFactory,并配置 ... -
Spring源代码解析(一):IOC容器
2010-08-17 13:33 775在认真学习Rod.Johnson的三部曲之一:< < ...
相关推荐
浅谈 Spring 原理 透析,ioc aop
主要介绍了详解Spring 框架中切入点 pointcut 表达式的常用写法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
狂神spring-security静态资源.zip
主要介绍了浅谈Spring Security 对于静态资源的拦截与放行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
浅谈Spring+Hibernate整合(共16页).doc
结合spring框架实现的静态代理,接口回掉
spring-security静态资源
spring 静态实例化
详细介绍spring aop中9种切入点表达式的写法execute、within、this、target、args、@target、@within、@annotation、@args
NULL 博文链接:https://free9277.iteye.com/blog/1935492
springboot项目中静态方法中使用spring托管的三种类方式
使用mindmaster打开
JavaEE spring静态工厂创建实例
NULL 博文链接:https://xpninja.iteye.com/blog/1138916
来自于狂神Spring Security
切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指示符如下: execution:用于匹配方法执行的连接点; within:用于匹配指定类型内的方法执行...
Spring专题与案例 Spring代理 静态代理和动态代理 Spring专题与案例-代理(静态和动态) Spring专题与案例-代理(静态和动态)
SpringSecurity项目的静态资源
狂神说SpringSecurity静态资源.rar
主要介绍了Spring实战之使用静态工厂方法创建Bean操作,结合实例形式分析了静态工厂方法创建Bean的相关实现步骤与操作注意事项,需要的朋友可以参考下