1.实例化Spring IoC容器:
Spring Ioc容器有两种类型: BeanFactory和ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
从IoC容器中得到Bean
SequenceGenerator generator = (SequenceGenerator) context.getBean("sequenceGenerator");
2.配置Spring IoC容器中的Bean(简单属性:
完整事例代码(包括SequenceGenerator.java + beans.xml + Main.java),有多个beans.xml任选其一,不同的beans.xml说明了不同的配置方式。
SequenceGenerator.java
package com.apress.springrecipes.sequence;
public class SequenceGenerator {
private String prefix;
private String suffix;
private int initial;
private int counter;
public SequenceGenerator() {}
public SequenceGenerator(String prefix, String suffix, int initial) {
super();
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setInitial(int initial) {
this.initial = initial;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
buffer.append(initial + counter++);
buffer.append(suffix);
return buffer.toString();
}
}
beans.xml(使用set方法)
<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-3.0.xsd">
<bean id="sequenceGenerator" class=" com.apress.springrecipes.sequence.SequenceGenerator">
<property name="prefix">
<value>30</value>
</property>
<property name="suffix">
<value>A</value>
</property>
<property name="initial">
<value>100000</value>
</property>
</bean>
</beans>
beans.xml(使用set方法,简写)
<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-3.0.xsd">
<bean id="sequenceGenerator" class=" com.apress.springrecipes.sequence.SequenceGenerator">
<property name="prefix" value="30"/>
<property name="suffix" value="A"/>
<property name="initial" value="100000"/>
</bean>
</beans>
beans.xml(使用构造器方法)
<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-3.0.xsd">
<bean id="sequenceGenerator" class=" com.apress.springrecipes.sequence.SequenceGenerator">
<constructor-arg>
<value>30</value>
</constructor-arg>
<constructor-arg>
<value>A</value>
</constructor-arg>
<constructor-arg>
<value>100000</value>
</constructor-arg>
</bean>
</beans>
beans.xml(使用构造器方法,简写)
<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-3.0.xsd">
<bean id="sequenceGenerator" class=" com.apress.springrecipes.sequence.SequenceGenerator">
<constructor-arg value="30"/>
<constructor-arg value="A"/>
<constructor-arg value="100000"/>
</bean>
</beans>
beans.xml(p schema),注意:xmlns:p="http://www.springframework.org/schema/p"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.apress.springrecipes.sequence.SequenceGenerator"
p:prefix="30" p:suffix="A" p:initial="100000"/>
</beans>
Main.java方法
package com.apress.springrecipes.sequence;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
SequenceGenerator generator =
(SequenceGenerator) context.getBean("sequenceGenerator");
System.out.println(generator.getSequence());
System.out.println(generator.getSequence());
}
}
控制台输出结果
30100000A
30100001A