Spring学习笔记2
来源:互联网 发布:centos rpm e 编辑:程序博客网 时间:2024/04/29 10:39
依赖注入
1.属性通过依赖注入, 必须在bean中提供set方法
2.接口和bean不能在同一个包中
3.声明bean方法
A. 最常用的
<bean id="personService"
class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBean" scope="prototype" lazy-init="default" destroy-method="destory"></bean>
B.在类PersonServiceBeanFactory里添加createPersonServiceBean方法
使用factory-method
<bean id="personService2"
class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean">
</bean>
C.另一种方法
<bean id="personServiceBeanFactory"
class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBeanFactory"/>
<bean id="personService3"
factory-bean="personServiceBeanFactory" factory-method="createPersonServiceBean3"/>
3.依赖注入:
- 使用setter方法依赖注入bean:
在PersonServiceBean为personDao添加set方法.
A. <bean id="personDao" class="com.bhr.strutsdemo.spring.test.dao.impl.PersonDaoBean"/>
<bean id="personService4" class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
B. <bean id="personService5" class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBean">
<property name="personDao">
<bean class="com.bhr.strutsdemo.spring.test.dao.impl.PersonDaoBean"/>
</property>
</bean>
前一种方式的好处在于personDao单独声明,可以被多个bean使用.
使用setter方法依赖注入其他类型:
<bean id="personDao"
class="com.bhr.strutsdemo.spring.test.dao.impl.PersonDaoBean" />
<bean id="personService4"
class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
<property name="name" value="itcase" />
<property name="set"> //property name与PersonServiceBean里面的get***()方法get后面的名字要相同!
<set>
<value>first</value>
<value>scond</value>
<value>third</value>
</set>
</property>
<property name="list"> //property name与PersonServiceBean里面的get***()方法get后面的名字要相同!
<list>
<value>first</value>
<value>scond</value>
<value>third</value>
</list>
</property>
<property name="properties"> //property name与PersonServiceBean里面的get***()方法get后面的名字要相同!
<props>
<prop key="first"></prop>
<prop key="scond"></prop>
<prop key="third"></prop>
</props>
</property>
<property name="map"> //property name与PersonServiceBean里面的get***()方法get后面的名字要相同!
<map>
<entry key="first" value="first"></entry>
<entry key="sercond" value="sercond"></entry>
<entry key="third" value="third"></entry>
</map>
</property>
</bean>
- 使用构造器
<bean id="personDao"
class="com.bhr.strutsdemo.spring.test.dao.impl.PersonDaoBean" />
<bean id="personService6" class="com.bhr.strutsdemo.spring.test.impl.PersonServiceBean">
<constructor-arg index="0" type="com.bhr.strutsdemo.spring.test.dao.interfac.PersonDao" ref="personDao"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="hello"></constructor-arg> //这里type是基本类型,可以不写,千万不要只写"String", 要写全称,否则会出错
</bean>
- 使用field注入(一般用于注解,最常用,能改善xml文件变臃肿)
注解: 需要导入common-annotation.jar
.......................留空..................
- spring学习笔记2
- Spring学习笔记2
- spring学习笔记(2)
- spring 学习笔记2
- Spring学习笔记(2)
- Spring学习笔记(2)
- spring学习笔记2
- spring学习笔记-2
- Spring学习笔记2
- Spring学习笔记2
- Spring 学习笔记2
- spring学习笔记2
- Spring学习笔记2
- Spring 学习笔记2
- spring学习笔记(2)
- Spring学习笔记(2)
- [2] spring mvc学习笔记
- spring学习笔记(2)
- Ext.Window+Ext.grid.GridPanel
- WINDOWS消息大全
- EXT 方法集锦
- Lucene 基础指南
- Oracle的rownum原理和使用
- Spring学习笔记2
- 一叶偏舟,独临沧海
- CXF简单例子
- 行列变换
- 如何创建Oracle10G EM dbcontrol
- Flex+JAVA+BlazeDS开发环境配置(Java工程和Flex工程独立)
- 仅有一行,输出i到N再到i
- Eclipse中设置代理
- 编译Tiny-x