• ClassPathXmlApplicationContext的使用


    转载自:https://blog.csdn.net/u012397322/article/details/87545950

    核心在创建IoC容器对象,用容器获取bean实例,有了实例就可以根据API提供的方法使用了。 而且,一个IoC容器可以获取多个实例:
    // 1. 创建 Spring 的 IOC 容器
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

    //2. 从 IOC 容器中获取 bean 的实例
    HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); Car car = (Car) ctx.getBean("car");

    //这里是重复的例子,用bean.xml中的id = car
    Car car2 = (Car) ctx.getBean(“car2”);
    //3. 使用 bean //有了实例,就可以通过实例来调用实例的方法,那么实例有哪些方法,文档中是要写清楚的。方法可以直接用。

    1 helloWorld.setUser("chao");
    2         helloWorld.hello();//hello这个方法,要做什么事情,也是要在方法里面写清楚的。比如这里我输入了setUser的"chao"参数,那么返回的是hello chaobin
    3         

    测试类Main:

     1 package com.atguigu.spring.helloworld;
     2 
     3 import org.springframework.context.support.ClassPathXmlApplicationContext;
     4 
     5 public class Main {
     6     
     7     public static void main(String[] args) {
     8         
     9 //        HelloWorld helloWorld = new HelloWorld();
    10 //        helloWorld.setUser("Tom");
    11 //        helloWorld.hello(); 
    12         
    13         //1. 创建 Spring 的 IOC 容器
    14         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    15 
    16         //2. 从 IOC 容器中获取 bean 的实例
    17         HelloWorld  helloWorld = (HelloWorld) ctx.getBean("helloWorld");
    18         //是通过bean.xml里面定义的id来定位获取哪个bean实例的。@lcb
    19          System.out.println("bean实例的样子是"+ ctx);
    20          ctx.getBean("car");//获取的bean实例是什么样子的?
    21         //org.springframework.context.support.ClassPathXmlApplicationContext@b81eda8: startup date [Sun Feb 17 17:40:32 CST 2019]; root of context hierarchy
    22         //可以看到bean实例,是一个runtime instance. 有一个生命周期。
    23 
    24         //根据类型来获取 bean 的实例: 要求在  IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常. 
    25         //一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个. 
    26 //        HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
    27         
    28         //3. 使用 bean
    29         //有了实例,就可以通过实例来调用实例的方法,那么实例有哪些方法,文档中是要写清楚的。方法可以直接用。
    30         helloWorld.setUser("chao");
    31         helloWorld.hello();//hello这个方法,要做什么事情,也是要在方法里面写清楚的。比如这里我输入了setUser的"chao"参数,那么返回的是hello chaobin
    32         
    33         Car car = (Car) ctx.getBean("car"); //这里是重复的例子,用bean.xml中的id = car 得到实例。
    34         System.out.println(car);
    35         
    36         Car car2 = (Car) ctx.getBean("car2");
    37         //@lcb 这里必须进行一次墙砖,原因如下:
    38         //Incompatible types.
    39         //Required:
    40         //com.atguigu.spring.helloworld.Car
    41         //Found:
    42         //java.lang.Object
    43 
    44         System.out.println(car2);
    45         
    46         //4. 测试使用集合属性
    47         User user = (User) ctx.getBean("user5");
    48         System.out.println(user);
    49     }
    50     
    51 }

    beans.xml 文件 (会被作为参数传给ClassPathXmlApplicationContext)

      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:util="http://www.springframework.org/schema/util"
      5     xmlns:p="http://www.springframework.org/schema/p"
      6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
      8     
      9     <!-- 配置一个 bean -->
     10     <bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld">
     11         <!-- 为属性赋值 -->
     12         <property name="user" value="Jerry"></property>
     13     </bean>
     14     
     15     <!-- 配置一个 bean -->
     16     <bean id="helloWorld2" class="com.atguigu.spring.helloworld.HelloWorld">
     17         <!-- 为属性赋值 -->
     18         <!-- 通过属性注入: 通过 setter 方法注入属性值 -->
     19         <property name="user" value="Tom"></property>
     20     </bean>
     21     
     22     <!-- 通过构造器注入属性值 -->
     23     <bean id="helloWorld3" class="com.atguigu.spring.helloworld.HelloWorld">
     24         <!-- 要求: 在 Bean 中必须有对应的构造器.  -->
     25         <constructor-arg value="Mike"></constructor-arg>
     26     </bean>
     27     
     28     <!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
     29     <!-- 可以根据 index 和 value 进行更加精确的定位. (了解) -->
     30     <bean id="car" class="com.atguigu.spring.helloworld.Car">
     31         <constructor-arg value="KUGA" index="1"></constructor-arg>
     32         <constructor-arg value="ChangAnFord" index="0"></constructor-arg>
     33         <constructor-arg value="250000" type="float"></constructor-arg>
     34     </bean>
     35     
     36     <bean id="car2" class="com.atguigu.spring.helloworld.Car">
     37         <constructor-arg value="ChangAnMazda"></constructor-arg>
     38         <!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. (了解) -->
     39         <constructor-arg>
     40             <value><![CDATA[<ATARZA>]]></value>
     41         </constructor-arg>
     42         <constructor-arg value="180" type="int"></constructor-arg>
     43     </bean>
     44     
     45     <!-- 配置 bean -->
     46     <bean id="dao5" class="com.atguigu.spring.ref.Dao"></bean>
     47 
     48     <bean id="service" class="com.atguigu.spring.ref.Service">
     49         <!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
     50         <property name="dao" ref="dao5"></property>
     51     </bean>
     52     
     53     <!-- 声明使用内部 bean -->
     54     <bean id="service2" class="com.atguigu.spring.ref.Service">
     55         <property name="dao">
     56             <!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
     57             <bean class="com.atguigu.spring.ref.Dao">
     58                 <property name="dataSource" value="c3p0"></property>
     59             </bean>
     60         </property>
     61     </bean>
     62     
     63     <bean id="action" class="com.atguigu.spring.ref.Action">
     64         <property name="service" ref="service2"></property>
     65         <!-- 设置级联属性(了解) -->
     66         <property name="service.dao.dataSource" value="DBCP2"></property>
     67     </bean>
     68     
     69     <bean id="dao2" class="com.atguigu.spring.ref.Dao">
     70         <!-- 为 Dao 的 dataSource 属性赋值为 null, 若某一个 bean 的属性值不是 null, 使用时需要为其设置为 null(了解) -->
     71         <property name="dataSource"><null/></property>
     72     </bean>
     73     
     74     <!-- 装配集合属性 -->
     75     <bean id="user" class="com.atguigu.spring.helloworld.User">
     76         <property name="userName" value="Jack"></property>
     77         <property name="cars">
     78             <!-- 使用 list 元素来装配集合属性 -->
     79             <list>
     80                 <ref bean="car"/>
     81                 <ref bean="car2"/>
     82             </list>
     83         </property>
     84     </bean>
     85     
     86     <!-- 声明集合类型的 bean -->
     87     <util:list id="cars">
     88         <ref bean="car"/>
     89         <ref bean="car2"/>
     90     </util:list>
     91     
     92     <bean id="user2" class="com.atguigu.spring.helloworld.User">
     93         <property name="userName" value="Rose"></property>
     94         <!-- 引用外部声明的 list -->
     95         <property name="cars" ref="cars"></property>
     96     </bean>
     97     
     98     <bean id="user3" class="com.atguigu.spring.helloworld.User"
     99         p:cars-ref="cars" p:userName="Titannic"></bean>
    100         
    101     <!-- bean 的配置能够继承吗 ? 使用 parent 来完成继承 -->    
    102     <bean id="user4" parent="user" p:userName="Bob"></bean>
    103     
    104     <bean id="user6" parent="user" p:userName="维多利亚"></bean>
    105     
    106     <!-- 测试 depents-on -->    
    107     <bean id="user5" parent="user" p:userName="Backham" depends-on="user6"></bean>
    108     
    109 </beans>

    HelloWorld类

     1 package com.atguigu.spring.helloworld;
     2 
     3 public class HelloWorld {
     4 
     5     private String user;
     6     
     7     public HelloWorld() {
     8         System.out.println("HelloWorld's constructor...");
     9     }
    10     
    11     public void setUser(String user) {
    12         System.out.println("setUser:" + user);
    13         this.user = user;
    14     }
    15     
    16     public HelloWorld(String user) {
    17         this.user = user;
    18     }
    19 
    20     public void hello(){
    21         System.out.println("Hello: " + user);
    22     }
    23     
    24 }
  • 相关阅读:
    软件需求变更管理
    Flex自定义事件
    Flex基础控件Button
    Ext JS高级程序设计》即将隆重上市(预计上市时间:091115)
    迈向工程硕士
    Flex基础控件ComboBox
    ExtJS 4 Beta 2预览:Ext.Brew包
    ExtJS 4 Grid组件
    REST WebService与SOAP WebService的比较
    Step by Step WebMatrix网站开发之一:Webmatrix安装
  • 原文地址:https://www.cnblogs.com/Yi-ling/p/14318046.html
Copyright © 2020-2023  润新知