读书人

Spring依托属性注入和构造函数注入

发布时间: 2012-10-07 17:28:51 作者: rapoo

Spring依赖属性注入和构造函数注入

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!-- init-method:注册初始化函数,destroy-method:注册消亡函数 -->
<bean id="conf" init-method="init" destroy-method="destroy">

<!-- 构造函数注入值? index:表明在构造函数里面的参数序号 需要知道参数的 类型,不需知道参数的名字-->
<constructor-arg index="0" type="java.lang.String">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</constructor-arg>

<constructor-arg index="1" type="java.lang.String">
<value>jdbc:odbc:mysql//localhost:3306/db</value>
</constructor-arg>

<!-- 属性注入 只需要知道属性名,而不需知道函数类型 -->
<!--?
<property name="driverclass">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:mysql//localhost:3306/db</value>
</property>
-->
</bean>

</beans>

?

读书人网 >软件架构设计

热点推荐