Ioc 反转控制 反转资源获取的方向

分离接口与实现

采用工厂模式

采用反转控制

 

Di 依赖注入 依赖容器把资源注入

 

配置bean 通过全类名(反射)

配置形式:基于xml方式

Ioc容器的beanFactory&ApplicationContext

依赖注入的方式:属性注入,构造器注入

 

 

Bean必须要有一个无参的构造函数

Class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无参的构造函数

id :bean 的标示,id唯一

 

applicationContext 是spring的ioc容器,是一接口。

其实现类:

ClassPathXmlApplicatContext从类的路径下来加载配置文件

Getbean()

FileSystemXmlApplicationContext

 

属性注入

通过setter方法注入bean的属性值或依赖的对象

属性注入使用<property>元素,使用name属性指定bean的属性名称,value属性或<value>子节点指定属性值。

属性注入是实际应用中最常见的注入方式。

<bean
id="helloWorld"
class="com.test.bean.HelloWorld">

        <property
name="name"
value="spring"></property>

    </bean>

构造函数注入

<!-- 通过构造函数配置bean的属性 -->

    <bean
id="car"
class="com.test.bean.Car">

        <constructor-arg
value="audi"
index="0"></constructor-arg>

        <constructor-arg
value="shanghai"
index="1"></constructor-arg>

        <constructor-arg
value="30000"
index="2"
type="double"></constructor-arg>

    </bean>

    

    <bean
id="car2"
class="com.test.bean.Car">

        <constructor-arg
value="bwm"></constructor-arg>

        <constructor-arg
value="shandong"></constructor-arg>

        <constructor-arg
value="30000"
type="int"></constructor-arg>

    </bean>

使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器!

如果value字面的值中包含特殊字符就可以通过<![CDATA[字符串]]>的形式注入。

我们也可通过value子节点进行配置

 

可以使用property的ref属性建立bean之间的引用关系。

    <bean
id="person"
class="com.test.bean.Person">

        <property
name="name"
value="zhangsan"></property>

        <property
name="age"
value="20"></property>

        <property
name="car"
ref="car2"></property>

    </bean>

 

可以通过一组内置的xml标签来配置集合属性如 list set map

<bean
id="person2"
class="com.test.bean.collect.Person">

        <property
name="name"
value="lisi"></property>

        <property
name="age"
value="20"></property>

        <property
name="cars">

            <list >

                <ref
bean="car"
/>

                <ref
bean="car2"
/>

            </list>

        </property>

    </bean>

 

 

也可通过内部bean来注入属性

 

<bean
id="person2"
class="com.test.bean.collect.Person">

        <property
name="name"
value="lisi"></property>

        <property
name="age"
value="20"></property>

        <property
name="cars">

            <list >

                <ref
bean="car"
/>

                <ref
bean="car2"
/>

                <bean
class="com.test.bean.Car">

                    <constructor-arg
value="ford"></constructor-arg>

                    <constructor-arg
value="济南"></constructor-arg>

                    <constructor-arg
value="20000"
type="int"></constructor-arg>

                </bean>

            </list>

        </property>

 

Map注入

<bean
id="newPerson"
class="com.test.bean.collect.NewPerson">

        <property
name="name"
value="Wangwu"></property>

        <property
name="age"
value="20"></property>

        <property
name="cars">

            <map>

                <entry
key="AA"
value-ref="car"/>

                <entry
key="BB"
value-ref="car2"/>

            </map>

        </property>

    </bean>

 

Properties属性注入

 

    <bean
id="dataSource"
class="com.test.bean.collect.DataSource">

        <property
name="properties">

            <props>

                <prop
key="user">root</prop>

                <prop
key="password">root</prop>

                <prop
key="jdbcUrl">jdbc:mysql:///test</prop>

                <prop
key="driverClass">com.mysql.jdbc.Driver</prop>

            </props>

        </property>

    </bean>

 

 

 

 

 

 

 

 

 

 

 

Spring笔记②--各种属性注入的更多相关文章

  1. 这篇文章,我们来谈一谈Spring中的属性注入

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...

  2. Spring学习笔记(二)——Spring相关配置&属性注入&Junit整合

    一.Spring的相关配置 1.1 Bean元素 class属性:被管理对象的完整类名 name属性:给Bean起个名字,能重复,能使用特殊字符.后来属性 id属性:给Bean起个名字,不能重复,不能 ...

  3. Spring框架的属性注入

    1. 对于类成员变量,常用的注入方式有两种 * 构造函数注入(没有空的构造方法注入) * 属性setter方法注入(有空的构造方法注入) 2. 在Spring框架中提供了前两种的属性注入的方式 1. ...

  4. Spring - IoC(2): 属性注入 & 构造注入

    依赖注入是指程序运行过程中,如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...

  5. Spring为某个属性注入值或为某个方法的返回值

    项目中用到需要初始化一些数据,Spring提供了filed的值注入和method的返回值注入. 一.Field值的注入 filed值注入需要使用org.springframework.beans.fa ...

  6. Spring入门_02_属性注入

    Spring 的set方法(属性)注入 UserAction类中设置属性和get.set方法.(实际上只需要set方法) private List list = null; private Set s ...

  7. Spring中的属性注入注解

    @Inject使用 JSR330规范实现的 默认按照类型注入 如果需要按照名称注入,@Inject需要和@Name一起使用 @Resource JSR250规范实现的,需要导入不同的包 @Resour ...

  8. Spring.net页面属性注入

    .条件spring.web程序集 1.1 system.web配置 <httpHandlers> <add verb="*" path="*.aspx& ...

  9. 【Java Web开发学习】Spring构造器和属性注入

    测试类 public class Construct { private String address; private long phone; public Construct(String nam ...

随机推荐

  1. block本质探寻七之内存管理

    说明: <1>阅读本问,请参照block前述文章加以理解: <2>环境:ARC: <3>变量类型:基本数据类型或者对象类型的auto局部变量: 一.三种情形 //代 ...

  2. mysql获取随机题目、排序

    mysql排序问题(对字符串类型数据进行排序)对普通数字字符串字段排序:select * from qq ORDER BY score*1 DESC,time*1 ASC 一.在mysql操作中我们经 ...

  3. c语言输出控制符

    c语言格式输出 %d 10进制 %f 浮点型输出 %lf 长浮点型输出 %c 字符输出 %s 字符串输出 %o 八进制输出 %x 十六进制输出 %p 16进制,一般输出地址 %e 科学计数法输出 %m ...

  4. scala 读取文件加下的指定文件

    1,获取指定类型文件 def getFile(file:File): Array[File] ={ val files = file.listFiles().filter(! _.isDirector ...

  5. 补交20145226蓝墨云班课 -- MyCP

    蓝墨云班课 -- MyCP.java 具体描述: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt ...

  6. Vue 技巧

    1.在 v-html 中执行 vue 绑定的事件,默认是不能执行的.这里需要把 html 重新解析一下 loadMsg:function(html){ html = $.parseHTML(html) ...

  7. [Deep-Learning-with-Python]神经网络入手学习[上]

    神经网络入手[上] [x] 神经网络的核心部分 [x] Keras介绍 [ ] 使用Keras解决简单问题:分类和回归 神经网络剖析 神经网络的训练与下列对象相关: 网络层Layers,网络层结合形成 ...

  8. 柯朗微积分与数学分析习题选解(1.2 节 d)

    一直在读<陶哲轩实分析>,陶的书非常的严谨,环环相扣,但是也有个缺点就是计算性的例子和应用方面的例子太少了.所以就又找了本柯朗的<微积分与数学分析>搭配着看.柯朗的书的习题与陶 ...

  9. git删除所有提交历史记录

    这种方式是最快最有效的 进项目根目录启动git bash,然后执行这些即可 最后的 git push -f origin master 会失败,直接在idea里push就能成功了 .Checkout ...

  10. windows10如何将python2和python3添加到环境变量中

    点击我的电脑----->右键‘属性’----->高级系统管理-------->高级-------->环境变量------>新建------->此时输入变量名和变量值 ...