Spring--IOC

来源:互联网 发布:matlab矩阵求转置 编辑:程序博客网 时间:2024/06/06 03:39

1、Spring 生命周期Scope

<bean id=""  class=""  scope=“”></bean>

singleton  (默认) 单例模式

prototype   每次取都是新的

以下三种与web使用

    request

session

globalsession

2、集合注入

<bean id=""  class="" >

<property name="">相当于调用set方法

<props>

<prop key="">...</prop>

<prop key="">...</prop>

<prop key="">...</prop>

</props>

</property>

</bean>


<bean id=""  class="" >

<property name="">相当于调用set方法

<list>

<value>...</value>

<ref bean=""/>

</list>

</property>

</bean>


<bean id=""  class="" >

<property name="">相当于调用set方法

<map>

<entry>

<key>

<value>...</value>

</key>

<value>....</value>

</entry>

<entry>

<key>

<value>...</value>

</key>

<value>....</value>

</entry>


</map>

</property>

</bean>


3、自动装配 Autowire

<bean id=""  class=""  scope=“”  autowire=""></bean>

byName   按照名字自动匹配

byType     按照类型自动匹配

default

4、注解

<context:annotation-config /> 初始化4个bean


@Autowired  对应类型的自动注入(默认byType) 位于setter方法上面       如果有两个一样的类型的beanid  可用@Qualifier(“beanID”)指定是哪个

@Resource 位于setter方法上面 (默认byName) 引用资源         按名称@Resource(name=“”)    推荐使用该注解


<context:component-scan base-package="org.example"/>

@Commponent      通过@Resource引入  相当于xml中的被引用的beanid所属的对象


@Scope

初始化:@PostConstruct=init-method

@PreDestroy=destroy-method



0 0
原创粉丝点击