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. 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

  2. centos7之saltstack安装

    查阅来自salt官网:http://docs.saltstack.cn/topics/installation/rhel.html To install using the SaltStack rep ...

  3. 如何规范移动应用交互设计?UI/UX设计师须知的11个小技巧

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 十年前,手机的使用只是为了沟通. 而近几年,情况发生了很大变化,我们很难找到不使用手机的人.手机在极 ...

  4. swift - 歌曲列表动画

    // //  ViewController.swift //  songAnimation // //  Created by su on 15/12/10. //  Copyright © 2015 ...

  5. Google Tango service outdated谷歌Tango的服务过时了

    If you device showed "tango service outdated." It means that your Tango Core need to be up ...

  6. ubuntu 安装 zend studio

    hi,everyone!2014 年到了,是20你死还是爱你一世,世人不得而知.夜观天象,道德依旧在沦丧,经济依然在滑坡.行了,就整这几句.最近在折腾linux,这篇文章,没有什么意义.只是找找写bl ...

  7. CURL命令测试网站打开速度

    curl -o /dev/null -s -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total} http: ...

  8. Selenium窗口切换-----Selenium快速入门(六)

    有时候,我们打开多个窗口,进行多窗口操作,那么窗口间该如何切换呢? 切换的方法有两个,一个是通过窗口标题来验证,另一个是通过窗口特定的内容来验证,这两个方法都要求得到的标题或内容是唯一的. 用到的相关 ...

  9. JS三个编码函数和net编码System.Web.HttpUtility.UrlEncode比较

    JS三个编码函数和net编码比较 总结 1.escape.encodeUri.encodeUriComponent均不会对数字.字母进行编码.2.escape:对某些字符(如中文)进行unicode编 ...

  10. Visual Studio效率神器——超级扩展ReSharper安装和破解

    Visual Studio效率神器--超级扩展ReSharper安装和破解   ReSharper的使用方法网络上有文章Resharper安装和破解极其简单,2分钟就搞定了.安装ReSharper官方 ...