DI (依赖注入)有二种: Constructor-based dependency injection and Setter-based dependency
a)、Constructor-based DI
com.spring305.test.beanInit.po.InitSingeBean.java
import java.util.Date; public class InitSingeBean { private int id; private Integer age; private String name; private Date birth; private Double weight; private float height; private Boolean sex; public InitSingeBean() { super(); System.out.println(this); } public InitSingeBean(int id, Integer age) { super(); this.id = id; this.age = age; System.out.println(this+"_"+id+"_"+age); } public InitSingeBean(int id, Integer age, String name, Date birth, Double weight, float height, Boolean sex) { super(); this.id = id; this.age = age; this.name = name; this.birth = birth; this.weight = weight; this.height = height; this.sex = sex; System.out.println(this); } public InitSingeBean(int id, Integer age, String name, Double weight, float height, Boolean sex) { super(); this.id = id; this.age = age; this.name = name; this.weight = weight; this.height = height; this.sex = sex; System.out.println(this); } public void test(){ System.out.println(id+"_"+age+"_"+name+"_"+birth+"_"+weight+"_"+height+"_"+sex); } .....getter settter }
Test.java
@Test public void InitSingeBean() { //System.out.println(new Date()); ApplicationContext context = new ClassPathXmlApplicationContext("testBeanInit.xml"); InitSingeBean initSingeBean = (InitSingeBean) context.getBean("initSingeBean"); initSingeBean.test(); }
src/ testBeanInit.XML
<!-- 没有任何参数注入 <bean id="initSingeBean" class="com.spring305.test.beanInit.po.InitSingeBean"> </bean> --> <!-- 构造参数注入,但是,要注意的是这个index(顺序)/type(类型)/value(值)的顺序,而且,这里是不支持自动装箱和拆箱的哦,int Integer,貌似不能自动转 <bean id="initSingeBean" class="com.spring305.test.beanInit.po.InitSingeBean"> <constructor-arg type="int" value="750"/> <constructor-arg type="Integer" value="057"/> </bean> --> <!-- 构造参数注入2 <constructor-arg index="0" value="750"/> <constructor-arg type="int" value="750"/> 2011-04-16 12:00:00 时间的注入不能写成这样子的,这种写法应该是中国cn区的写法吧。 包装类型的要写包装类型,加上包名也是可以的 <bean id="initSingeBean" class="com.spring305.test.beanInit.po.InitSingeBean"> <constructor-arg index="0"><value>750</value></constructor-arg> <constructor-arg type="java.lang.Integer" value="057"/> <constructor-arg type="String" value="00001测试"/> <constructor-arg type="java.util.Date" value="Sat Apr 16 15:20:13 CST 2011"/> <constructor-arg type="java.lang.Double" value="2011.0123"/> <constructor-arg type="float" value="2011.0321"/> <constructor-arg type="java.lang.Boolean" value="true"/> </bean> -->
b)、Setter-based DI
XML
<!-- setter设值注入 <bean id="dateBean" class="java.util.Date"/> 或者 <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <ref bean="dateBean"/> or <bean id="dateBean" class="java.util.Date"/> either is ok--> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="initSingeBean" class="com.spring305.test.beanInit.po.InitSingeBean"> <property name="id" value="23"></property> <property name="age" value="34"></property> <property name="name"><value>jdbc:mysql://localhost:3306/mydb</value></property> <property name="birth"> <!-- <ref bean=dateBean/> --> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2010-01-31" /> </bean> </property> <property name="weight" value="2011.0123"></property> <property name="height" value="23"></property> <property name="sex" value="false"></property> </bean>
<!-- org.apache.commons.dbcp.BasicDataSource 这个怎么样,不过你得先在XML头中加上 xmlns:p="http://www.springframework.org/schema/p" <bean id="initSingeBean" class="com.spring305.test.beanInit.po.InitSingeBean" p:id="123" p:age="345" p:name="dbc:mysql://localhost:3306/mydb" p:birth="Sat Apr 16 15:20:13 CST 2011"/> -->
到底使用哪种来注入?
Spring3官方文档:
<!--EndFragment-->
it is a good rule of thumb to use constructor arguments for mandatory dependencies and setters for optional dependencies。....The Spring team generally advocates setter injection
关于一般属性的注解现没在文档上找到。。。估计那样也没多大意义。。。