Sping in Action读书笔记之一----------Bean的各种注入操作
来源:互联网 发布:广告设计和美工的区别 编辑:程序博客网 时间:2024/06/07 18:25
Spring in Action这本书可以说是非常好的一本讲解Spring框架的好书,现在出版了第三版,包含了Spring3.0的许多
新特性,特此拿来好好系统的学习一番,以前对Spring有所了解,但是不是非常的详细,现在打算好好的认真的研究一
下Spring,先从最基础的开始吧。
1.Declaring a simple bean
这里声明了一个非常简单的Bean然后使用Spring的依赖注入技术实现对他的调用。
package com.bird.springidol;/** * declaring a simple bean * @author Bird * */public class Juggler {private int beanBags = 3;public Juggler(){}public Juggler(int beanBags){this.beanBags=beanBags;}public void perform(){System.out.println("Juggling "+beanBags+" BEANBAGS");}}
只需要在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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > <bean id="duke" class="com.bird.springidol.Juggler"></bean>
然后使用Junti测试框架进行测试
/** * declaring a simple bean */@Testpublic void test1(){ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");Juggler ju = (Juggler) ctx.getBean("duke");ju.perform();}
2.injecting through constructors
通过构造函数传入参数的注入方式
还是刚才的那个代码,这里使用了不同的配置方式
<bean id="duke1" class="com.bird.springidol.Juggler"> <constructor-arg value="15"></constructor-arg> </bean>
测试代码如下
/** * Injecting through constructors */@Testpublic void test2(){ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");Juggler ju = (Juggler) ctx.getBean("duke1");ju.perform();}
3.injecting object references with constructors
通过构造函数传入对象
package com.bird.springidol;/** * injecting object references with constructors * @author Administrator * */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(){super.perform();System.out.println("whing reciting.....");poem.recite();}}
package com.bird.springidol;public class Sonnect29 implements Poem {@Overridepublic void recite() {System.out.println("this is a poem that too long");}}
配置方式如下
<bean id="sonnet29" class="com.bird.springidol.Sonnect29"></bean> <bean id="poeticDuke" class="com.bird.springidol.Poeticjuggler"> <constructor-arg value="15"></constructor-arg> <constructor-arg ref="sonnet29"></constructor-arg> </bean>
4.creating beans through factory methods
很多类使用单例设计模式,这样的对象只能通过静态方法生成一个对象,这里说一下如何注入这些对象
package com.bird.springidol;/** * creating beans through factory methods * @author Administrator * */public class Staage {private Staage(){}private static class StageSingletonHolder{static Staage instance = new Staage();}public static Staage getInstance(){return StageSingletonHolder.instance;}}
<bean id="theStage" class="com.bird.springidol.Staage" factory-method="getInstance"></bean>
5.initializing and destorying beans
在创建和删除Bean的时候调用的代码
只需要这样配置
<bean id="test1" class="" init-method="" destroy-method=""></bean>
6.injecting into bean properties
注入对象的属性
package com.bird.springidol;/** * injecting into bean properties * * @author Administrator * */public class Instrumentalist {private String song;private Instrument instrument;public String getSong() {return song;}public void setSong(String song) {this.song = song;}public Instrument getInstrument() {return instrument;}public void setInstrument(Instrument instrument) {this.instrument = instrument;}public Instrumentalist(){}public void perform(){System.out.println("playing"+song+":");instrument.play();}}
package com.bird.springidol;public class Saxophone implements Instrument {public Saxophone(){}@Overridepublic void play() {System.out.println("TOOT TOOT TOOT");}}
配置如下
<bean id="saxophone" class="com.bird.springidol.Saxophone"></bean> <bean id="kenny" class="com.bird.springidol.Instrumentalist"> <property name="song" value="Jingle Bells"></property> <property name="instrument" ref="saxophone"></property> </bean>
- Sping in Action读书笔记之一----------Bean的各种注入操作
- action层的sping注入
- 【Spring In Action】注入Bean属性
- Sping中的Bean注入Servlet
- [读书笔记] EJB 3 in Action: Spring Bean 与 EJB Session Bean 的异同
- Sping bean的生命周期
- Sping Bean的装配
- Action中成员bean的自动注入
- Sping(一):Bean装配和注入
- spring in action 学习笔记(七)注入内部bean
- 《Spring In Action》 读书笔记(1) -- bean装配
- 《Spring In Action》 读书笔记(2) -- bean装配
- Spring in Action 读书笔记之二-----------SpEL初步和容器的注入
- Struts2 In Action读书笔记一------------简单的表单验证和属性注入
- sping+struts2中配置Action的bean中scope="prototype"的作用
- sping+struts2中配置Action的bean中scope="prototype"的作用
- Sping之Bean的生命周期
- SSH下Action中无法注入业务bean的解决方法
- ELisp编程十三:宏二
- 配置tomcat通过doget servlet 方法 向jquery脚本发送json格式数据
- Vendor master data
- 设计模式之工厂模式学习
- 开篇
- Sping in Action读书笔记之一----------Bean的各种注入操作
- Linux解压缩指令大全
- 按照RFC3984协议实现H264视频流媒体 RTSP H264
- convert , transform , transfer , transmit ,transit 的区别
- Perl中的向后引用
- gcc常用的编译选项对代码的影响
- 1IOCP背景知识--重叠I/O理解
- SQLite使用
- JAVA GC原理