装配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. WPF之X名称空间学习

    WPF的X名称空间都有什么呢?首先,盗用张图来说明: 我将就图表中的内容进行总结: 1.x:Array具有一个Iteams属性,它能暴漏一个ArratList实例,ArratList实例的内部成员类型 ...

  2. docker+jenkins 部署持续集成环境

    1.使用docker container的方式安装jenkins [root@hadoop default]# docker pull jenkin 创建一个目录用于后边映射 [root@hadoop ...

  3. FastAdmin 的 Bootstrap-Table 如何合并字段?

    FastAdmin 的 Bootstrap-Table 如何合并字段? ★hey-成都 14:13:34 把下面那个字段合并到上面那个字段是用什么方法 ^★暗物质-江西 14:17:21 city加上 ...

  4. Linux 终端 忽略大小写

    忘了在哪里看到的了,记录一下. 在-/.inputrc中加入一行 set completion-ignore-case on 搞定! 这样在终端输入.补全时就忽略大小写了.当然,Linux本身还是区分 ...

  5. 在系统学习javaEE开发的颠覆者Springboot时遇到的localhost无法访问的问题

    就是新建了一个Springboot项目,但是无法正常访问. 关闭防火墙试了,mvn方式启动试了,换端口试了.然后用Tomcat的start.bat测试发现localhost是可以访问的. 上网找到各种 ...

  6. WPF基本概念入门

    关于数据类型,有原子类型,列表类型,字典类型等等,而wpf对应控件有contentControl,itemsControl,headerItemsControl等. 控件和类型一一对应,控件和类型之间 ...

  7. laravel 多个where的连接使用

    在查询的时候需要用到多个where条件来查询 1.直接多个where连接 ->where()->where() 2.把查询条件 放到where数组$where中 然后 ->where ...

  8. 命令"service 服务名 restart" 与 "service 服务名 reload"的区别

    由于今天用到了service nginx reload 和 service nginx restart,说说他俩的区别吧: reload:不间断服务重启,就像一张网页上面的刷新按钮一样. restar ...

  9. kubernetes 学习 ingress

    ingress是Kubernetes 暴露服务的一种方式. Ingress由两部分组成:Ingress Controller 和 Ingress 服务.     Ingress Contronler ...

  10. SQL中得到最后一个“-”后面的字符串

    declare @str varchar(50) set @str='-'+'1-9-3-2'--前面加个'-'防止没有'-'出错 select REVERSE(SUBSTRING(REVERSE(@ ...