Spring容器:

Spring有两种不同的容器,一个是org.springframework.beans.factory.BeanFactory接口实现,另一个是应用上下文org.springframework.context.ApplicationContext接口实现.

BeanFactory是简单的容器,提供了基础的依赖注入,ApplicationContext是建立在BeanFactory基础之上的,它是Beanfactory的子类,功能比BeanFactory更加强大。

ApplicationContext提供了更多功能:

  • 提供了文本信息解析工具,包括对国际化的支持
  • 提供了载入文件资源的通用方法,如载入图片
  • 可以向注册为监听器的Bean发送事件

ApplicationContext经常用到的三种实现:ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, XmlWebApplicationContext

使用BeanFactory示例:

1         BeanFactory factory = new XmlBeanFactory(new ClassPathResource(
2                 "beans.xml"));
3         UserSave usersave = (UserSave) factory.getBean("usersave");

使用ApplicationContext示例:

1         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
2         UserSave usersave = (UserSave) context.getBean("usersave");

Bean的作用域:

作用域

描述

singleton

在每个Spring IoC容器中一个bean定义对应一个对象实例。

prototype

一个bean定义对应多个对象实例。

request

在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。

session

在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。

global session

在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。

示例:在beans.xml中

1     <bean id="usersave" class="com.yuan.test.UserSave" scope="singleton">
2         <property name="user">
3             <ref bean="user" />
4         </property>
5     </bean>

第1行中scope属性定义bean的作用域,将其定义为singleton即为单例。下面代码输出为true

1 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
2
3         UserSave usersave = (UserSave) context.getBean("usersave");
4         UserSave usersave2 = (UserSave) context.getBean("usersave");
5
6         System.out.println(usersave == usersave2);

同样可以测试其他的属性值。

依赖注入的方法:

  1. 接口注入
  2. 构造注入
  3. 设值注入

接口注入是要自定义的Bean实现规定的接口,Spring会自动进行注入,一般用的很少,下面是构造注入和设值注入的示例:

设值注入:

在Bean中为属性设置setter方法。

 1 public class UserSave {
 2     private User user;
 3
 4     public User getUser() {
 5         return user;
 6     }
 7
 8     public void setUser(User user) {
 9         this.user = user;
10     }
11
12     public void saveUser() {
13         System.out.println("user saved!");
14     }
15 }

然后在配置文件中设置对应<property>元素并指定初值,如下第4~6行中引用外部bean作为初值

1     <bean id="user" class="com.yuan.test.User"></bean>
2
3     <bean id="usersave" class="com.yuan.test.UserSave" scope="singleton">
4         <property name="user">
5             <ref bean="user" />
6         </property>
7     </bean>

构造注入:

为bean提供构造函数。

1 public class UserSave {
2     private User user;
3
4     public UserSave(User user) {
5         this.user = user;
6     }
7 }

修改配置文件,在<constructor-arg>中设置初值

1     <bean id="usersave" class="com.yuan.test.UserSave" scope="singleton">
2         <constructor-arg>
3             <ref bean="user" />
4         </constructor-arg>
5     </bean>

构造注入在有些情况下会遇到些问题,如果有多个参数,我们可以依据类型依次设置配置文件中的参数值,也可以调换顺序,不过要让Spring知道

 1 public class Student {
 2     private String arg1;
 3     private URL arg2;
 4
 5     public Student(String arg1, URL arg2) {
 6         this.arg1 = arg1;
 7         this.arg2 = arg2;
 8     }
 9
10 }

如下是按照顺序,http://www.baidu.com赋给第一个参数arg1, http://www.google.com赋给第二参数。

1 <bean id="student" class="com.yuan.test.Student">
2         <constructor-arg>
3             <value>http://www.baidu.com</value>
4         </constructor-arg>
5         <constructor-arg>
6             <value>http://www.google.com</value>
7         </constructor-arg>
8     </bean>

也可以是随便编写参数的位置,不过要在属性中指定对应的参数位置,如下:

1 <bean id="student" class="com.yuan.test.Student">
2         <constructor-arg index="1">
3             <value>http://www.baidu.com</value>
4         </constructor-arg>
5         <constructor-arg index="0">
6             <value>http://www.google.com</value>
7         </constructor-arg>
8     </bean>

在<constructor-arg>元素中加了index属性,设置对应构造函数中的参数位置。这里http://www.baidu.com赋给arg2, http://www.google.com赋给arg1

1 <bean id="student" class="com.yuan.test.Student">
2         <constructor-arg type="java.net.URL">
3             <value>http://www.baidu.com</value>
4         </constructor-arg>
5         <constructor-arg type="java.lang.String">
6             <value>http://www.google.com</value>
7         </constructor-arg>
8     </bean>

如上,也可以通过type属性设置对应构造函数中的参数类型,不过如果构造函数中有相同的类型参数,这种方法不可取。

自动装配:

关于自动装配,个人觉得不够细粒度,如果为了系统稳定最好还是手工编写好。可以参考文章:了解Spring自动装配

 

Spring中特殊的Bean:

参考文章:Spring笔记 (2) 特殊Bean

分散配置:

在项目开发中,有时候要从一些配置文件中(properties)读取一些配置信息,如数据库的连接信息。在Spring程序中可以从不同的properties中读取有用的信息。这里要用到org.springframeword.beans.factory.config.PropertyPlaceholderConfigurer类,它是BeanFactoryPostProcessor的实现类.

下面例子:

项目结构:

connet.properties:

driver=mysql
username=yuan
password=test

test.properties:

1 age=12
2 height=13
3 weight=14

Connect.java装载着两个properties的信息:

public class Connect {private String driver;private String username;private String password;int age;int height;int weight;//省略getter,setter
}

beans.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 5
 6     <bean id="propertyConfigurer"
 7         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 8         <property name="locations">
 9             <list>
10                 <value>connect.properties</value>
11                 <value>test.properties</value>
12             </list>
13         </property>
14     </bean>
15
16     <bean id="connect" class="com.sunflower.yuan.Connect">
17         <property name="driver">
18             <value>${driver}</value>
19         </property>
20
21         <property name="username">
22             <value>${username}</value>
23         </property>
24
25         <property name="password">
26             <value>${password}</value>
27         </property>
28
29         <property name="age">
30             <value>${age}</value>
31         </property>
32
33         <property name="height">
34             <value>${height}</value>
35         </property>
36
37
38         <property name="weight">
39             <value>${weight}</value>
40         </property>
41     </bean>
42
43 </beans>

第6~14行中,<properties name="locations">标签里面指定要读取的properties文件的位置。16~41行中用EL表达式将预先加载的properties属性注入到Bean中

测试类Test.java:

public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Connect source = (Connect) context.getBean("connect");String username = source.getUsername();String password = source.getPassword();int age = source.getAge();int weight = source.getWeight();int height = source.getHeight();System.out.println("username is:" + username + " password is:"+ password);System.out.println("age is:" + age + " weight is:" + weight+ " height is:" + height);}
}

运行结果:

 

转载于:https://www.cnblogs.com/hanyuan/archive/2012/09/21/2690549.html

spring_装配Bean相关推荐

  1. Spring学习笔记:2(IOC装配Bean之xml方式)

    xml配置方式装配Bean 本文借鉴于:https://www.cnblogs.com/qdhxhz/p/6511887.html Spring框架Bean实例化的方式提供了三种方式实例化Bean 构 ...

  2. SpringInAction--自动化装配Bean(显示装配之xml配置)

    Spring在配置时候有三种方案可选 1.在xml中进行显示配置 2.在java中进行显示配置 3.隐式的Bean发现机制和自动装配 今天学习的 第一种-- 在xml中进行显示配置 老规矩 先创建 C ...

  3. 十年架构师详解,Spring-IoC容器装配Bean

    先看一下Spring容器内部是如何协助的,也就是Spring容器.Bean的配置信息.Bean的实现类及应用程序之间的关系,架构社区:142019080 如下图 由上图我们看到,一个Bean从创建到被 ...

  4. Spring实战之二:装配Bean

    2.1 Spring配置的可选方案 Spring提供了三种装配机制: 在XML中显式配置 在Java中显式配置 隐式的bean发现机制和自动装配 Best Practice:尽可能使用自动配置的机制, ...

  5. 《Spring实战》第四版读书笔记 第二章 装配Bean

    2019独角兽企业重金招聘Python工程师标准>>> 在Spring中,对象无需自己查找或创建与其所关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象. 创建应用对 ...

  6. spring三: 装配bean( 在xml中进行显式配置, 在java中进行显式配置)

    ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); SpringCon ...

  7. spring装配Bean过程

    主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...

  8. Spring Boot2.x-06Spring Boot基础-使用@Conditional注解根据特定的条件装配bean

    文章目录 概述 例子 Step1 实现Condition接口,重写matches方法 Step2 在对应的@Bean上使用@Conditional注解 测试 其他相关的注解 概述 假设在某些特定的场景 ...

  9. Spring Boot2.x-04Spring Boot基础-使用注解装配bean

    文章目录 概述 通过Java配置文件@Bean的方式定义Bean 通过注解扫描的方式(@Component/@ComponentScan)装配Bean 使用excludeFilters属性不让IoC加 ...

最新文章

  1. 局部响应归一化LRN(Local Response Normalization)
  2. PHPRunner中文版
  3. css 中 border 断线解决,简单实用
  4. 数学之美笔记(二十)
  5. 【新星计划】MATLAB plot绘制图像
  6. Boost:bind绑定boost::apply的测试程序
  7. 数据库元数据数据字典查询_7_列出给定表的检查约束
  8. HDOJ HDU 1709 The Balance ACM 1709 IN HDU
  9. HDU - 3360 National Treasures(最小点覆盖-二分图最大匹配+奇偶拆点)
  10. 【java设计模式】迭代子模式
  11. C++中用new和不用new定义类的对象的区别解答
  12. 【故障分析】基于matlab ICA故障监测【含Matlab源码 1590期】
  13. Ubuntu系统下面软件安装更新命令
  14. Android实现访斗鱼视频播放
  15. Deeping Learning学习与感悟——《深度学习工程师》_2
  16. Qt6.2.2+libCef Demo
  17. virtualbox中给redhat安装增强功能
  18. ArcGIS教程:图形处理概述
  19. matlab和robotstudio,一种从Robotstudio环境中导出机器人模型并在MATLAB下使其可视化的研究记录...
  20. 联想E430使用移动硬盘做系统启动

热门文章

  1. tomcat内存溢出(修改catalina.bat后windows启动tomcat服务没有效果) | 王猛的个人主页...
  2. cocos2d-x 3.0 画图节点——Node
  3. 会议容易中吗_【留学评估】美国留学后就业真的和想象中的一样容易吗?
  4. blender使用_DigiVita使用Blender教女孩编码
  5. 第三十一章 考试作弊
  6. Bootstrap3 滚动监听插件的调用方式
  7. Bootstrap 条纹进度条
  8. java学习是什么_学习JAVA有什么作用?
  9. c语言只能最大值不能最小值,用c语言编写输入10个无序的整数,去掉一个最大值和最小值,然后求其平均值...
  10. java 正则提取大于等于号_Java正则表达式