Spring支持3种依赖注入方式,分别为属性注入、构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解。

1、常量注入

  属性注入是通过setter方法注入Bean的属性值,属性注入使用<property>元素,使用name属性指定Bean的属性名称,使用value属性或者<value>子节点指定属性值。

beans.xml文件主要内容

<!--通过属性注入方式配置  -->
<bean id="person" class="com.spring.Person">
<property name="name" value="Tom"></property>
<property name="age" value="29"></property>
</bean>

Person.java

package com.spring;
public class Person {
private String name;
private int age;
private double height;
private double weight; public Person(String name, int age, double height, double weight) {
super();
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
} public Person() {
super();
} public double getHeight() {
return height;
} public void setHeight(double height) {
this.height = height;
} public double getWeight() {
return weight;
} public void setWeight(double weight) {
this.weight = weight;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", height=" + height + ", weight=" + weight + "]";
} }

Main.java

package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}

运行结果截图如下:

以上就是属性注入的例子

2、构造器注入

2.1 按照索引匹配

beans.xml

<bean id="person1" class="com.spring.Person">
<constructor-arg value="Tim" index="0"></constructor-arg>
<constructor-arg value="30" index="1"></constructor-arg>
<constructor-arg value="30" index="3"></constructor-arg>
<constructor-arg value="65" index="2"></constructor-arg> </bean>

结果:

2.2 按类型匹配

beans.xml

 <bean id="person2" class="com.spring.Person">
<constructor-arg value="34" type="int"></constructor-arg>
<constructor-arg value="175" type="double"></constructor-arg>
<constructor-arg value="Jerry" type="java.lang.String" ></constructor-arg>
<constructor-arg value="70" type="double"></constructor-arg>
</bean>

结果:

Spring注入方式(1)的更多相关文章

  1. Spring注入方式及用到的注解

    注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ...

  2. Spring课程 Spring入门篇 2-2 Spring注入方式

    课程链接: 本节主要讲了以下两块内容: 1 xml两种注入方式 2 注入方式代码实现 3 特别注意 1 xml两种注入方式 构造注入和set注入 2 注入方式代码实现 2.1 set注入方式的实现 实 ...

  3. 【原创】Spring 注入方式

    Spring 强烈推荐注解在构造器上,且对于不能为null的字段或者属性都用断言. 1. 设值注入 原理:通过setter方法注入 XML配置方式:bean下的property标签,用value指定基 ...

  4. Spring注入方式及注解配置

    一:基于xml的DI(Dependency Injection) 注入类型: 定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car Student ...

  5. Spring源码学习之:你不知道的spring注入方式

    前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...

  6. Spring注入方式(2)

    3.引用其他bean Bean经常需要相互协作完成应用程序的功能,bean之间必须能够互相访问,就必须在bean配置之间指定对bean的引用,可以通过节点<ref>或者ref来为bean属 ...

  7. 一个接口多个实现类的Spring注入方式

    1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...

  8. Spring注入方式

  9. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

随机推荐

  1. VC获取网页标题,解决乱码问题 学习

    博主不让转载 仅记录下地址 稍后 放出自己的代码 http://blog.csdn.net/friendan/article/details/11821135

  2. ResourceUtils读取properties文件

    注意: properties文件要放在classPath下面,也就是与src下. path.properties(在properties文件中\代表着没有完,下行同本行是一个内容) perfectMa ...

  3. Web Api 中返回JSON的正确做法(转)

    出处:http://www.cnblogs.com/acles/archive/2013/06/21/3147667.html 在使用Web Api的时候,有时候只想返回JSON:实现这一功能有多种方 ...

  4. swoole多进程操作

    多个任务同时执行 将顺序执行的任务,转化为并行执行(任务在逻辑上可以并行执行) 比如,我们要对已知的用户数据进行判断,是否需要发送邮件和短信,如果需要发送则发送. 不使用多进程时,我们首先判断是否发送 ...

  5. java 传参数时 类型后跟 3个点 "..." 的意义

    对照代码和运行结果便知"..." 的意义 import java.util.ArrayList; public class StringDemo { public static v ...

  6. 使用Student T'test方法做性能测试

    性能测试 日常工作中对比函数间的快慢时,最直接的方法就是根据timer:tc/1结果的时间来衡量,比如想知道lists:reverse/1与直接使用自己写的尾递归函数谁更快?最直接的方法就是 -mod ...

  7. TSQL--自增键和索引对表的插入效率测试

    问题描述:数据库操作中,需要对表频繁插入数据,但发现插入效率不高. 解决思路:将数据缓存在服务器上,积攒到一定条数后批处理发送到数据库,在插入时考虑并发和索引对插入效率的影响. 测试结果:      ...

  8. Solr相似度算法二:Okapi BM25

    地址:https://en.wikipedia.org/wiki/Okapi_BM25   In information retrieval, Okapi BM25 (BM stands for Be ...

  9. 「ZJOI 2010」 排列计数

    题目链接 戳我 \(Solution\) 其实我们可以发现这题等价于让你求: 用\(1\)~\(n\)的数组成一个完全二叉树使之满足小根堆性质的方案数 于是我们可以考虑\(dp\) 假设我们现在在\( ...

  10. 激活xmind的方法

    https://blog.csdn.net/qq_38238041/article/details/81107127 这里以windows为例来演示,其它操作系统需根据情况修改相应步骤. 下载安装包 ...