IOC装配Bean

(1)Spring框架Bean实例化的方式提供了三种方式实例化Bean
    构造方法实例化(默认无参数,用的最多)
    静态工厂实例化
    实例工厂实例化

下面先写这三种方法的applicationContext.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"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8.  
  9. <!-- Bean的三种实例化方式================================================================================ -->
  10. <!-- 2.1 使用无参的构造器 -->
  11. <bean id="bean1" class="com.study.spring.b_instance.Bean1"></bean>
  12. <!-- 2.2使用静态工厂方法 factory-method 是工厂提供的静态方法 -->
  13. <bean id="bean2" class="com.study.spring.b_instance.Bean2" factory-method="createInstance"></bean>
  14. <!-- 2.3配置实例化工厂的方法 -->
  15. <bean id="bean3Factory" class="com.study.spring.b_instance.Bean3Factory"></bean>
  16. <bean id="bean3" factory-bean="bean3Factory" factory-method="getInstance"></bean>
  17. <!-- end.Bean的三种实例化方式================================================================================ -->
  1. Bean1
  1. public class Bean1 {
  2.  
  3. //必须提供无参的构造函数 系统有默认无参的构造函数
  4. }
  1. Bean2
  1. public class Bean2 {
  2. private static Bean2 Bean2 = new Bean2();
  3.  
  4. private Bean2() {
  5. }
  6.  
  7. public static Bean2 createInstance() {
  8. return Bean2;
  9. }
  10. }

 Bean3类

  1. public class Bean3 {
  2.  
  3. }

Bean3Factory类

  1. public class Bean3Factory {
  2.  
  3. private Bean3Factory(){
  4.  
  5. }
  6.  
  7. public Bean3 getInstance(){
  8. return new Bean3();
  9. }
  10. }

测试类InstanceDemo

  1. import org.junit.Test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4.  
  5. public class InstanceDemo {
  6.  
  7. //实例化工厂方法
  8. @Test
  9. public void demo3(){
  10. //加载配置文件 创建工厂
  11. ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  12.  
  13. Bean3 bean3 =(Bean3) applicationContext.getBean("bean3");
  14. System.out.println(bean3);
  15.  
  16. }
  17.  
  18. //静态工厂方法
  19. @Test
  20. public void demo2(){
  21. //加载配置文件 创建工厂
  22. ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  23.  
  24. Bean2 bean2 =(Bean2) applicationContext.getBean("bean2");
  25. System.out.println(bean2);
  26.  
  27. }
  28. //构造方法得到bean对象
  29. @Test
  30. public void demo1(){
  31. //加载配置文件 创建工厂
  32. ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  33.  
  34. Bean1 bean1 =(Bean1) applicationContext.getBean("bean1");
  35. System.out.println(bean1);
  36.  
  37. }
  38. }
  39. /*
  40. * 这三个都得到类似于com.study.spring.b_instance.Bean1@7229c204 的内存地址
  41. */

(2).Bean的其他配置:

一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称

id 属性在IoC容器中必须是唯一的

id 的命名要满足XML对ID属性命名规范 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

如果Bean的名称中含有特殊字符,就需要使用name属性 例如: <bean name="#person" class="cn.itcast.bean.Person"/>

因为name属性可以相同,所以后出现Bean会覆盖之前出现的同名的Bean

id和name的区别:

id遵守XML约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

name没有这些要求

如果bean标签上没有配置id,那么name可以作为id.

Bean的scope属性

  1. <!-- 3.Bean的scope属性===================================================================== -->
  2. <bean id="product" class="com.study.spring.c_scope.Product" scope="singleton"></bean>
  3. <!-- end.Bean的scope属性===================================================================== -->

     * singleton :单例的.(默认的值.)

    * prototype :多例的.

     * request :web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();

   * session :web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();

   * globalSession :一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

3.Bean属性的依赖注入

前面已经知道如何获得对象,那我们接下来要知道如果给对象对象的属性赋值。

下面通过举例说明:

  1. public class Car {
  2.  
  3. private String name;
  4.  
  5. private double price;
  6.  
  7. public Car(String name, double price) {
  8. super();
  9. this.name = name;
  10. this.price = price;
  11. }
  12.  
  13. @Override
  14. public String toString() {
  15. return "Car [name=" + name + ", price=" + price + "]";
  16. }
  17. }

Car 类

  1. public class Car2 {
  2. private String name;
  3.  
  4. private double price;
  5.  
  6. public void setName(String name) {
  7. this.name = name;
  8. }
  9.  
  10. public void setPrice(double price) {
  11. this.price = price;
  12. }
  13.  
  14. @Override
  15. public String toString() {
  16. return "Car2 [name=" + name + ", price=" + price + "]";
  17. }
  18.  
  19. }

Car2类

  1. public class CarInfo {
  2.  
  3. public String getName(){
  4. return "哈弗H6";
  5. }
  6.  
  7. public double caculatePrice(){
  8. return 110000;
  9. }
  10. }

CarInfo类

  1. import java.util.List;
  2. import java.util.Map;
  3. import java.util.Properties;
  4. import java.util.Set;
  5.  
  6. public class CollectionBean {
  7. private String name;
  8.  
  9. private Integer age;
  10.  
  11. private List<String> hobbies;
  12.  
  13. private Set<Integer> numbers;
  14.  
  15. private Map<String, String> map;
  16.  
  17. private Properties properties;
  18.  
  19. public String getName() {
  20. return name;
  21. }
  22.  
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26.  
  27. public Integer getAge() {
  28. return age;
  29. }
  30.  
  31. public void setAge(Integer age) {
  32. this.age = age;
  33. }
  34.  
  35. public List<String> getHobbies() {
  36. return hobbies;
  37. }
  38.  
  39. public void setHobbies(List<String> hobbies) {
  40. this.hobbies = hobbies;
  41. }
  42.  
  43. public Set<Integer> getNumbers() {
  44. return numbers;
  45. }
  46.  
  47. public void setNumbers(Set<Integer> numbers) {
  48. this.numbers = numbers;
  49. }
  50.  
  51. public Map<String, String> getMap() {
  52. return map;
  53. }
  54.  
  55. public void setMap(Map<String, String> map) {
  56. this.map = map;
  57. }
  58.  
  59. public Properties getProperties() {
  60. return properties;
  61. }
  62.  
  63. public void setProperties(Properties properties) {
  64. this.properties = properties;
  65. }
  66.  
  67. @Override
  68. public String toString() {
  69. return "CollectionBean [name=" + name + ", age=" + age + ", hobbies=" + hobbies + ", numbers=" + numbers
  70. + ", map=" + map + ", properties=" + properties + "]";
  71. }
  72.  
  73. }

CollectionBean类

  1. public class Employee {
  2.  
  3. private String name;
  4.  
  5. private Car2 car2;
  6.  
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10.  
  11. public void setCar2(Car2 car2) {
  12. this.car2 = car2;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return "Employee [name=" + name + ", car2=" + car2 + "]";
  18. }
  19.  
  20. }

Employee类

  1. import org.junit.Test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4.  
  5. public class TestDi {
  6.  
  7. @Test
  8. public void demo6() {
  9. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  10.  
  11. CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
  12.  
  13. System.out.println(collectionBean);
  14. }
  15.  
  16. @Test
  17. public void demo5() {
  18. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  19.  
  20. Car2 car2 = (Car2) applicationContext.getBean("car2_2");
  21.  
  22. System.out.println(car2);
  23. }
  24.  
  25. @Test
  26. public void demo4() {
  27. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  28.  
  29. Employee e = (Employee) applicationContext.getBean("employee2");
  30.  
  31. System.out.println(e);
  32. }
  33.  
  34. @Test
  35. public void demo3() {
  36. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  37.  
  38. Employee e = (Employee) applicationContext.getBean("employee");
  39.  
  40. System.out.println(e);
  41. }
  42.  
  43. @Test
  44. public void demo2() {
  45. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  46.  
  47. Car2 car2 = (Car2) applicationContext.getBean("car2");
  48.  
  49. System.out.println(car2);
  50. }
  51.  
  52. @Test
  53. public void demo1() {
  54. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  55.  
  56. Car car = (Car) applicationContext.getBean("car");
  57.  
  58. System.out.println(car);
  59. }
  60. }

TestDi测试类

  1. 上面这几个类都不是最主要的,我们主要是来看配置文件怎么写,这才是最关键的: 
  2.  
  3. applicationContext.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"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  8.  
  9. <!-- Bean的依赖注入===================================================================================== -->
  10. <!-- 4.1构造器注入 -->
  11. <bean id="car" class="com.study.spring.e_di.Car">
  12. <!-- 方式一.根据索引的位置 -->
  13. <!-- <constructor-arg index="0" value="保时捷"></constructor-arg>
  14. <constructor-arg index="1" value="1500000"></constructor-arg> -->
  15. <!-- 方式二.根据名字配置 -->
  16. <!-- <constructor-arg name="name" value="宝马"></constructor-arg>
  17. <constructor-arg name="price" value="500000"></constructor-arg> -->
  18. <!-- 方式三.根据类型配置 -->
  19. <constructor-arg type="java.lang.String" value="奔驰"></constructor-arg>
  20. <constructor-arg type="double" value="600000"></constructor-arg>
  21. </bean>
  22.  
  23. <!-- 4.2setter方法中注入 -->
  24. <bean id="car2" class="com.study.spring.e_di.Car2">
  25. <property name="name" value="雪佛兰"></property>
  26. <property name="price" value="100000"></property>
  27. </bean>
  28.  
  29. <bean id="employee" class="com.study.spring.e_di.Employee">
  30. <property name="name" value="张三"></property>
  31. <property name="car2" ref="car2"></property>
  32. </bean>
  33.  
  34. <!-- 引用p命名空间 --><!-- 如果要引用p命名,那在最上面sxd中就要配置 xmlns:p="http://www.springframework.org/schema/p"-->
  35. <bean id="car22" class="com.study.spring.e_di.Car2" p:name="宝马" p:price="500000">
  36. </bean>
  37. <bean id="employee2" class="com.study.spring.e_di.Employee" p:name="李四" p:car2-ref="car22"></bean>
  38.  
  39. <!-- 引入spEL表达式 -->
  40. <bean id="carInfo" class="com.study.spring.e_di.CarInfo"></bean>
  41. <bean id="car2_2" class="com.study.spring.e_di.Car2">
  42. <property name="name" value="#{carInfo.name}"></property>
  43. <property name="price" value="#{carInfo.caculatePrice()}"></property>
  44. </bean>
  45.  
  46. <!-- 复杂属性的依赖注入 -->
  47. <bean id="collectionBean" class="com.study.spring.e_di.CollectionBean">
  48. <!-- 简单属性的注入 -->
  49. <property name="name" value="归谷"></property>
  50. <property name="age" value="12"></property>
  51. <!-- 注入list集合 -->
  52. <property name="hobbies">
  53. <list>
  54. <value>吃饭</value>
  55. <value>睡觉</value>
  56. <value>敲代码</value>
  57. </list>
  58. </property>
  59.  
  60. <!-- 注入set集合 -->
  61. <property name="numbers">
  62. <set>
  63. <value>10</value>
  64. <value>20</value>
  65. <value>30</value>
  66. <value>40</value>
  67. <value>50</value>
  68. </set>
  69. </property>
  70. <!-- 注入map集合 -->
  71. <property name="map">
  72. <map>
  73. <entry key="birthday" value="2017-1-1"></entry>
  74. <entry key="address" value="杭州西湖"></entry>
  75. <entry key="sex" value="female"></entry>
  76. </map>
  77. </property>
  78.  
  79. <!-- 注入Properties -->
  80. <property name="properties">
  81. <props>
  82. <prop key="compamy">杭州余杭未来科技城</prop>
  83. <prop key="pnum">200</prop>
  84. </props>
  85. </property>
  86. </bean>
  87.  
  88. <!-- end Bean的依赖注入===================================================================================== -->
  89. <import resource="classpath:bean1.xml"/>
  90. <import resource="classpath:bean2.xml"/>
  91. <!-- 这里导入是指如果在src下还有其它的beans.xml我们可以这样去调用 -->
  92.  
  93. </beans>

有关applicationContext.xml这个配置文件里的内容一定要看懂,我写的还是比较基础和全面的。

有关命名空间p的使用我这里在解释下:

p:<属性名>="xxx" 引入常量值

p:<属性名>-ref="xxx" 引用其它Bean对象

关于这篇文章,我就写到这里,不足之处,欢迎大家多多指点,谢谢!

Spring框架---IOC装配Bean的更多相关文章

  1. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

  2. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  3. Spring框架(2)---IOC装配Bean(xml配置方式)

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  4. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  5. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  6. IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注解 ...

  7. Spring框架IOC容器和AOP解析 非常 有用

    Spring框架IOC容器和AOP解析   主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...

  8. spring中自动装配bean

    首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...

  9. Spring总结 1.装配bean

    本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...

随机推荐

  1. windows中如何查看某个端口被谁占用

    说明:本人操作系统为win7 x64,文章转自http://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html,加上本人的注释. 开始--- ...

  2. 解决Apache的错误日志巨大的问题以及关闭Apache web日志记录

    调整错误日志的级别 这几天 apache错误日志巨大 莫名其妙的30G  而且 很多都是那种页面不存在的  网站太多了  死链接相应的也很多于是把错误警告调低了 因为写日志会给系统带来很大的损耗.关闭 ...

  3. easyUI 添加排序到datagrid

    http://www.cnblogs.com/javaexam2/archive/2012/08/10/2632645.html

  4. V8引擎嵌入指南

    如果已读过V8编程入门那你已经熟悉了如句柄(handle).作用域(scope)和上下文(context)之类的关键概念,以及如何将V8引擎作为一个独立的虚拟机来使用.本文将进一步讨论这些概念,并介绍 ...

  5. Oracle job调用存储过程

    在PL/SQL中的what值中直接写入存储过程的名称+“分号”: begin sys.dbms_job.submit(job => :job, what => 'del_ky_items; ...

  6. java配置环境变量-及原因

    为什么java要配置环境变量? 那就要从java的编译和解析过程说起 java文件的编译和解析过程 一.一个hellow.java文件,要经历先编译(变成hellow.class),再解析(解析成机器 ...

  7. com.atomikos.icatch.HeurHazardException: Heuristic Exception

    com.atomikos.icatch.HeurHazardException: Heuristic Exception: 删除Tomcat  bin文件夹下的spring.loglog4j.appe ...

  8. 转载:MyEclipse安装插件的几种方法

    地址:http://www.cnblogs.com/pharen/archive/2012/02/08/2343342.html 本文讲解MyEclipse(MyEclipse10)的三种方法,以SV ...

  9. ESLint 规则详解(一)

    前端界大神 Nicholas C. Zakas 在 2013 年开发的 ESLint,极大地方便了大家对 Javascript 代码进行代码规范检查.这个工具包含了 200 多条 Javascript ...

  10. PHP编码规范实例

    <?php   /**   * 符合psr-1,2的编程实例   *   * @author 作者 描述   */       namespace Standard; // 顶部命名空间   / ...