Spring的《XML显式装配bean》之通过构造器注入Bean
来源:互联网 发布:抓包软件下载 编辑:程序博客网 时间:2024/06/11 10:54
本文主要讲解两点:
1.怎么样声明一个bean
2.通过构造器注入bean
1. 怎么样声明一个bean?
1) 创建一个类:
package spring.ch1.topic5;public class Song { private String name; public Song(String name) { this.name = name; } public Song() { } @Override public String toString() { return "the song:" + name; }}
2)配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="song" class="spring.ch1.topic5.Song"> <constructor-arg value="there will be" /> </bean></beans>
这一句表示往构造器里面注入参数。
2.怎样通过构造器注入Bean?
下面以厨师制作蛋糕为例子。
(1)创建厨师类,在构建厨师的对象时,我们把需要制作的蛋糕的对象也传进去。
package spring.ch1.topic5;public class Chief { private Cake cake = null; public Chief(Cake cake) {//构造器注入的地方 this.cake = cake; } public void makeOneCake() { System.out.println(cake.toString()); }}
(2)创建蛋糕类,通过final来标识每一个对象的id
package spring.ch1.topic5;public class Cake { private final int id = index++; private static int index = 0; @Override public String toString() { return "create the cake,its id:" + id; }}
(3)配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="cake" class="spring.ch1.topic5.Cake"> </bean> <bean id="chief" class="spring.ch1.topic5.Chief"> <constructor-arg ref="cake"/> </bean></beans>
配置文件这里主要通过bean标签来配置:
在Bean标签里面还可以写入constructor-arg 标签,这里就是通过构造器注入,他里面有两个参数,一个是ref,引用上面某个bean;一个是value,直接写入值(比如string类型数据)。
(4)测试类
package spring.ch1.topic5;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "/spring/ch1/topic5/ApplicationContext-test.xml" })public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief() { Chief chief = applicationContext.getBean(Chief.class); chief.makeOneCake(); }}
运行结果:
create the cake,its id:0
0 0
- Spring的《XML显式装配bean》之通过构造器注入Bean
- Spring的《XML显式装配bean》之通过属性property注入bean
- Spring Bean xml显式装配之构造函数
- Spring的《XML显式装配bean》- 注入Map
- Spring 装配Bean的构造注入
- Spring之通过XML装配bean(三)
- Spring的《XML显式装配bean》- XML自动装配
- Spring学习笔记(四) --- 装配Bean之通过XML装配
- Spring的《XML显式装配bean》- 默认自动装配
- Spring的《XML显式装配bean》之设置bean的作用域
- Spring的《XML显式装配bean》之Bean的创建与销毁操作
- Spring的《XML显式装配bean》- 注入List或则set
- Spring装配Bean——通过配置XML装配bean
- 深入学习Spring框架之二构造器注入方式装配Bean
- Spring 如何通过 XML 装配 bean?
- 一步一步学Spring-通过xml装配bean
- spring bean的构造器注入
- Spring学习笔记之通过XML装配Bean的一些细节
- 滴滴2017秋招笔试刷题
- 十大算法收藏
- poj 2299 Ultra-QuickSort
- 泛化Abstract Factory
- Makefile简介
- Spring的《XML显式装配bean》之通过构造器注入Bean
- web.py 使用
- 信息搜集(未完)
- 负载均衡(Load Balancing)学习笔记二
- 使用gcc给待编译C/C++源码注入宏的2种方法
- 常用集合简介
- pat-bl-1035
- scala interpolator 字符串插值
- WIN7环境下通过ISS7快速搭建简易FTP服务器