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 ...
随机推荐
- Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
术语和概念 屏幕尺寸 屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸). 简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小. 程序可以针对这三种尺 ...
- 1651. Shortest Subchain(bfs)
1651 终于A了 看这题容易想到最短路 看到错的很多 还特意注意了好几处 后来发现 必须按给出的顺序出边 想了想 这不就是BFS 然后就是各种细节 i->i+1ori->j(a[i]== ...
- JAVA中获取工程路径的方法
在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...
- return File
public ActionResult DownloadMessage() { string strExportData = "无数据!"; byte[] data = Syste ...
- apache开源项目-- Usergrid
Ed Anuff上周宣布第一版 Usergrid 的代码已经发布,Usergrid 是一款适用于移动和富客户端应用的综合性平台.Ed 认为,良好的移动性指的是将设备中运行应用的良好交互带到云中,其中既 ...
- 从投影的角度理解pca:向量,投影,基,内积,坐标,维数,分散程度,方差,协方差矩阵,对角化,特征值分解,主成分分析PCA
参考:http://blog.csdn.net/songzitea/article/details/18219237
- 学习Mongodb(一)
图片摘录自陈彦铭出品2012.5的<10天掌握MongDB> MongoDB的特点--->面向集合存储,易于存储对象类型的数据--->模式自由--->支持动态查询---& ...
- memcache的应用场景和实现原理
面临的问题 对于高并发高访问的 Web应用程序来说,数据库存取瓶颈一直是个令人头疼的问题.特别当你的程序架构还是建立在单数据库模式,而一个数据池连接数峰 值已经达到500的时候,那你的程序运行离崩溃的 ...
- Spring学习之声明式事物管理
public List<Student> selectStudent() { Student s = new Student(); s.setName("zhengbin&quo ...
- jsp防盗链代码
// 禁止缓存 response.setHeader("Cache-Control", "no-store"); response.setHeader( ...