zoukankan      html  css  js  c++  java
  • Spring 框架 (初学)

    在resources中创建 New -> XMLConfiguration File -> SpringConfig -> beans.xml
    先编写一个普通类:

    package com.by;


    public class HelloWord {
    private String message;

    public String getMessage(){
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }
    }

    在beans标签中配置信息 如:
    <bean id="hello"class="com.by.HelloWord"></bean>

    再编写类:

    package com.by;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;


    /**
    * Hello world!
    *
    */
    public class App {
    public static void main( String[] args ) {
    ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");
    HelloWord helloWord = (HelloWord) context.getBean("hello");
    helloWord.setMessage("HELLO WORLD!");
    System.out.println(helloWord.getMessage());
    }

    static void test1(){
    Resource resource = new ClassPathResource("beans.xml");
    BeanFactory beanFactory = new XmlBeanFactory(resource);
    HelloWord helloWord=(HelloWord)beanFactory.getBean("hello");
    helloWord.setMessage("HELLO WORLD!");
    System.out.println(helloWord.getMessage());

    }
    static void test2(){
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    beanDefinitionReader.loadBeanDefinitions("beans.xml");
    BeanFactory factory = beanFactory;
    HelloWord helloWord = (HelloWord) factory.getBean("hello");
    helloWord.setMessage("HELLO WORLD!");
    System.out.println(helloWord.getMessage());
    }

    }

  • 相关阅读:
    STM32F4 SPI双机通讯调试总结
    Altium Designer (DXP) 复制粘贴,放器件 出错报异常的原因
    C++中一个0xC0000005访问冲突问题
    将Windows 7安装到移动固态硬盘(U盘)
    Delphi 的TStringBuilder防止服务器内存碎片化
    Delphi XE5的Android开发平台搭建
    利用RTTI实现Delphi的多播事件代理研究
    博客即将同步至 OSCHINA 社区
    用太极拳讲分布式理论,真舒服!
    记录一次C#调用Delphi编写Dll程序过程
  • 原文地址:https://www.cnblogs.com/liuyunche/p/14230752.html
Copyright © 2011-2022 走看看