Spring IOC(Inversion Of Controll 控制反转)

来源:互联网 发布:美工和设计师的区别 编辑:程序博客网 时间:2024/06/05 10:02

IOC是什么?

对象之间的依赖关系由容器来建立。

DI(Dependency Injection 依赖注入)是什么?

容器通过调用对象提供的set方法或者构造器来建立依赖关系。

注:IOC是目标,DI是手段。

set方式注入

step1.提供相应的set方法。

step2.配置元素。


构造器方式注入

step1.添加相应的构造器。

public A(B b)    {        System.out.println("A(B)");        this.b = b;    }

step2.配置constructor-arg元素。

自动装配(了解)

指的是spring容器依据某种规则,自动建立对象之间的依赖关系。

注意:

a、默认情况下,容器不会自动装配。

b、可以通过指定autowire属性来告诉容器进行自动装配(容器仍然需要通过调用set方法或构造器来完成依赖关系的建立)。

<bean id="wt" class="ioc2.Waiter"/><bean id="wt1" class="ioc2.Waiter"/><!--     autowire属性:表示让容器自动装配,    该属性有如下三个值:    byName:容器依据属性名查找对应的bean然后调用对应的set方法来完成注入。                 注:              a.如果找不到对应的bean注入null。              b.不可能找到多个符合条件的bean。    byType:容器依据属性类型查找对应的bean,然后调用对应的set方法来完成注入。            注:              a.如果找不到对应的bean注入null。              b.有可能找到多个符合条件的bean,此时会出错。    constructor:容器依据属性类型查找对应的bean,然后调用对应的构造器来完成注入。 --><bean id="rest" class="ioc2.Restaurant" autowire="byType"/>

注入基本类型的值

使用value属性即可。

注入集合类型的值(List Set Map Properties)

引用的方式注入集合类型的值

<%@taglib uri=”http//xxxxxx” prefix=”c”%>

uri–>命名空间 namespace

c–>命名空间的别名

<bean id="vb1" class="value.ValueBean">    <property name="name" value="胡八一"/>    <property name="age" value="30"/>    <property name="city">        <list>            <value>北京</value>            <value>上海</value>            <value>杭州</value>        </list>    </property>    <property name="interest">        <set>            <value>钓鱼</value>            <value>看书</value>            <value>听音乐</value>        </set>    </property>    <property name="score">        <map>            <entry key="english" value="90"/>            <entry key="math" value="95"/>        </map>    </property>    <property name="db">        <props>            <prop key="username">Tiger</prop>            <prop key="password">123456</prop>        </props>    </property></bean><!-- 将集合类型的值配置成一个bean --><util:list id="cityBean">    <value>上海</value>    <value>杭州</value>    <value>北京</value></util:list><util:set id="interestBean">    <value>书法</value>    <value>绘画</value>    <value>写作</value></util:set><util:map id="scoreBean">    <entry key="english" value="90"/>    <entry key="math" value="96"/></util:map><util:properties id="dbBean">    <prop key="username">Sally</prop>    <prop key="password">123456</prop></util:properties><!-- 引用的方式注入集合类型的值 --><bean id="vb2" class="value.ValueBean">    <property name="city" ref="cityBean"/>    <property name="interest" ref="interestBean"/>    <property name="score" ref="scoreBean"/>    <property name="db" ref="dbBean"></property></bean><!--     读取properties文件的内容    classpath:按照类路径来搜素。    spring容器会依据路径找到对应的配置文件,然后读取该文件的内容到properties对象。 --><util:properties id="config" location="classpath:config.properties"/>

读取properties文件的内容

<!--     读取properties文件的内容    classpath:按照类路径来搜素。    spring容器会依据路径找到对应的配置文件,然后读取该文件的内容到properties对象。-->    <util:properties id="config" location="classpath:config.properties"/>   

使用spring表达式

可以使用spring表达式读取其他bean的属性,它的语法类似于el表达式。

    <bean id="sp1" class="value.SpelBean">        <property name="name" value="#{vb1.name}"/>        <property name="city" value="#{vb1.city[1]}"></property>        <!-- #{vb1.score['英语']} -->        <property name="score" value="#{vb1.score.english}"></property>        <property name="pageSize" value="#{config.pagesize}"/>    </bean>    <bean id="vb1" class="value.ValueBean">

使用注解简化配置

什么是组件扫描?

spring容器启动之后会扫描指定的包及其子包下面的所有类,如果该类前面有特定的注解(比如@Component),则spring容器会将其纳入容器进行管理。(相当于这儿配置了一个bean元素)。

如何进行组件扫描?

step1.在类前添加特定的注解。



注:除了@Component注解,还有@Service,@Repository,@Controller,作用是等价的,只不过有语义上的差异。

step2.在配置文件当中,添加组件扫描的配置。

<!-- 配置组件扫描 --><!--     base-package属性:指定要扫描的包名,spring容器会扫描该包及其子包下面    的所有类,如果该类前面有特定的注解(比如@Component),则spring容器会将    其纳入容器进行管理。(相当于这儿配置了一个bean元素)。 --><context:component-scan base-package="annotation"></context:component-scan>

其他相关注解

@Scope("singleton")//指定作用域@Lazy(true)//指定延时加载public class SomeBean{    public SomeBean()    {        System.out.println("SomeBean()");    }    @PostConstruct//指定初始化方法    public void init()    {        System.out.println("init()");    }    @PreDestroy//指定销毁方法    public void destroy()    {        System.out.println("destroy()");    }}

依赖注入相关的注解

@Autowired和Qualifier

a.该注解支持set方式注入和构造器方式的注入。

b.当使用set方式注入时,可以将@Autowired添加到set方法前面,如果不使用@Qualifier,则容器会使用byType的方式来注入,有可能出错,所以建议使用@Qualifier注解,明确指出要注入的bean的id。

注:也可以将这两个注解直接添加到属性前。


@Autowiredpublic void setWt(@Qualifier("wt") Waiter wt){    System.out.println("setWt()");    this.wt = wt;}

c.当采用构造器注入时,可以将注解添加到对应的构造器前面即可。

@Autowiredpublic School(@Qualifier("wt")Waiter wt){    System.out.println("School(wt)");    this.wt = wt;}

@Resource注解

a.只支持set方式的注入。
b.可以将该注解添加到属性前,使用name属性指定要注入的bean的id(如果不指定,会按照byType的方式注入)。



注:也可以将注解添加到属性前。

@Value注解

a.可以使用该注解来注入基本类型的值。

b.也可以使用该注解来使用spring表达式。

c.该注解可以添加到属性前,或者添加到对应的set方法前。

@Value("#{config.pagesize}")private String pageSize;@Value("花千骨")private String name;
阅读全文
0 0
原创粉丝点击