package com.cgy.springrecipes.sequence;
/**
* 用于定义前缀生成操作
*/
public interface PrefixGenerator {
public String getPrefix();
}
package com.cgy.springrecipes.sequence;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 使用特殊模式格式化当前系统日期以生成前缀
*/
public class DatePrefixGenerator implements PrefixGenerator{
private DateFormat formatter;
public void setPattern(String pattern) {
this.formatter = new SimpleDateFormat(pattern);
}
public String getPrefix() {
return formatter.format(new Date());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<property name="prefixGenerator">
<ref bean="datePrefixGenerator"/>
</property>
</bean>
<bean id="datePrefixGenerator" class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean></beans>
此时运行出来的序列为2017033010000A和2017033010001A
<ref>元素的bean属性名称可以使对IoC容器中的任何Bean的引用(即使这个Bean不在同一个XML配置文件中定义)。如果想引用相同XML文件中的一个Bean,应该使用local属性。使用local属性的好处是,xml编辑器将帮助校验BeanID是否存在于相同的XML中,因为local的值是一个XML ID引用。
简写形式
<property name="prefixGenerator" ref="datePrefixGenerator"/>
但是注意:该简写相当于使用bean属性,因此无法利用xml编辑器的验证。
********************************************************************************************************************************************Spring2.X之后还有一个便利的简写来指定Bean引用,利用pschema将bean引用作为<bean>元素的一个属性。同时注意为了
区分Bean引用与简单的属性值,必须在属性名后加上-ref后缀。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- <bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<property name="prefixGenerator">
<ref local="datePrefixGenerator"/>
</property>
</bean> -->
<bean id="sequenceGenerator"
class="com.cgy.springrecipes.sequence.SequenceGenerator"
p:suffix="A"
p:initial="10000"
p:prefixGenerator-ref="datePrefixGenerator"/>
<bean id="datePrefixGenerator" class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</beans>
package com.cgy.springrecipes.sequence;
public class SequenceGenerator {
private PrefixGenerator prefixGenerator;
private String suffix;
private int initial;
private int counter;
public SequenceGenerator() {}
public SequenceGenerator(PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}
......省略
}
*****************************************************************************************************************************************<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<constructor-arg>
<ref local="datePrefixGenerator"/>
</constructor-arg>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<property name="prefixGenerator">
<bean class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</property>
</bean></beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="suffix" value="A"/>
<property name="initial" value="10000"/>
<constructor-arg>
<bean class="com.cgy.springrecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</constructor-arg>
</bean></beans>