zoukankan      html  css  js  c++  java
  • Spring基础——一个简单的例子

    一、学习版本 spring-framework-4.0.0

    二、导入 jar 包:

    三、在类路径下创建 Spring Config 文件:ApplicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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.xsd">
    </beans>

    四、创建一个非侵入的 Bean

    /**
     * @author solverpeng
     * @create 2016-07-15-10:09
     */
    public class HelloWorld {
        private String userName;
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public void hello() {
            System.out.println("hello:" + userName);
        }
    }

    五、在 Spring Config 文件中配置该 Bean

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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.xsd">
        <bean id="helloWorld" class="com.nucsoft.spring.helloworld.HelloWorld">
            <property name="userName" value="spring"/>
        </bean>
    </beans>

    六、通过 IOC 容器对象来获取在 Spring Config 文件中配置的 Bean,并调用其方法

    public static void main(String[] args) {
      ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml");
      HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
      helloWorld.hello(); // 输出 hello:spring
    }

    七、总结

    两个重点:

    1.在 Spring Config 中配置 Bean

    2.通过 IOC 容器中获取 Sring Config 配置的 Bean 对象

  • 相关阅读:
    Pascal Analyzer 4 代码分析使用简要说明
    界面动态配置:持久化反持久化
    (Sql Server)数据的拆分和合并
    javascript中的promise和deferred:实践(二)
    对面向接口编程、按分层建项目的反思和新的分层结构思路
    revel框架教程之权限控制
    等待与通知机制
    实现代码编辑器
    自定义html标签
    javascript生成自定义的arcgis simpletoolbar
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5673330.html
Copyright © 2011-2022 走看看