装配Bean

1、装配wiring,即创建应用对象之间的协作关系的行为,者也是依赖注入的本质。

2、创建Spring配置

从Sring3.0开始,Spring容器提供了两种配置Bean的方式:

XML文件配置方式

基于Java注解的配置方式

3、典型的xml配置文件:

beans命名空间不是唯一的Spring命名空间,Spring核心框架自带了10个命名空间配置,如下:

4、当Spring容器加载Bean时,Spring使用反射来创建Bean。

5、覆盖Spring默认的单例配置:Spring Bean默认都是单例,但有时我们每次都需要获得唯一的Bean实例,比如每个人的门票要唯一:

我们只需要配置Bean的scope属性为prototype即可:

<bean id="ticket" class="com....Ticket" scope="prototype" />

Spring提供的作用域选项:

6、初始化和销毁Bean

Auditorium(舞台)要保证做的最先和最后两件事情:

开灯,关灯。

需要在Bean中使用init-method和destory-method属性:

当有很多上下文定义的Bean有相同名字的初始化方法和销毁方法时,可以直接在上层beans元素中声明default-init-method和default-destory-method属性,从而避免每一个都要设置一遍的问题。

示例:

1、程序结构:

2、简单的Spring装配

Performer.java接口

//<start id="performer_java" />
package com.springinaction.springidol;
public interface Performer {
void perform() throws PerformanceException;
}
//<end id="performer_java" />

Juggler.java实现类

//<start id="juggler_java" />
package com.springinaction.springidol;
public class Juggler implements Performer {
private int beanBags = 3;
public Juggler() {
}
public Juggler(int beanBags) {
this.beanBags = beanBags;
}
public void perform() throws PerformanceException {
System.out.println("JUGGLING " + beanBags + " BEANBAGS");
}
}
//<end id="juggler_java" />

spring-idol.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="duke"
class="com.springinaction.springidol.Juggler" />
</beans>

JugglerTest.java测试类

package com.springinaction.springidol;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by LTN on 2016/4/17.
*/
public class JugglerTest {
public static void main(String[] args) throws Exception{
ApplicationContext context =new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer) context.getBean("duke");
// Performer performer = (Performer) context.getBean("poeticDuke");
performer.perform();
}
}

3、有参数的装配过程

PoeticJuggler.java

其构造器需要一个int和Poem的参数

//<start id="poeticjuggler_java" />
package com.springinaction.springidol;
public class PoeticJuggler extends Juggler {
private Poem poem;
public PoeticJuggler(Poem poem) { //<co id="co_injectPoem"/>
super();
this.poem = poem;
}
public PoeticJuggler(int beanBags, Poem poem) { // <co id="co_injectPoemBeanBags"/>
super(beanBags);
this.poem = poem;
}
public void perform() throws PerformanceException {
super.perform();
System.out.println("While reciting...");
poem.recite();
}
}
//<end id="poeticjuggler_java" />

参数在xml文件中,需要以下配置:

 <constructor-arg value="15" />
<constructor-arg ref="sonnet29" />

value可以指定基本类型;

ref是引用其他的bean定义。

Poem.java接口

//<start id="poem_java" />
package com.springinaction.springidol;
public interface Poem {
void recite();
}
//<end id="poem_java" />

Sonnet29.java实现类

//<start id="sonnet29_java" />
package com.springinaction.springidol;
public class Sonnet29 implements Poem {
private static String[] LINES = {
"When, in disgrace with fortune and men's eyes,",
"I all alone beweep my outcast state",
"And trouble deaf heaven with my bootless cries",
"And look upon myself and curse my fate,",
"Wishing me like to one more rich in hope,",
"Featured like him, like him with friends possess'd,",
"Desiring this man's art and that man's scope,",
"With what I most enjoy contented least;",
"Yet in these thoughts myself almost despising,",
"Haply I think on thee, and then my state,",
"Like to the lark at break of day arising",
"From sullen earth, sings hymns at heaven's gate;",
"For thy sweet love remember'd such wealth brings",
"That then I scorn to change my state with kings." };
public Sonnet29() {
}
public void recite() {
for (int i = 0; i < LINES.length; i++) {
System.out.println(LINES[i]);
}
}
}
//<end id="sonnet29_java" />

spring-idol.java配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--<start id="duke_bean" />-->
<bean id="duke"
class="com.springinaction.springidol.Juggler" />
<!--<end id="duke_bean" />-->
<!--<start id="poeticduke_bean" />-->
<bean id="poeticDuke"
class="com.springinaction.springidol.PoeticJuggler">
<constructor-arg value="15" />
<constructor-arg ref="sonnet29" />
</bean>
<!--<end id="poeticduke_bean" />--> <!--<start id="sonnet29_bean" />-->
<bean id="sonnet29"
class="com.springinaction.springidol.Sonnet29" />
<!--<end id="sonnet29_bean" />-->
</beans>

《Spring实战》-2的更多相关文章

  1. 《从零开始做一个MEAN全栈项目》(1)

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在本系列的开篇,我打算讲一下全栈项目开发的优势,以及MEAN项目各个模块的概览. 为什么选择全栈开发? ...

  2. 《从零开始做一个MEAN全栈项目》(2)

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习.   上一节简单介绍了什么是MEAN全栈项目,这一节将简要介绍三个内容:(1)一个通用的MEAN项目的技 ...

  3. 《从零开始做一个MEAN全栈项目》(3)

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 上一篇文章给大家讲了一下本项目的开发计划,这一章将会开始着手搭建一个MEAN项目.千里之行,始于足下, ...

  4. 《从零开始做一个MEAN全栈项目》(4)

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在上一篇中,我们讲了如何去构建第一个Express项目,总结起来就是使用两个核心工具,express和 ...

  5. 一个关于vue+mysql+express的全栈项目(一)

    最近学了mysql数据库,寻思着能不能构思一个小的全栈项目,思来想去,于是就有了下面的项目: 先上几张效果图吧       目前暂时前端只有这几个页面,后端开发方面,有登录,注册,完善用户信息,获取用 ...

  6. Vue、Nuxt服务端渲染,NodeJS全栈项目,面试小白的博客系统~~

    Holle,大家好,我是李白!! 一时兴起的开源项目,到这儿就告一段落了. 这是一个入门全栈之路的小项目,从设计.前端.后端.服务端,一路狂飙的学习,发量正在欣喜若狂~~ 接触过WordPress,H ...

  7. Vue、Node全栈项目~面向小白的博客系统~

    个人博客系统 前言 ❝ 代码质量问题轻点喷(去年才学的前端),有啥建议欢迎联系我,联系方式见最下方,感谢! 页面有啥bug也可以反馈给我,感谢! 这是一套包含前后端代码的个人博客系统,欢迎各位提出建议 ...

  8. SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践

    一. 前言 日志对于一个程序的重要程度不用过多的言语修饰,本篇将以实战的方式讲述开源微服务全栈项目 有来商城 是如何整合当下主流日志解决方案 ELK +Filebeat . 话不多说,先看实现的效果图 ...

  9. 全栈项目|小书架|服务器端-NodeJS+Koa2实现首页图书列表接口

    通过上篇文章 全栈项目|小书架|微信小程序-首页水平轮播实现 我们实现了前端(小程序)效果图的展示,这篇文章来介绍服务器端的实现. 首页书籍信息 先来回顾一下首页书籍都有哪些信息: 从下面的图片可以看 ...

  10. 全栈项目|小书架|服务器开发-NodeJS 使用 JWT 实现登录认证

    通过这篇 全栈项目|小书架|服务器开发-JWT 详解 文章我们对JWT有了深入的了解,那么接下来介绍JWT如何在项目中使用. 安装 $ npm install jsonwebtoken 生成 Toke ...

随机推荐

  1. C++语言对C的增强(2)—— const增强、枚举的增强

    1.const基础知识 #include <iostream> int main(void) { //const定义常量--->const意味着只读 const int a; int ...

  2. Android UI--提高Android UI体验

    1,自定义虚拟键盘 当一个用户被要求在一个文本框输入时希望又怎样的体验?  从用户需求来看,虚拟键盘应该改变以帮助用户输入的数据.这里是一些例子: 如果一个视图是一个电子邮件地址,一个键盘的“@”符号 ...

  3. 相当有用的react+redux渲染性能优化原理

    学习地址:http://foio.github.io/react-redux-performance-boost/

  4. 使用case语句给字体改变颜色

    使用case语句给字体改变颜色 #!/bin/bash color(){ RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1; ...

  5. 用PowerShell在China Azure创建ARM虚拟机

    Azure目前有两种工作模式:ASM和ARM. 在国内的Azure,我们都是使用ASM的模式.但这种模式有很多限制,比如每个VM必须有一个公网地址,部署不能批量部署等等.ARM对Azure的整体架构做 ...

  6. nginx错误

    在开发的时候遇到nginx错误 网上找了半天也没有找到解决方案: 先查看了一下nginx错误日志 cat /usr/local/nginx/logs/error.log 然后发现看不太懂 那么只能重启 ...

  7. 判断唯一约束是否是唯一的Unique

    //检查 唯一约束Name //检查 唯一约束Name int count = new BLL.Funcs().GetRecordCount(string.Format("Name={0}& ...

  8. Qt多选框

    1.获取并显示复选框文本内容 ui->label->setText(ui->comboBox->currentText());

  9. 【转载】Linux 进程调度时间测量

    测试Context Switch time(进程上下文切换时间) --------------------------------------------------     创建两个进程(实时进程) ...

  10. uboot - *** Warning - bad CRC, using default environment

    出现这个现象的原因 环境变量存储区没有相应的数据,产生的原因可能是: 1.首次烧写uboot启动,,出现这个提示,执行saveenv 指令保存环境变量即可: 2.nor fash芯片的 基地址出错. ...