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 ...
随机推荐
- Cookie的具体使用之来存储对象
1.创建一个新的cookie,并赋值. HttpCookie cookie; cookie=new HttpCookie("user"); cookie.D ...
- POJ 1808 Quadratic Residues(平方剩余相关)
题目链接:http://poj.org/problem?id=1808 题意:如下.对于素数p,若存在x使得x^2%p=a,则其值为1.否则为-1.现在给出a.p,计算其值. 思路: 若a为正数则利用 ...
- Android log日志
LOG是用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用. androidsdk中提供了l ...
- JAVA中获取项目文件路径
在java中获得文件的路径在我们做上传文件操作时是不可避免的. web 上运行 1:this.getClass().getClassLoader().getResource("/" ...
- 图表框架HelloCharts(2)柱状图
1.效果图 2.xml代码 activity_column_chart.xml <FrameLayout xmlns:android="http://schemas.android.c ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- java classpath、path用法
java环境配置classpath和path变量的作用: path:指定cmd中命令执行文件所在的路径.比如javac.java两个可执行文件在jdk的bin目录下,如果path值含有这个bin目录, ...
- IOS AFNetworking简介及使用
转:http://www.it165.net/pro/html/201405/13099.html 一AFNetworking简介AFNetworking是一个在IOS开发中使用非常多网络开源库,适用 ...
- 强大的日志分析工具 -- NSLogger
转:http://www.cnblogs.com/yingkong1987/p/3329945.html 强大的日志分析工具 -- NSLogger 源码:https://github.com/fpi ...
- 在PC上收发短信--Pidgin短信(Linux Pidgin插件)
Pidgin短信是Pidgin/empathy的一个插件.当手机收到短信进,电脑上面有提示:可以在电脑上发手机短信.让你时刻保持在电脑屏幕上,也不会错过短信.目前只支持安卓,wifi连接方式. 功能 ...