前言

Spring最基础的功能就是一个bean工厂,所以本文讲解的是Spring生成bean的种种方法及细节,Spring配置文件的名字是bean.xml,定义几个类:

一个Person类:

public class Person
{private String     personName; // 人的名字private int        personAge;    // 人的年龄    public Person(String personName, int personAge){this.personName = personName;this.personAge = personAge;}public String getPersonName(){return personName;}public void setPersonName(String personName){this.personName = personName;}public int getPersonAge(){return personAge;}public void setPersonAge(int personAge){this.personAge = personAge;}public String toString(){return "personName = " + personName + ", personAge = " + personAge;}
}

一个Family类,里面持有Person的引用:

public class Family
{private Person person;private String familyName;public Family(Person person, String familyName){this.person = person;this.familyName = familyName;}public String toString(){return person.toString() + ", familyName = " + familyName;}
}

一个单例类:

public class SingletonClass
{private SingletonClass instance = new SingletonClass();private SingletonClass(){}public SingletonClass getInstance(){return instance;}
}

一个空的类,为了测试初始化和销毁用的:

public class EmptyClass
{static{System.out.println("Enter EmptyClass.static block");}public EmptyClass(){System.out.println("Enter EmptyClass.construct()");}public void init(){System.out.println("Enter EmptyClass.init()");}public void destory(){System.out.println("Enter EmptyClass.destory()");}
}

一个集合类,为了演示集合注入:

public class CollectionClass
{private List<String> list;private Map<Family, Person> map;private int[] ints;public List<String> getList(){return list;}public void setList(List<String> list){this.list = list;}public Map<Family, Person> getMap(){return map;}public void setMap(Map<Family, Person> map){this.map = map;}public int[] getInts(){return ints;}public void setInts(int[] ints){this.ints = ints;}
}

最简单的bean实例化

bean.xml中注入这个bean,以Person类为例:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="person" class="com.xrq.bean.Person" /></beans>

main函数这么写:

public static void main(String[] args)
{ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");Person person1 = (Person)ctx.getBean("person");Person person2 = (Person)ctx.getBean("person");System.out.println(person1 == person2);
}

运行结果为true,也就是说Spring默认以单例的形式给开发者构造出指定的bean。另外有两点要注意:

1、同一个spring.xml中不可以定义两个id相同的bean

2、ClassPathXmlApplicationContext中有一个可变长度的构造函数,用于加载多个.xml中的bean,如果bean中有id相同,那么id相同的bean,后加载的会覆盖先加载的

bean的作用域及生命周期

代码不动,把bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="person" class="com.xrq.bean.Person" scope="prototype"lazy-init="true"/></beans>

这里出现了两个属性,scope和lazy-init:

1、scope表示的是bean的作用域,有prototype、request、session、singleton四种,其中singleton是默认的,表示单例。prototype表示每次创建都会产生一个bean实例。request和session只在web项目中才会用,其作用域就和web中的request和session一样

2、lazy-init表示的是bean的生命周期,默认为false。当scope=singleton时,bean会在装在配置文件时实例化,如果希望bean在产生时才实例化,可以把lazy-init设置为true。当scope=prototype时,在产生bean时才会实例化它。补充一点,如果希望该配置文件中所有的bean都延迟初始化,则应该在beans根节点中使用lazy-init="true"

三种注入方式

所谓注入即注入bean中的属性,Spring为用户提供了三种注入方式,settter注入、构造方法注入、注解注入,不过对于Spring的讲解,不讲注解,所以看一下前两种注入方式:

1、setter注入,bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="person" class="com.xrq.bean.Person"><property name="personName" value="Alice"/><property name="personAge" value="10" /></bean>
</beans>

2、构造方法注入,bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="family" class="com.xrq.bean.Family"><constructor-arg name="person" ref="person" /><constructor-arg name="familyName" value="friendly" /></bean><bean id="person" class="com.xrq.bean.Person"><property name="personName" value="Alice"/><property name="personAge" value="10" /></bean>
</beans>

这里故意把family的定义写在person的定义上面,说明即使前面的beanA持有beanB的引用,把beanA定义在beanB前面也不影响

集合注入

spring对于集合属性的支持非常好,以CollectionClass为例,看下如何配置bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="collectionClass" class="com.xrq.bean.CollectionClass"><property name="list"><list><value>111</value><value>222</value></list></property><property name="map"><map><entry key="111"><bean class="com.xrq.bean.Person"><property name="personName" value="Mike"/><property name="personAge" value="11" /></bean></entry></map></property><property name="ints"><array><value>333</value><value>444</value></array></property></bean>
</beans>

工厂方式生成类

Spring虽然可以指定bean以单例的方式生成出来,但是每次都要用getBean方法获取类的实例非常麻烦,有办法像单例模式一样使用XXX.getInstance()就好了,不过还真有,以SingletonClass作为例子:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="singletonClass" class="com.xrq.bean.SingletonClass"factory-method="getInstance"></bean></beans>

这样,我们就可以使用单例的方式去调用这个类了,如果类里面有一些私有属性,还可以注入的方式在生成这个bean的时候就注入进去,非常方便

init-method和destory-method

有时候我们希望,在某个bean加载的时候做一些事情,销毁的时候做一些事情(是不是想到了Servlet),所以我们可以自定义初始化和销毁的方法EmptyClass这个类,bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="emptyClass" class="com.xrq.bean.EmptyClass" init-method="init" destroy-method="destory"/>
</beans>

注意两点:

1、实例化类的时候,几个方法的加载顺序为静态资源->构造方法->初始化方法

2、触发destory()方法的调用可以使用"((ClassPathXmlApplicationContext)ctx).close();",注意scope="prototype"是不会触发destory()的,没有为什么,设计就是这样

父子类继承关系

有时候我们有要求,一个类是某一个类/抽象类的子类,可以这么做:

public abstract class AbstractClass
{}

public class ImplClass extends AbstractClass
{}

此时bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><bean id="abstractClass" class="com.xrq.bean.abstractClass" abstract="true"/><bean id="implClass" class="com.xrq.bean.ImplClass" parent="abstractClass" />
</beans>

注意这种写法对接口也有效。

Spring2:bean的使用相关推荐

  1. Spring 快速入门详解

    Spring 入门详解 简介 Spring就是指Spring framework是分层的 Java SE/EE 一站式轻量级开源框架,Spring 框架是全家桶(spring mvc.spring b ...

  2. Spring2.5.6学习笔记-实例化bean的几种方法

    1. 用构造器来实例化 <bean id="exampleBean" class="examples.ExampleBean"/> <bean ...

  3. 详解Spring中Bean的自动装配~

    目录 1. 环境搭建 2. byName.byType 3. 使用注解实现自动装配 @Autowired @Resource 小结 自动装配是Spring满足bean依赖的一种方式 Spring会在上 ...

  4. 惊呆了,Spring中竟然有12种定义bean的方法

    前言 在庞大的 Java 技术体系中,Spring 有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜. 我们都知道 Spring 是创建和管理bean的工厂,它提供了多种方式定义 bean,能 ...

  5. Spring2.5注释驱动与基于注释的MVC

    写在前面: 好长时间没有写博客了,主要是最近一直忙于工作上面的事情没有研究什么新的东西,也没有什么写的,最近应一个朋友的邀请一起开发一套教材,我才有开始对Spring研究起来,今天把写的其中一部分贴出 ...

  6. 属性处理器Spring攻略学习笔记(2.12)------外部化Bean配置

    PS:今天上午,非常郁闷,有很多单简基础的问题搞得我有些迷茫,哎,码代几天不写就忘.目前又不当COO,还是得用心记码代哦! 一.知识点 Spring中PropertyPlaceholderConfig ...

  7. 使用Spring2.5的Autowired实现注释型的IOC(转载)

    专注java已6年,欢迎加入java核心技术QQ群:135138817,每周五晚有群主进行技术讲座. 使用Spring2.5的Autowired实现注释型的IOC     阅读(31) 评论(0) 发 ...

  8. Spring中bean的scope详解

    如何使用spring的作用域: <bean id="role" class="spring.chapter2.maryGame.Role" scope=& ...

  9. JBoss下布署Spring2.5和Struts2系统

    目前在做JBoss下布署String2.5 & Struts2集成的工程,在工程中用Spring2.5 的component scan, Struts2 的convention 和 rest ...

最新文章

  1. java连接mysql数据库增删改查_java连接mysql数据库增删改查操作记录
  2. GPT“高仿”系列开源了!最大可达GPT-3大小,还能自主训练
  3. Apache Shiro教程
  4. 学python爬虫需要什么基础-学习python爬虫需要具备的基本功
  5. 1078 Hashing (25 分)【难度: 一般 / 知识点: 哈希表二次探测法】
  6. SpringSecurity简单应用(二)
  7. java多选代码_[一天一点java web]复选框全选代码
  8. oracle存在于A不存在于B,Oracle试题
  9. 在多个线程中使用QNetworkAccessManager
  10. 数据库设计三范式(3NF)
  11. java day15 【Map】
  12. node 学习 ——模块导出
  13. 【产品】产品经理手册
  14. YYText的介绍和使用
  15. Spring Boot 微信-验证服务器有效性
  16. windows10系统自带linux子系统(WSL)的安装目录
  17. 电路矢量图编辑器数据库设计
  18. canvas-随机粒子特效
  19. Timingdesigner timing designer 入门 基础 教程
  20. jsp获取静态服务器文件路径,11、统一处理异常、处理静态资源访问、项目中的绝对地址跟相对地址问题...

热门文章

  1. 机器学习python中train_test_split()函数进行数据集分割
  2. android微信支付开发过程
  3. 通信原理_2 确定信号分析
  4. 单片机c语言或运算,单片机avr c语言位运算 与或非 异或逻辑 运算介绍 详解
  5. 高通平台开发系列讲解(AtCoP篇)AtCoP架构简介
  6. Linux 如何设置代理
  7. 成功解决 pycharm一直indexing
  8. MTK 虚拟 sensor bring up (pick up) sensor1.0
  9. ACL的rule中的deny/permit在各个业务模块里的场景是怎样的
  10. 什么是 “零” 拷贝?