Spring入门(2)-通过构造器注入Bean

前一篇文章将了最基本的spring例子,这篇文章中,介绍一下带有参数的构造函数和通过构造器注入对象引用。

0. 目录

  1. 带有参数的构造函数
  2. 通过构造器注入对象引用
  3. 通过工厂方法创建Bean

1. 带有参数的构造函数

把需要实例化的类中增加一个有参数的构造函数

package com.chzhao.springtest;

public class PersonBll implements IPersonBll {

	private Person person;

	public PersonBll() {

	}

	public PersonBll(String name) {
this.person = new Person();
this.person.setName(name);
} public void show() {
if (this.person != null) {
System.out.println(this.person.getName());
} else {
System.out.println("no person");
}
} public void addPerson(Person p) {
System.out.println("add person: " + p.getName());
}
}

配置文件

<?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.xsd">
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
<constructor-arg value="老王"></constructor-arg>
</bean>
</beans>

通过这种方式,可以把参数传入到构造函数中。

2. 通过构造器注入对象引用

把构造函数改为引用类型,代码如下:

package com.chzhao.springtest;

public class PersonBll implements IPersonBll {

	private Person person;

	public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} public PersonBll() { } public PersonBll(Person p) {
this.person = p;
} public void show() {
if (this.person != null) {
System.out.println(this.person.getName());
} else {
System.out.println("no person");
}
} public void addPerson(Person p) {
System.out.println("add person: " + p.getName());
}
}

同时配置文件改为

<?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.xsd">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
<constructor-arg ref="laowang"></constructor-arg>
</bean>
</beans>

3. 通过工厂方法创建Bean

有时候一些对象是通过工厂方法实例化的,这里介绍一下。

package com.chzhao.springtest;

public class BllFactory {

	public static PersonBll getPersonBll() {
Person p = new Person();
p.setName("老李");
PersonBll bll = new PersonBll(p);
return bll;
}
}

对应配置文件

<?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.xsd">
<bean name="PersonBll" class="com.chzhao.springtest.BllFactory"
factory-method="getPersonBll">
</bean>
</beans>

Spring入门(2)-通过构造器注入Bean的更多相关文章

  1. Spring入门(4)-注入Bean属性

    Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...

  2. Spring入门2. IoC中装配Bean

    Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...

  3. 【Spring 基础】通过注解注入Bean

    原课程:通过注解注入Bean 注入bean知识点思维导图 Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection spring为什么推荐使用构 ...

  4. Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean

    在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...

  5. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

  6. Spring配置优化_构造器注入+自动装配

    2014-05-16 09:01:08上课内容: 依赖注入的第二种注入方式:构造器注入 创建带参数的构造方法,参数类型为注入类的类型 项目要先添加Spring支持: package com; publ ...

  7. spring为什么推荐使用构造器注入?

    闲谈 ​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由Spr ...

  8. spring为什么推荐使用构造器注入

    一.前言 ​ 项目中遇到一个问题:项目启动完成前,在A类中注入B类,并调用B类的某个方法. 那么调用B类的这个方法写在哪里呢,我选择写到构造器里,但是构造器先于Spring注入执行,那么执行构造器时, ...

  9. 浅谈spring为什么推荐使用构造器注入

    转载自: https://www.cnblogs.com/joemsu/p/7688307.html 一.前言 ​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversi ...

随机推荐

  1. Ubuntu 12.04搭建MTK 6577 安卓开发环境

    Ubuntu 12.04搭建 MTK 6577安卓开发环境 1.       下载并安装Vmware虚拟机: 2.       下载并在虚拟机上安装Ubuntu 12.04 iso 安装包:下载地址: ...

  2. BZOJ 2820 YY的GCD(莫比乌斯函数)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2820 题意:给定n,m.求1<=x<=n, 1<=y<=m且Gc ...

  3. Android开发之Okhttp:java.lang.IllegalStateException: closed

    在使用Okhttp的时候 运行到response.body().string()一步时抛异常,java.lang.IllegalStateException: closed 查阅各种资料大致意思是Th ...

  4. Android studio在真机上进行调试

    1.在Android Studio中,把app的默认启动目标改为USB device,点击[app]→[app configuration],在[Target Device]选择[USB device ...

  5. 函数fsp_header_init

    /**********************************************************************//** Initializes the space he ...

  6. VirtualBox clonevdi文件和修改vdi的uuid

    因为VirtualBox下,不允许有相同的uuid,所以要拷贝一份新的vdi像普通的拷贝是办不到的.需要用VirtualBox自带的一个.exe文件VBoxManage. 1.首先,进入终端或者是命令 ...

  7. Buffer cache 的调整与优化

    Buffer cache 的调整与优化 -============================== -- Buffer cache 的调整与优化(一) --==================== ...

  8. SwipeRefreshLayout的简要说明及使用demo

    在最新的  Android Support Library, revision 19.1.0 (March 2014)   添加了SwipeRefreshLayout控件. 版本发布的说明信息如下: ...

  9. 转载:ofstream和ifstream详细用法

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O, ...

  10. 2、列表item_圆头像_信息提示

    import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import an ...