(一)设值注入就是指要被注入的类中定义有一个setter()方法,并在参数中定义需要注入的对象。简单的看个例子。

建一个User类:

package com.ioc;

public class User {
private String name ;
private int age ;
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;
}
public String printInfo(){
return "name : "+ name +" ; age : "+age;
}
}

spring-ioc.xml配置(具体取名随意,保持调用时一致即可)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="user" class="com.ioc.User">
<property name="name" value="Json" />
<property name="age" value="25" />
</bean> </beans>

测试:

package com.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpingTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-ioc.xml") ;
User user = (User)context.getBean("user") ;
System.out.println(user.printInfo()) ; }
}

PS:这里用到了单元测试,稍后会做讲解
输出结果:

2015-6-26 14:50:08 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7d26f75b: startup date [Fri Jun 26 14:50:08 CST 2015]; root of context hierarchy
2015-6-26 14:50:08 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-ioc.xml]
name : Json ; age : 25

(二)构造注入就是指要被注入的类中声明一个构造方法,并在此方法的参数中定义要注入的对象。

修改一下User类:

package com.ioc;

public class User {
private String name ;
private int age ;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String printInfo(){
return "name : "+ name +" ; age : "+age;
}
}

spring-ioc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="user" class="com.ioc.User">
<constructor-arg name="name" value="Json" />
<constructor-arg name="age" value="22" />
</bean>
</beans>

测试:

package com.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpingTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-ioc.xml") ;
User user = (User)context.getBean("user") ;
System.out.println(user.printInfo()) ; }
}

结果:

2015-6-26 15:05:53 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Fri Jun 26 15:05:53 CST 2015]; root of context hierarchy
2015-6-26 15:05:53 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-ioc.xml]
name : Json ; age : 22

 PS:Construcotrer注入和setter注入不同的XML写法方式

constructor注入4种不同写法

第1种方法:直接传值:

<!-- constructor方式注入写法1,直接传值 -->
<bean id="user" class="com.ioc.User">
<constructor-arg value="Json" />
<constructor-arg value="22" />
</bean>

ps:直接给参数赋值,这种方法也是根据顺序排的,所以一旦调换位置的话,就会出现bug,这种方法已经很原始了。

第2种方法:根据索引赋值,索引都是以0开头的:

<!-- constructor方式注入写法2,根据索引赋值 -->
<bean id="user" class="com.ioc.User">
<constructor-arg index="0" value="Json" />
<constructor-arg index="1" value="22" />
</bean>

第3种方法:是根据所属类型传值:

 <!-- constructor方式注入写法3,根据所属类型传值 -->
<bean id="user" class="com.ioc.User">
<constructor-arg type="String" value="Json" />
<constructor-arg type="int" value="22" />
</bean>

ps:这种方法基本上不怎么适用,因为一个类里可以有好几个相同基本类型的变量,很容易就混淆值传给哪一个参数了所以最好不要使用这种方法。

第4种方法:根据参数的名字传值:(推荐用法)

<!-- constructor方式注入写法4,根据参数的名字传值:记得传入名是构造器的参数名(推荐用法) -->
<bean id="user" class="com.ioc.User">
<constructor-arg name="name" value="Json" />
<constructor-arg name="age" value="22" />
</bean>

setter注入3种不同写法

<!-- setter方式注入写法1,记得要有无参构造函数 -->
<bean id="user" class="com.ioc.User">
<property name="name">
<value>json</value>
</property>
<property name="age">
<value>22</value>
</property>
</bean>
<!-- setter方式注入写法2,记得要有无参构造函数 -->
<bean id="user" class="com.ioc.User">
<property name="name" value="Json" />
<property name="age" value="22" />
</bean>
<!-- setter方式注入写法7,记得要有无参构造函数  -->
<bean id="user" class="com.ioc.User"
p:name="Json" p:age="22" />

PS:推荐用第2种或第3种,第1种要写的代码比较多,看直来也没那么顺。

Spring学习(3)---Spring设值注入和构造注入的更多相关文章

  1. Spring基于构造函数和设值函数的依赖注入

    基于构造函数的依赖注入 我们知道,bean标签中指定的类会进行初始化,这个初始化过程中自然会调用构造函数,那我们也可以利用这个构造函数完成依赖注入. 先创建一个类: public class Text ...

  2. Spring注入值得2种方式:属性注入和构造注入

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  3. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  4. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  5. spring--设置注入VS构造注入

    1.在传统的程序设计中,调用亲自创建被调用者的实例,即由程序控制“对象之间的依赖关系”,这种方式的耦合度比较高:控制反转就是将由程序控制的“对象间的依赖关系”转交给Ioc容器来进行控制,被调用者的实例 ...

  6. 【Spring学习笔记-2.1】Spring的设值注入和构造注入

    设值注入: 先通过无参数的构造函数创建一个Bean实例,然后调用对应的setter方法注入依赖关系: 配置文件: <?xml version="1.0" encoding=& ...

  7. Spring接口编程_设值注入和构造注入

    说明: UserManagerImp是设值注入,UserManagerImp2是构造注入 接口不注入,也就是在Spring配置文件中没有接口的<bean>,但是定义的时候是用接口 priv ...

  8. 【Spring实战】—— 5 设值注入

    本篇主要讲解了Spring的最常用的功能——依赖注入. 注入的方式,是使用Getter Setter注入,平时大多的编程也都是使用这种方法. 举个简单的例子,还是表演者. 表演者有自己的属性,年龄或者 ...

  9. Spring学习笔记——Spring依赖注入原理分析

    我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...

随机推荐

  1. AspNet.OData 协议概述

    1. 什么是OData? OData 全称 Open Data Protocol,字面理解为开放数据协议,是一个基于Http协议且API实现为Restful风格的协议标准,目前由微软支持大力推广,你可 ...

  2. bootstrap快速入门笔记(七)-表格,表单

    一,表格 1,<table>中加.table类 2,条纹表格:通过 .table-striped 类可以给 <tbody> 之内的每一行增加斑马条纹样式. **跨浏览器兼容性: ...

  3. Git操作指南

    请访问以下网址,很详细,今天偷个懒记录一下,之后有时间再来补全吧! https://git-scm.com/book/zh/v2

  4. C++STL中map容器的说明和使用技巧(杂谈)

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  5. NodeJS+Express+MongoDB 简单实现数据录入及回显展示【适合新人刚接触学习】

    近期在看NodeJS相关 不得不说NodeJS+Express 进行网站开发是很不错,对于喜欢玩JS的来说真是很好的一种Web开发组合 在接触NodeJS时受平时Java或者C#中API接口等开发的思 ...

  6. ng-options语法详解

    我们先看下options的这条语句 ng-options="value.id as value.label group by value.group for value in myOptio ...

  7. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  8. 栈的Java简单实现

    关于栈 栈(Stack)是限定只能在一段进行插入和删除操作的线性表. 进行插入和删除操作的一端称为“栈顶”(top),另一端称为“栈底”(bottom). 栈的插入操作称为“入栈”(push),栈的删 ...

  9. Rabin-Karp字符串查找算法

    1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...

  10. Swift学习笔记(4):字符串

    目录: 初始化 常用方法或属性 字符串索引 初始化 创建一个空字符串作为初始值: var emptyString = "" // 空字符串字面量 var anotherEmptyS ...