一、构造方法注入

定义:通过构造函数来完成依赖关系的设定

优缺点:

在构造对象的同时,完成依赖关系的建立

如果关联的对象很多,那和不得不在构造方法上加入过多的参数

基中有index:如果指定索引从0开始,type用来指定类型

实体类:

package com.pb.entity;
/**
* 班级类
* @author Administrator
*
*/
public class Grade {
private int id; //班级编号
private String name; //班级名称 public Grade() {
super();
// TODO Auto-generated constructor stub
} public Grade(int id, String name) {
super();
this.id = id;
this.name = name;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
package com.pb.entity;
/**
* 学生类
* @author Administrator
*
*/
public class Student {
private String name; //学生名称
private Integer age; //学生年龄
private Grade grade; //所属班级 public Student() {
super();
} public Student(String name, Integer age, Grade grade) {
super();
this.name = name;
this.age = age;
this.grade = grade;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
} }

使用构造方法注入

applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--班级类 -->
<bean id="grade" class="com.pb.entity.Grade">
<!-- 使用构造方法注入 -->
<constructor-arg>
<value>1001</value>
</constructor-arg>
<constructor-arg>
<value>计算应用一班</value>
</constructor-arg> </bean>
<!--学生类 -->
<bean id="student" class="com.pb.entity.Student">
<!-- 使用构造方法注入 -->
<constructor-arg>
<value>张三</value>
</constructor-arg>
<constructor-arg>
<value>23</value>
</constructor-arg>
<!-- 使用ref注入班级bean -->
<constructor-arg ref="grade">
</constructor-arg>
</bean> </beans>

测试类:

package com.pb.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.pb.entity.Student; public class Demo1 { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Student stu = context.getBean("student", Student.class);
System.out.println("学生姓名:" + stu.getName() + "学生年龄:" + stu.getAge()
+ "学生班级编号: " + stu.getGrade().getId() + "学生班级名称: "
+ stu.getGrade().getName()); } }

二、属性注入

其它不变只更改配置文件

applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--班级类 -->
<bean id="grade" class="com.pb.entity.Grade">
<property name="id">
<value>1001</value>
</property>
<property name="name" value="计算机应用一班"></property>
</bean>
<!-- 学生类 -->
<bean id="student" class="com.pb.entity.Student">
<property name="name" value="张三" />
<property name="age" value="18"/>
<property name="grade" ref="grade"/>
</bean>
</beans>

三、注入NULL和空字符串值

<value></value>表示空字符串

<null></null>表示为null值

<bean id="student" class="com.pb.entity.Student">
<!-- 姓名注入空字符串 <value></value>表示空字符串 -->
<property name="name"><value></value></property>
<!--年龄注入为NULL值 -->
<property name="age"><null></null></property>
<property name="grade" ref="grade"/>
</bean>

四、使用p 命名空间注入bean

官方推荐的注入方式

需要在XML上加入

xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--班级类 使用p命名空间注入 -->
<bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外语一班"> </bean>
<!-- 学生类 使用p命名空间注入 -->
<bean id="student" class="com.pb.entity.Student" p:name="张三"
p:age="23" p:grade-ref="grade"> </bean>
</beans>

效果一目了然

五、自动装配

需要使用autowire属性来配置

可以在每个bean中使用autowire来配置

也可以在<beans>中使用autowire全局配置表示这个beans下的都使用自动装配,

缺点:不清晰,有问题比较难以查找

autowire:

no(默认值):不进行自动装配,必须显示指定依赖对象

byName: 根据属性名自动装配。自动查找与属性名相同的id,如果找到,则自动注入,否则什么都不做

byType:根据属性的类型自动装配,Spring自动查找与属性类型相同的Bean,如果刚好找到唯一的那个,则自动注入,如果找到多个与属性类型相同的Bean,则抛出异常,如果没有找到就什么都不做。

constructor:和byType类似,不过它针对构造方法,如果找到一个Bean和构造方法的参数类型相匹配,则通过构造注入该依赖对象,如果找不到,就抛出异常

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--班级类 使用p命名空间注入 -->
<bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外语一班" > </bean>
<!-- 学生类 使用p命名空间注入 -->
<bean id="student" class="com.pb.entity.Student" p:name="张三"
p:age="23" autowire="byName"> </bean>
</beans>

自动装配使得配置文件可以非常简洁,但同时也造成组件之间的依赖关系不明确,容易引发一些潜在的错误,谨慎使用

Spring(四)Bean注入方试的更多相关文章

  1. spring的bean注入扫瞄方法和mybatis的dao bean注入扫描方法

    spring的bean注入扫面方法:@ComponentScan(basePackages = "com.pingan.property.icore.pap.*")mybatis的 ...

  2. 在Spring的Bean注入中,即使你私有化构造函数,默认他还是会去调用你的私有构造函数去实例化

    在Spring的Bean注入中,即使你私有化构造函数,默认他还是会去调用你的私有构造函数去实例化. 如果我们想保证实例的单一性,就要在定义<bean>时加上factory-method=” ...

  3. spring框架bean注入

    今天学习了spring框架,刚刚入门简单的了解了spring并学习了bean的注入IOC:IOC(Inversion of Control,控制反转)不是什么技术,而是一种设计思想.它的目的是指导我们 ...

  4. 学习 Spring (四) Bean 的生命周期

    Spring入门篇 学习笔记 定义 --> 初始化 --> 使用 --> 销毁 初始化 实现 org.springframework.beans.factory.Initializi ...

  5. spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)

    这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...

  6. Spring中bean的四种注入方式

    一.前言   最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...

  7. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  8. Spring之Bean的注入

    Bean的配置中介绍的是Bean声明问题,在哪声明怎么声明的问题.Bean的注入是怎么实例化,怎么注入的问题.Bean注入的方式有两种,一种是在XML中配置,另一种则是使用注解的方式注入. 一.XML ...

  9. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

随机推荐

  1. Javascript this 关键字

    Javascript 的 this 关键字总是指向当前被执行函数的所有者. 换句话说,如果当前函数可以视为某个对象的一个方法,那么 this 就指向该对象. 例如有这么一个函数 doSomething ...

  2. SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...

  3. unity4.6 failed to update unity web player

    unity4.6 failed to update unity web player 新升级的 4.6.2P2 版本修复了IOS很多的bug. 但突然发现导出的Web版本反而不能工作了. “faile ...

  4. Cwinux简介及用法简述

    我在我的个人博客上发表了一篇文章 Cwinux简介及用法简述 http://apprentice89.com/cwinux_introduction_and_use/

  5. Membership三步曲

    http://www.cnblogs.com/jesse2013/p/membership-part1.html http://www.cnblogs.com/jesse2013/p/membersh ...

  6. 缓存池扩展 (Buffer Pool Extension)实践

    SQL Server 2014缓存池扩展 (Buffer Pool Extension)功能可以将缓存池扩展到较快的SSD存储上.为内存比较紧张的系统提供了新的扩展途径. Buffer Pool 扩展 ...

  7. 初涉SQL Server性能问题(3/4):列出阻塞的会话

    在 初涉SQL Server性能问题(2/4)里,我们讨论了列出等待资源或正运行的会话脚本.这篇文章我们会看看如何列出包含具体信息的话阻塞会话清单. /************************ ...

  8. mysql-5.6.14-winx64免安装配置

    MySQL5.6.11安装步骤(Windows7 64位) 1. 下载MySQL Community Server 5.6.14 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录 ...

  9. C#版本的历史

    + 展开目录 - 版本号的一些命名规则 - 语言,运行时,类库,开发工具的区 - 2002年 C#1.0发布 - 2005年 C#2.0发布 - 2007年 C#3.0发布 - 2010年 C#4.0 ...

  10. [CLR via C#]11. 事件

    一. 设计要公开事件的类型 如果类型定义了事件成员,那么类型(或类型实例)就可以通知其他对象发生了一些特定的事情. 例如,Button类提供了一个名为Click的事件.应用程序中的一个或多个对象可能想 ...