spring基础

2017-05-08  本文已影响0人  岁月是首歌

使用spring

1.导入spring价包
2.配置applocationContext文件(配置bean)

 <bean id="iUserDao" class="com.dao.UserDaoImpl"></bean>

3.调用bean

            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            UserDaoImpl bean = (UserDaoImpl)ac.getBean("iUserDao");

简称

IOC:依赖性注入,也被称作控制反转(Inversion Of Control,IOC),即使用 Spring之类的框架在运行的时候(而不是在编译的时候)链接对象的依赖关系。

DI:依赖注入(Dependency Injection) :用一个单独的对象(装配器)来装配对象之间的依赖关系 。

依赖注入

配置文件

<property name="userDao" ref="iUserDao"></property> //通过属性注入
                                                       要有set方法
<constructor-arg>
      <ref bean="iUserDao"/>
</constructor-arg>                 //通过构造方法注入,要有构造方法

<bean id="service" class="com.service.Service" p:userDao-ref="iUserDao">            //通过工作空间注入,需要set方法


default-autowire="byType" //自动装配,需要set方法

手动装配优先级高于自动装配

注入特殊字符

<value><![CDATA[特殊字符]]></value>

注入map

<property name="map">
            <map>
                <entry>
                    <key>
                        <value>浙江</value>
                    </key>
                    <value>杭州</value>
                </entry>
            </map>
        </property>
上一篇 下一篇

猜你喜欢

热点阅读