Spring笔记2---DI

工厂注入:   web

<bean id="xxx" class="x.x.x" factory-method="methodName" /> spring

Bean的做用域 session

<bean id="xxx" class="x.x.x" scope="scope" /> mvc

scope="prototype"---每次都产生一个新的实例 this

scope="singleton" spa

scope="request" 在1次HTTP请求中,每一个bean定义对应一个实例,该做用域仅仅在基于web的spring上下文 prototype

例如springmvc中才有效。 xml

session 在一个HTTP session中,每一个bean定义对应一个实例,该做用域仅仅在基于web的spring上下文中 作用域

例如springMVC中才有效 get

global-session 在一个全局HTTP session中,每一个bean定义对应一个实例,该做用域仅仅在基于portlet

上下文中才有效。

------

Bean的初始化和销毁方法定义

<bean id="xxx" class="x.x.x" init-method="methodName" destroy-method="methodName"/>

经过Beans指定默认的init和destroy

<beans XXX

      default-init-method="methodName" default-destroy-method="methodName">

...

 经过getter和setter注入

<bean id="k" class="x.x.x" >

<property name="a" value="b"/>

<property name="c" ref="beanid"/>

<property name="ins"><bean class="" /> </property><!--内部Bean-->

</bean>

 -----

 

 

<?xml version=”1.0” encoding=”UTF-8”?>

<beans xmlns=http://www.springframework.org/schema/beans

        xmlns:p="http://www.springframework.org/scheme/p"

        xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

        xmnls:aop="http://www.springframework.org/schema/aop"

        Xsi:schemaLocation=”http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd>

<bean id="x" class="" p:song="a" p:i-ref="b" />

 

 装配集合

<list> 装配list类型的值,能够重复

<set>装配set类型的值,不能够重复

<map>装配map类型的值,名称和值能够是任何类型

<props>装配properties类型的值,名称和值必须都是在String类型

 若是参数为Collection<Type> ,List<Ins>或者Ins[] array时,能够

<property name="xxx">

<list>

 <ref bean="a"/>

<ref bean="b" />

<ref bean="c />

</list>

</property>

或者

 

<property name="xxx">

<set>

 <ref bean="a"/>

<ref bean="b" />

<ref bean="c />

</set>

</property>

也能够包含<value><bean><null/> 甚至再嵌套一个<list>

-------

 

<bean id="xxx" class="xxx">

<property name="x">

<map>

<entry key="x" value-ref="x" />

<entry key-ref="x" value-ref="x" />

<entry key="x" value="x" />

<entry key="x" value-ref="x" />

</map>

</property>

</bean>

---------

private Properties instruments;

public void setInstruments(Properties ins){

this.instruments=ins;

此时

<property name="ins>

<props>

 <prop key="xxx">xxx</prop>

<prop key="xxx">xxx</prop>

<prop key="xxx">xxx</prop>

</props>

</property>

---

<property name="x"><null/></property>

相关文章
相关标签/搜索