SpringIOC容器——参数值注入
基本在注入:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" /> <property name="username" value="jsd1403" /> <property name="password" value="root" /> </bean>
或者下面的写法:
<!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
Bean对象注入:
<bean id="empservice" class="spring.EmpService"> <!-- 构造器注入: <constructor-arg index="0" ref="empdao"></constructor-arg> --> <!--属性注入: <property name="empDao" ref="empdao"></property> --> <constructor-arg index="0"> <bean class="spring.EmpDao"></bean> </constructor-arg> </bean> <bean id="empdao" class="spring.EmpDao"></bean>
EmpService.java编写
public class EmpService { private EmpDao empDao; public EmpService() { System.out.println("------new( )-------"); } public EmpService(EmpDao empDao) { System.out.println("------new( param1 )-------"); this.empDao = empDao; } //getter and setter }
List集合注入
<bean id="msg" class="com.lydia.Message"> <property name="friends"> <list> <value>jack</value> <value>kevin</value> </list> </property> </bean>
Set集合注入:
<bean id="msg" class="com.lydia.Message"> <property name="friends"> <set> <value>jack</value> <value>kevin</value> </set> </property> </bean>
Map注入:
<bean id="msg" class="com.lydia.Message"> <property name="friends"> <map> <entry key="1001" value="kevin"></entry> <entry key="1002" value="Charles"></entry> </map> </property> </bean>
Properties注入:
<bean id="msg" class="com.lydia.Message"> <property name="dbProp"> <props> <prop key="username">root</prop> <prop key="password">root</prop> </props> </property> </bean>
也可以引入Spring再带的标签,不过要先修改applicationContext.xml中的文件协议头,加上util的描述:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
接下来就可以使用了:
<!-- 定义集合bean --> <util:list id="friendList"> <value>jack</value> <value>kevin</value> </util:list> <util:set id="friendSet"> <value>jack</value> <value>kevin</value> </util:set> <util:properties id="dbprop" > <prop key="username">root</prop> <prop key="password">root</prop> </util:properties> <util:map id="friendMap"> <entry key="username" value="root"></entry> <entry key="password" value="root"></entry> </util:map> <!-- 引入上面定义的集合 --> <bean id="msgBean" class="com.tarena.MessageBean"> <property name="someList" ref="friendList"></property> <property name="someSet" ref="friendSet"></property> <property name="someMap" ref="friendMap"></property> <property name="someProps" ref="dbprop"></property> </bean>
注入Spring表达式
<util:properties id="jdbc" location="classpath:db.properties"/> <bean id="mydb" class="org.apache.commons.dbcp.datasources"> <property name="username">#{jdbc.username}</property> <property name="password">#{jdbc.password}</property> </bean
注入null 或 空字符串
Spring将属性的空参数当作空字符串而不是null
<bean id="myBean' class=""> <property name="username' value=""> <bean>
如果注入null值则:
<bean id="myBean" class> <property name="username"> <null/> <property> </bean>
相关推荐
yangkang 2020-11-09
alien 2020-11-15
ChlatZed 2020-11-11
远远的山 2020-11-09
xysoul 2020-11-03
onepiecedn 2020-10-29
lnkToKing 2020-10-29
ChlatZed 2020-10-27
谢恩铭 2020-10-23
yuanye0 2020-10-15
liverlife 2020-10-10
somyjun 2020-09-29
xiaoying 2020-09-28
kunyus 2020-09-25
dusuanyun 2020-09-21
newisI 2020-09-01
afanti 2020-09-14