SequenceGeneratorImpl 类
@Scope("session")
public class SequenceGeneratorImpl implements SequenceGenerator {
/** 前缀 ***/
private String prefix;
/** 后缀 **/
private String suffix;
/** 初始值 **/
private int initial;
/** 计数器 ***/
private int counter;
public SequenceGeneratorImpl() {
}
public SequenceGeneratorImpl(String prefix, String suffix, int initial,
int counter) {
super();
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial;
this.counter = counter;
}
public String getSequence() {
StringBuffer sb = new StringBuffer();
sb.append(prefix);
sb.append(initial + counter++);
sb.append(suffix);
return sb.toString();
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public int getInitial() {
return initial;
}
public void setInitial(int initial) {
this.initial = initial;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
Bean 的配置
<bean id="sequenceGenerator" class="SpringInAction.IOC.SequenceGeneratorImpl">
<property name="prefix">
<value>30</value>
</property>
<property name="suffix">
<value>A</value>
</property>
<property name="initial">
<value>10000</value>
</property>
</bean>
运行结果
3010000A
3010001A
----------------------------------------------------------------------------------------------------------------
简单的属性注入:
<bean id="sequenceGenerator" class="SpringInAction.IOC.SequenceGeneratorImpl">
<property name="prefix" value="B30"/>
<property name="suffix" value="A"/>
<property name="initial" value="10000" />
</bean>
结果
B3010000A
B3010001A
-----------------------------------------------------------------------------------------------------------------------------------------
构造器注入属性
<bean id="sequenceGenerator" class="SpringInAction.IOC.SequenceGeneratorImpl">
<constructor-arg value="C30" />
<constructor-arg value="B" />
<constructor-arg value="10000" />
</bean>
--------------------------------------------------------------------------------------------------------------------------------------
从Spring 2.0 开始添加了一种便利的属性定义缩写。 它使用 p schema 像 bean 元素中的属性那样定义bean属性。这样可以缩短XML的配置
<bean id="sequenceGenerator" class="SpringInAction.IOC.SequenceGeneratorImpl" p:prefix="D" p:suffix="D" p:initial="10000"/>