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. docker 容器创建参数错误记录

    sudo docker ps -a -q sudo docker ps -a|cutawk '{print $1}' #删除前八条 sudo docker ps -a -q|head -n |xarg ...

  2. requests.session之set trust_env to disable environment searches for proxies

    import requests s = requests.Session() s.trust_env = False This will prevent requests getting any in ...

  3. git忽略某个文件夹

    data/cache/* !data/cache/index.html !data/cache/smiOAuthToken.php

  4. redhat6.7在线安装postgresql9

    原文:http://wandejun1012.iteye.com/blog/2015777 1.安装postgresql9.0 yum 仓库 rpm -i http://yum.postgresql. ...

  5. linux安装mysql后root无法登录

    [root@localhost mysql]# mysql -u root -pEnter password: ERROR 1045 (28000): Access denied for user ' ...

  6. C/C++编程可用的Linux自带工具

    GNU Binary Utilities或binutils是一整套的编程语言工具程序,用来处理许多格式的目标文件.当前的版本原本由在Cygnus Solutions的程序员以Binary File D ...

  7. (匹配 Hopcroft-Karp算法)Rain on your Parade -- Hdu --2389

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2389 不能用匈牙利,会TEL的,用Hopcroft-Karp Hopcroft-Karp课件 以前是寻找 ...

  8. 动态规划 HDU1231-------最大连续子序列

    Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...

  9. 汉诺塔问题的算法分析与实现(Java)

    汉诺塔问题是源于印度一个古老传说的益智玩具.要求将圆盘从A柱移动到C柱规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘. 可以先通过3个盘子的hanoi游戏得出其算法步骤如下: if ...

  10. MySQL—练习2

    参考链接:https://www.cnblogs.com/edisonchou/p/3878135.html   感谢博主 https://blog.csdn.net/flycat296/articl ...