博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Spring实战》-2
阅读量:4669 次
发布时间:2019-06-09

本文共 3560 字,大约阅读时间需要 11 分钟。

装配Bean

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

2、创建Spring配置
从Sring3.0开始,Spring容器提供了两种配置Bean的方式:
XML文件配置方式
基于Java注解的配置方式
3、典型的xml配置文件:
775861-20160531210245883-2033469334.png

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

775861-20160531210328149-1356635304.png

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

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

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

Spring提供的作用域选项:

775861-20160531210407117-1105533614.png

6、初始化和销毁Bean

Auditorium(舞台)要保证做的最先和最后两件事情:
开灯,关灯。
775861-20160531210549414-9503843.png

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

775861-20160531210559836-1419730905.png

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

示例:

1、程序结构:

775861-20160531210629711-533423001.png

2、简单的Spring装配

Performer.java接口

//
package com.springinaction.springidol;public interface Performer { void perform() throws PerformanceException;}//

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"); }}//

spring-idol.xml配置文件

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的参数

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

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

value可以指定基本类型;

ref是引用其他的bean定义。

Poem.java接口

//
package com.springinaction.springidol;public interface Poem { void recite();}//

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]); } }}//

spring-idol.java配置文件

转载于:https://www.cnblogs.com/myitroad/p/5547551.html

你可能感兴趣的文章
Spring3.0.5 获取表中自增的主键(mysql)
查看>>
delphi dxBarManager 的dxBarEdit 输入问题
查看>>
Hadoop入门介绍一
查看>>
面试经典-分金条
查看>>
利用AutoSPSourceBuilder和Autospinstaller自动安装SharePoint Server 2013图解教程——Part 1...
查看>>
ZOJ-2972-Hurdles of 110m(记忆化搜索)
查看>>
一些新了解到技术
查看>>
vue.js click点击事件获取当前元素对象
查看>>
【单调栈,单调队列】总结
查看>>
LeetCode:Gas Station
查看>>
MyBatis初识(通过小实例清晰认识MyBatis)
查看>>
面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力
查看>>
XCODE快捷键和功能汇总篇(不断更新)
查看>>
Servlet开发(一)
查看>>
linux下如何查看某个容器的详细信息?
查看>>
bzoj 2843: 极地旅行社
查看>>
车林通购车之家--购车计算器模块--算法js
查看>>
webpack使用教程
查看>>
MySQL学习8 - 数据的增删改
查看>>
Linux笔记(开机自动将kerne log保存到SD卡中)
查看>>