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. 265. Paint House II 房子涂色K种选择的版本

    [抄题]: There are a row of n houses, each house can be painted with one of the k colors. The cost of p ...

  2. DOM 删除HTML元素

    常用方法: <script> var parent=document.getElementById("div1"); var child=document.getEle ...

  3. Condition Variable使用及其Thread Cancellation线程取消

    条件变量Condition Variable的一般用法: 唤醒用法: struct { pthread_mutex_t mutex; pthread_cond_t cond; //whatever v ...

  4. python动态捕获异常-乾颐堂

    在讨论动态捕获异常时让我大吃一惊的是,可以让我找到隐藏的Bug和乐趣... 有问题的代码 下面的代码来自一个产品中看起来是好的抽象代码 - slightly(!) .这是调用一些统计数据的函数,然后进 ...

  5. centos6.5修改root密码

    转:https://blog.csdn.net/cui1834515/article/details/77860113

  6. MDK5使用技巧

    1.文本美化 修改 修改字体以及颜色如下: 修改用户自定义的关键字,如下: 代码编辑技巧 1.TAB键的妙用 使用TAB键可以整体向右移动相应位,使用SHIFT+TAB键整体左移相应位. 2.快速定位 ...

  7. Page Object页面设计模式核心要点

      Page Object,页面对象.一种设计模式,实施selenium的最佳实践,体现了web应用与页面显示之间的关系.为什么需要Page Object?测试代码维护的需要:减少代码的编码量,减少代 ...

  8. 8-5 Navicat工具与pymysql模块

    一 Navicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 需要掌握的基本操作 ...

  9. LDA详解

    PART 1 这个性质被叫做共轭性.共轭先验使得后验概率分布的函数形式与先验概率相同,因此使得贝叶斯分析得到了极⼤的简化.   V:文档集中不重复的词汇的数目 语料库共有m篇文档,: 对于文档,由个词 ...

  10. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...