当前位置: 代码迷 >> Web前端 >> Spring 统制反转IOC(依赖注入,)
  详细解决方案

Spring 统制反转IOC(依赖注入,)

热度:515   发布时间:2012-09-22 21:54:54.0
Spring 控制反转IOC(依赖注入,)

<?xml version="1.0" encoding="gb2312"?>

<!-- default-lazy-init="true" 为beans下所有的进行延迟初始化 -->
<beans xmlns="http://www.springframework.org/schema/beans"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xmlns:aop="http://www.springframework.org/schema/aop"
??? xmlns:tx="http://www.springframework.org/schema/tx"
??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
?????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
?????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
?????? <!-- id 不能是特殊字符,如:“/”,如果有特殊字符则应该用name。????? 交给Spring管理的Bean类-->
??????
?????? <!--默认情况下(没有配置scope属性)是单实例? 指的是每次获取Bean都是同一个对象? 初始化容器时候就实例化实例化 PersonService
?????? ?? 可是使用lazy-init="true" 进行延迟初始化 ,即第一次获取bean的时候才初始化
?????? ??? scope="prototype"每次获取Bean都是新的对象?? 第一次请求(调用getBean)的时候实例化 PersonService? .
?????? ??? init-method="" 初始化 方法。即当bean被初始化后接下来会继续执行的方法。
?????? ??? destroy-method=""? -->
?????? ???
????? <!--? <bean id="personService" class="com.milo.services.impl.PersonServiceBean"?
?????? ??lazy-init="false" init-method="init" destroy-method="destory" scope="prototype"/>???? -->
??????
??????
?????? <!-- 使用 类构造器方法??????? 实例化 PersonService???
??????? <bean id="personService1" class="com.milo.services.impl.PersonServiceBean" scope="prototype"/>
??????
?????? 使用 静态工厂方法??????? 实例化PersonService?
?????? <bean id="personService2" class="com.milo.services.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/>
??????
?????? 使用 实例化工厂方法?? 实例化PersonService?
?????? <bean id="personServiceFactory" class="com.milo.services.impl.PersonServiceBeanFactory"/>
?????? <bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"/>
??-->
??
??
??<!-- 依赖对象注入?? 可以被多个Bean调用-->
??<bean id="personDao" class="com.milo.dao.impl.PersonDaoBean"></bean>? <!-- 默认被实例化 -->
??<bean id="personService" class="com.milo.services.impl.PersonServiceBean" >
??? <!-- ref="personDao" 注入实例 -->
???<property name="personDaoTest" ref="personDao"></property>
???
??? <!--对基本类型的注入? -->
???<property name="name" value="milo.yang"></property>
???<property name="id" value="88"></property>
???
???<!-- 对集合类的注入操作 -->
???<property name="sets">
????<set> ????<!-- Set 集合注入 -->
?????<value>第1个set</value>
?????<value>第2个set</value>
?????<value>第3个set</value>
????</set>
???</property>
???<property name="lists">
????<list>????<!-- List 集合注入 -->
?????<value>第1个list</value>
?????<value>第2个list</value>
?????<value>第3个list</value>
????</list>
???</property>
???<property name="props">
????<props>????<!-- Properties 集合注入 -->
?????<prop key="propsKey1">第1个properties</prop>
?????<prop key="propsKey2">第2个properties</prop>
?????<prop key="propsKey3">第3个properties</prop>?
????</props>
???</property>
???<property name="maps">
????<map>????<!-- Map 集合注入 -->
?????<entry key="mapsKey1" value="第1个maps"></entry>
?????<entry key="mapsKey2" value="第2个maps"></entry>
?????<entry key="mapsKey3" value="第3个maps"></entry>
????</map>
???</property>
???
???
??</bean>
??
??<!-- 也可以采用内部Bean的方式注入对象
???但不可以为其他Bean调用,只能被PersonServiceBean使用。
??<bean id="personService" class="com.milo.services.impl.PersonServiceBean" >
???<property name="personDaoTest" >
????<bean class="com.milo.dao.impl.PersonDaoBean"/>
???</property>
??</bean>
?? -->
??
??
</beans>

?

?

?

?

?

?

?

?

?

?

  相关解决方案