Spring入门(2)-通过构造器注入Bean
Spring入门(2)-通过构造器注入Bean
前一篇文章将了最基本的spring例子,这篇文章中,介绍一下带有参数的构造函数和通过构造器注入对象引用。
0. 目录
- 带有参数的构造函数
- 通过构造器注入对象引用
- 通过工厂方法创建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的更多相关文章
- Spring入门(4)-注入Bean属性
Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...
- Spring入门2. IoC中装配Bean
Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...
- 【Spring 基础】通过注解注入Bean
原课程:通过注解注入Bean 注入bean知识点思维导图 Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection spring为什么推荐使用构 ...
- Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean
在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
- Spring配置优化_构造器注入+自动装配
2014-05-16 09:01:08上课内容: 依赖注入的第二种注入方式:构造器注入 创建带参数的构造方法,参数类型为注入类的类型 项目要先添加Spring支持: package com; publ ...
- spring为什么推荐使用构造器注入?
闲谈 Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由Spr ...
- spring为什么推荐使用构造器注入
一.前言 项目中遇到一个问题:项目启动完成前,在A类中注入B类,并调用B类的某个方法. 那么调用B类的这个方法写在哪里呢,我选择写到构造器里,但是构造器先于Spring注入执行,那么执行构造器时, ...
- 浅谈spring为什么推荐使用构造器注入
转载自: https://www.cnblogs.com/joemsu/p/7688307.html 一.前言 Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversi ...
随机推荐
- MyBatis学习总结4--解决字段名与实体类属性名不相同的冲突
在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定是完全相同的,如果直接在xml映射文件中使用sql进行映射,会造成返回值为空的情况,下面阐述解决方案: 测试所用表和数据 create t ...
- openfire中mysql的前期设置
使用openfire的时候如果需要使用自己的mysql数据库,需要提前进行设置,下面将记录下,基本的设置过程. 一.前期准备工作: 1.先下载两个工具一个是mysql数据库还有一个是SQLyog(可以 ...
- Linux查看所有用户用什么命令
用过Linux系统的人都知道,Linux系统查看用户不是会Windows那样,鼠标右键看我的电脑属性,然后看计算机用户和组即可. 那么Linux操作系统里查看所有用户该怎么办呢?用命令.其实用命令就能 ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- iOS开发:mac使用svn管理项目
记录mac下常用的svn命令: 1.检出项目: svn checkout .../svn/projectName --username=xxx --password=xxx //将ip换成svn服务器 ...
- 自学了三天的SeaJs学习,解决了前端的一些问题,与小伙伴们一起分享一下!
我为什么学习SeaJs? [第一]:为了解决项目中资源文件版本号的问题,以及打包压缩合并等问题. [第二]:好奇心和求知欲.[我发现很多知名网站也都在使用(qq空间, msn, 淘宝等等),而且 Se ...
- [反汇编练习] 160个CrackMe之013
[反汇编练习] 160个CrackMe之013. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 我是红领巾,分享2014 google不能用的方法。
那啥已经20天打不开了. 得爬qiang. 今天无意间发现一个好东东. 特记录一下. 360浏览器设置 1. 工具菜单==>选项==>高级设置==>管理搜索引擎 . 2. ...
- hihoCoder 1385 A Simple Job
#1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...
- hdu 1078(dfs记忆化搜索)
题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...