package onlyfun.caterpillar;
public class HelloBean {
private String name;
private String helloWord;
public HelloBean() {
}
public HelloBean(String name, String helloWord) {
this.name = name;
this.helloWord = helloWord;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setHelloWord(String helloWord) {
this.helloWord = helloWord;
}
public String getHelloWord() {
return helloWord;
}
}
<?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="helloBean"
class="onlyfun.caterpillar.HelloBean">
<constructor-arg index="0">
<value>Justin</value>
</constructor-arg>
<constructor-arg index="1">
<value>caterpillar</value>
</constructor-arg>
</bean>
</beans>
package onlyfun.caterpillar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello =
(HelloBean) context.getBean("helloBean");
System.out.print("name: ");
System.out.println(hello.getName());
System.out.print("word: ");
System.out.println(hello.getHelloWord());
}
}
资 讯: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@12b6651] 2005/10/17 下午 09:08:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [helloBean]; root of BeanFactory hierarchy] name: Justin word: caterpillar
这边的例子在Bean上使用具有两个参数的建构函式作范例,如果建构函式上只有一个参数,则不必指定index属性,例如建构函式上若只有一个name参数,则可以在Bean定义档中如下设定:
... <bean ...> <constructor-arg> <value>Justin</value> </constructor-arg> </bean> ...另一个例子是若有两个以上的参数,而参数型态各不相同的话,例如若HelloBean是这么定义的:
package onlyfun.caterpillar;
public class HelloBean {
private String name;
private Integer age;
public HelloBean() {
}
public HelloBean(String name, Integer age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
}
<?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="helloBean"
class="onlyfun.caterpillar.HelloBean">
<constructor-arg type="java.lang.String">
<value>Justin</value>
</constructor-arg>
<constructor-arg type="java.lang.Integer">
<value>20</value>
</constructor-arg>
</bean>
</beans>
package onlyfun.caterpillar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello =
(HelloBean) context.getBean("helloBean");
System.out.print("name: ");
System.out.println(hello.getName());
System.out.print("word: ");
System.out.println(hello.getAge());
}
}
... name: Justin word: 20 ...至于要使用Constructor或Setter来完成依赖注入这个问题,其实就等于在讨论一个古老的问题,要在对象建立时就准备好所有的资源,或是在对象建立好后,使用Setter来进行设定。