zoukankan      html  css  js  c++  java
  • 02_setter注入

    工程截图如下

    【HelloWorld.java】

     1 package com.HigginCui;
     2 
     3 public class HelloWorld {
     4     private String words;
     5 
     6     public void hello(){
     7         System.out.println(this.getWords());
     8     }
     9     public String getWords() {
    10         return words;
    11     }
    12     public void setWords(String words) {
    13         this.words = words;
    14     }
    15 }

    【applicationContext.xml】

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:aop="http://www.springframework.org/schema/aop"
           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-2.5.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <!-- 
            <beans>: 作为根节点
             <bean>: 来为每一个bean进行设定
                       id:设定bean的实例别名,可以用id来获取Bean的实例
                       class:用来指定bean的对应的类名称,即该bean的类路径
         <property>: 设定了setter的名称,此时使用了setWords()
              name:属性的名称 <value>: 将"Hello World!!"注入HelloWorld类的变量words
    --> <bean id="helloWorld" class="com.HigginCui.HelloWorld"> <property name="words"> <value>Hello World!!</value> </property> </bean> </beans>

    【testHelloWorld.java】

     1 package com.HigginCui.test;
     2 import org.junit.Test;
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 import com.HigginCui.HelloWorld;
     6 
     7 public class testHelloWorld {
     8     @Test
     9     public void testHelloWorld(){
    10         //1.启动Spring容器
    11         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    12         //2.从Spring容器中把对象提取出来,获取实例,此时的"helloWorld"即applicationContext.xml文件中的id="helloWorld"
    13         HelloWorld helloWorld=(HelloWorld) context.getBean("helloWorld");
    14         //3.对象调用方法
    15         helloWorld.hello();
    16     }
    17 }

    【运行结果】

    Hello World!!

    【小结】

    ApplicationContext:继承自BeanFactory接口,即“应用前后关系”,除了包含BeanFactory的所有功能之外,还支持国际化、资源访问(如URL和文件)、事件传播等等。

    ClassPathXmlApplicationContext:从类路径ClassPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。

    //装载单个配置文件实例化ApplicationContext容器
    ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
    //装载多个配置文件实例化ApplicationContext容器
    String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
    ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
  • 相关阅读:
    最近我总结的常用mate标签-常用mate标签
    同一个世界(erlang解题答案)
    ranch 源码分析(完)
    ranch 源码分析(三)
    ranch 源码分析(二)
    ranch 源码分析(一)
    port 执行命令的封装和参数详解
    erlang 笔记(06/03/02)
    recon工具解读
    erlang调试方法
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5573455.html
Copyright © 2011-2022 走看看