装配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. NET Core 2.0利用MassTransit集成RabbitMQ

    NET Core 2.0利用MassTransit集成RabbitMQ https://www.cnblogs.com/Andre/p/9579764.html 在ASP.NET Core上利用Mas ...

  2. 如何让 PADS Layout 识别到板框

    如何让 PADS Layout 识别到板框 在很久很久以前 PADS Laout 还是 PowerPCB 的时候,铺铜是不认识板框的. 当有铺铜时必须复制一份板框再设置为铺铜才可以. 但到了 PADS ...

  3. jquery中validation部分学习笔记

    1.valid()和rules()valid()方法:检查表单或某些元素是否有效,返回值为boolean型, 全部通过检查规则返回true,只要有不通过的检查规则都返回false rules()方法: ...

  4. C# 加密和解密文件

    相关示例代码如下: using System; using System.IO; using System.Security; using System.Security.Cryptography; ...

  5. Erlang pool management -- RabbitMQ worker_pool 2

    上一篇已经分析了rpool 的三个module , 以及简单的物理关系. 这次主要分析用户进程和 worker_pool 进程还有worker_pool_worker 进程之间的调用关系. 在开始之前 ...

  6. walle部署系统的使用

    在项目开发的时候要管理各种开发 测试 线上环境的代码 部署 回滚等操作 这里可以使用walle walle官网:http://www.walle-web.io/ 学习安装:https://blog.c ...

  7. 二:HTML文本编译器 kindeditor-4.1.10 的使用 SpringMVC+jsp的实现

    这和一篇与上一篇的区别在与,上一篇是直接请求到action我们剩下的都是我们全部手动处理, 而这一片篇是由kindeditor内部处理,图片上传到本地,基本上没什么区别,但是有一点一定要注意的就是,这 ...

  8. Day1--Python基础1--上半部分

    一.第一个python程序 在linux下创建一个文件叫做hello.py,并输入 print "Hello World" 然后执行命令:python hello.py,输出 [r ...

  9. 讯为开发板的最小LINUX系统烧写及U盘的挂载及卸载

    fdisk -c 0 fatformat mmc 0:1ext3format mmc 0:2ext3format mmc 0:3ext3format mmc 0:4 fastboot fastboot ...

  10. c#的日志插件NLog基本使用

    本文介绍c#的日志插件NLog 安装插件 创建logger 日志级别 书写日志信息 配置 包装器 布局 安装插件 直接下载插件包 Install-Package NLog.Config 创建logge ...