编程札记

S

Spring AOP拦截器的序列

Spring AOP 事务不是工作在以下拦截器? <bean id="testAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <idref bean="urlInterceptorInsert" /> <idref bean="urlInterceptorCommit" /> <idref bean="urlInterceptorRelease" /> <idref bean="matchGenericTxInterceptor" /> </list> </property> <property name="beanNames"> <list> <idref local="urlBo" /> </list> </property> </bean> matchGenericTxInterceptor”事务拦截器,假设来拦截器 urlInterceptorInsert,urlInterceptorCommit,urlInterceptorRelease, 但不能如预期一样工作? 解决 3个拦截器在事务管理器拦截器(matchGenericTxInterceptor)之前执行。 为了解决这个问题,必须改变拦截器 XML文件的顺序,如下面的(把 matchGenericTxInterceptor 放在顶部)。 <bean id="testAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <idref bean="matchGenericTxInterceptor" /> <idref bean="urlInterceptorInsert" /> <idref bean="urlInterceptorCommit" /> <idref bean="urlInterceptorRelease" /> </list> </proper

阅读全文 ↗
S

Spring AOP实例(Pointcut,Advisor)

在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截。但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因。它允许你通过它的方法名来拦截方法。另外,一个“切入点”必须具有“Advisor' 相关联。 在Spring AOP中,有三个非常专业术语- Advices, Yiibaicut , Advisor,把它在非官方的方式... Advice – 指示之前或方法执行后采取的行动。 Yiibaicut – 指明哪些方法应该拦截,通过方法的名称或正则表达式模式。 Advisor – 分组"通知"和”切入点“成为一个单元,并把它传递到代理工厂对象。 再次回顾上一个 Spring AOP通知的例子。 File : HijackAroundMethod.java package com.yiibai.aop; import java.util.Arrays; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class HijackAroundMethod implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("Method name : " + methodInvocation.getMethod().getName()); System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments())); System.out.println("HijackAroundMethod : Before method hijacked!"); try { Object result = methodInvocation.proceed(); System.out.println("HijackAroundMethod : Before afte

阅读全文 ↗
S

Spring AOP通知实例 – Advice

Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点。简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后添加额外的功能。 在Spring AOP中,有 4 种类型通知(advices)的支持: 通知(Advice)之前 - 该方法执行前运行 通知(Advice)返回之后 – 运行后,该方法返回一个结果 通知(Advice)抛出之后 – 运行方法抛出异常后, 环绕通知 – 环绕方法执行运行,结合以上这三个通知。 下面的例子显示Spring AOP 通知如何工作。 简单的 Spring 例子 创建一个简单的客户服务类及一个print方法作为演示。 package com.yiibai.customer.services; public class CustomerService { private String name; private String url; public void setName(String name) { this.name = name; } public void setUrl(String url) { this.url = url; } public void printName() { System.out.println("Customer name : " + this.name); } public void printURL() { System.out.println("Customer website : " + this.url); } public void printThrowException() { throw new IllegalArgumentException(); } } File : applicationContext.xml – 一个bean配置文件 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans ht

阅读全文 ↗
S

Spring过滤器组件自动扫描

在这个Spring自动组件扫描的教程,您已经了解如何使Spring自动扫描您的组件。在这篇文章中,我们将展示如何使用组件过滤器自动扫描过程。 1.过滤组件 - 包含 参见下面的例子中使用Spring “过滤” 扫描并注册匹配定义“regex”,即使该类组件的名称未标注 @Component 。 DAO 层 package com.yiibai.customer.dao; public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } } Service 层 package com.yiibai.customer.services; import org.springframework.beans.factory.annotation.Autowired; import com.yiibai.customer.dao.CustomerDAO; public class CustomerService { @Autowired CustomerDAO customerDAO; @Override public String toString() { return "CustomerService [customerDAO=" + customerDAO + "]"; } } Spring 过滤 <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-2.5.xsd http://www.springframework.org/schema/context http://www.springframewo

阅读全文 ↗
S

Spring自动扫描组件

通常情况下,声明所有的Bean类或组件的XML bean配置文件,这样Spring容器可以检测并注册Bean类或组件。 其实,Spring是能够自动扫描,检测和预定义的项目包并实例化bean,不再有繁琐的Bean类声明在XML文件中。 下面是一个简单的Spring项目,包括客户服务和DAO层。让我们来探讨手动申明组件和自动扫描组件之间的不同。 1、手动声明组件 看到在 Spring 的一个正常方式来声明一个 bean。 一个正常的 bean. package com.yiibai.customer.dao; public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } } DAO 层. package com.yiibai.customer.services; import com.yiibai.customer.dao.CustomerDAO; public class CustomerService { CustomerDAO customerDAO; public void setCustomerDAO(CustomerDAO customerDAO) { this.customerDAO = customerDAO; } @Override public String toString() { return "CustomerService [customerDAO=" + customerDAO + "]"; } } bean配置文件(applicationContext.xml),在Spring中的一个普通 bean 配置。 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <be

阅读全文 ↗
测

测试 Spring EL与ExpressionParser

Spring表达式语言(SpEL)支持多种功能,并且可以测试这个特殊的“ExpressionParser”接口的表达式功能。 下面是两个代码片段,展示了使用 Spring EL 的基本用法。 使用SpEL来计算评估文字字符串表达式。 ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("'put spel expression here'"); String msg = exp.getValue(String.class); 使用SpEL来计算评估 bean 属性 – “item.name”. Item item = new Item("yiibai", 100); StandardEvaluationContext itemContext = new StandardEvaluationContext(item); //display the value of item.name property Expression exp = parser.parseExpression("name"); String msg = exp.getValue(itemContext, String.class); 举几个例子来测试使用SpEL。这些代码和注释应该是自我探索。 import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class App { public static void main(String[] args) { ExpressionParser parser = new SpelExpressionParser(); //literal expression

阅读全文 ↗
S

Spring EL正则表达式实例

Spring EL支持正则表达式,可使用一个简单的关键词“matches”。如下实例, @Value("#{'100' matches '\\d+' }") private boolean isDigit; 它测试'100'是否是通过正则表达式‘\\d+‘测试过的一个有效的数字。 Spring EL以注解的形式 请参阅下面的 Spring EL 正则表达式的例子,这里有部分掺入三元运算符,这使得 Spring EL 非常灵活,功能强大。 下面的例子应该是不言自明的。 package com.yiibai.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { // email regular expression String emailRegEx = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)" + "*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; // if this is a digit? @Value("#{'100' matches '\\d+' }") private boolean validDigit; // if this is a digit + ternary operator @Value("#{ ('100' matches '\\d+') == true ? " + "'yes this is digit' : 'No this is not a digit' }") private String msg; // if this emailBean.emailAddress contains a valid email address? @Value("#{emailBean.emailAddress matches customerBean.emailRegEx}") private boolean validEmail; //getter and setter methods, a

阅读全文 ↗
S

Spring EL Lists,Maps实例

在这篇文章中,我们将介绍如何使用Spring EL从 Map 和 List 中获得值。事实上,使用SpEL与 Map 和 List 的工作方式与Java是完全一样的。请参阅例如: //get map whete key = 'MapA' @Value("#{testBean.map['MapA']}") private String mapA; //get first value from list, list is 0-based. @Value("#{testBean.list[0]}") private String list; Spring EL以注解的形式 在这里,创建了一个HashMap和ArrayList,并添加了一些初始测试数据。 package com.yiibai.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{testBean.map['MapA']}") private String mapA; @Value("#{testBean.list[0]}") private String list; public String getMapA() { return mapA; } public void setMapA(String mapA) { this.mapA = mapA; } public String getList() { return list; } public void setList(String list) { this.list = list; } @Override public String toString() { return "Customer [mapA=" + mapA + ", list=" + list + "]"; } } package com.yiibai.core; import java.util.ArrayList; import java.util.HashMap; import java.

阅读全文 ↗
S

Spring EL三元运算(if-then-else)实例

Spring EL支持三元运算符,执行“if then else”条件检查。 例如, condition ? true : false Spring EL以注解形式 Spring EL三元运算符可使用@Value注解。在这个例子中,如果“itemBean.qtyOnHand”小于100,则设置“customerBean.warning”为true,否则将其设置为false。 package com.yiibai.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{itemBean.qtyOnHand < 100 ? true : false}") private boolean warning; public boolean isWarning() { return warning; } public void setWarning(boolean warning) { this.warning = warning; } @Override public String toString() { return "Customer [warning=" + warning + "]"; } } package com.yiibai.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("itemBean") public class Item { @Value("99") private int qtyOnHand; public int getQtyOnHand() { return qtyOnHand; } public void setQtyOnHand(int qtyOnHand) { this.qtyOnHand = qtyOnHand; } } 输出 Customer [w

阅读全文 ↗
S

Spring EL运算符实例

Spring EL支持大多数标准的数学,逻辑和关系运算符。 例如, 关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , le), 大于 (>, gt), 和大于或等于 (>=, ge). 逻辑运算符 – 且, 或, 非 (!). 数学运算符 – 加法(+), 减法 (-), 乘法 (*), 除法(/), 除模(%) 和指数幂 (^). Spring EL以注解的形式 这个例子说明了如何在 SpEL 中使用运算符。 package com.yiibai.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { //Relational operators @Value("#{1 == 1}") //true private boolean testEqual; @Value("#{1 != 1}") //false private boolean testNotEqual; @Value("#{1 < 1}") //false private boolean testLessThan; @Value("#{1 <= 1}") //true private boolean testLessThanOrEqual; @Value("#{1 > 1}") //false private boolean testGreaterThan; @Value("#{1 >= 1}") //true private boolean testGreaterThanOrEqual; //Logical operators , numberBean.no == 999 @Value("#{numberBean.no == 999 and numberBean.no < 900}") //false private boolean testAnd; @Value("#{numberBean.no == 999 or numberBean.no < 900}")

阅读全文 ↗
S

Spring EL方法调用实例

Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作“使用SpEL方法调用”。 Spring EL在注解的形式 了解如何实现Spring EL方法调用与@Value注释。 package com.yiibai.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{'yiibai'.toUpperCase()}") private String name; @Value("#{priceBean.getSpecialPrice()}") private double amount; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } @Override public String toString() { return "Customer [name=" + name + ", amount=" + amount + "]"; } } package com.yiibai.core; import org.springframework.stereotype.Component; @Component("priceBean") public class Price { public double getSpecialPrice() { return new Double(199.09); } } 输出 Customer [name=YIIBAI, amount=199.09] 一点解释 在字串文本上调用 toUpperCase()方法。 @Value("#{'yiibai'.

阅读全文 ↗