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. Bug调试

    iPhone开发笔记——Xcode升级后的警告.错误的解决办法(一) http://blog.sina.com.cn/s/blog_58af95150101slit.html iPhone开发笔记—— ...

  2. TeeChart的X轴为时间,多个Y轴的显示

    最后上代码 public partial class Test : Form { private TChart tChart = new TChart(); ; public Test() { Ini ...

  3. [HZAU]华中农业大学第四届程序设计大赛网络同步赛

    听说是邀请赛啊,大概做了做…中午出去吃了个饭回来过掉的I.然后去做作业了…… #include <algorithm> #include <iostream> #include ...

  4. AVL的旋转

    转自http://blog.csdn.net/gabriel1026/article/details/6311339 平衡二叉树在进行插入操作的时候可能出现不平衡的情况,AVL树即是一种自平衡的二叉树 ...

  5. Python3 学习第二弹: 字符串String

    字符串表示问题 常见用法 '' 与 "" 就不提了 一些特殊用法 三引号:接收多行字符串的输入 >>>print('''Oh my God!''') Oh my ...

  6. Java Web编程的主要组件技术——JSP

    参考书籍:<J2EE开源编程精要15讲> JSP(Java Server Page)页面由HTML代码和嵌入其中的Java代码组成. 简单的JSP页面如: <html> < ...

  7. H264 帧结构分析、帧判断

    http://blog.csdn.net/dxpqxb/article/details/7631304 H264以NALU(NAL unit)为单位来支持编码数据在基于分组交换技术网络中传输. NAL ...

  8. Oracle IO优化心得

    很多的时侯,做Oracle DBA的我们,当应用管理员向我们通告现在应用很慢.数据库很慢的时侯,我们到数据库时做几个示例的Select也发现同样的问题时,有些时侯我们会无从下手,因为我们认为数据库的各 ...

  9. [C++]cin读取回车键

    最近碰到一个问题,就是从控制台读取一组数,如: 12 23 34 56 若是使用 int data; while ( cin >> data ) {//...} 当回车后,不能有效转换到后 ...

  10. Linux+Apache+Tomcat集群配置

    参考: http://blog.csdn.net/bluishglc/article/details/6867358# http://andashu.blog.51cto.com/8673810/13 ...