• spring hello world 入门


    参考:http://c.biancheng.net/view/4244.html

    1.下载包

    2.新建接口+类

    3.新建xml文件

    4.测试

    package com.ligy.ioc;
    
    public interface IStudent {
        void do1();
    }
    package com.ligy.ioc;
    
    public class StudentImpl implements IStudent {
        @Override
        public void do1() {
            System.out.println("this is in do1");
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
        <!-- 由 Spring容器创建该类的实例对象 -->
        <bean id="studentDao" class="com.ligy.ioc.StudentImpl" />
    </beans>
    package com.ligy.test;
    
    import com.ligy.ioc.StudentImpl;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class Test1 {
    
        @Test
        public void do1() {
            // 定义Spring配置文件的路径
            String xmlPath = "applicationContext.xml";
            // 初始化Spring容器,加载配置文件
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    xmlPath);
            // 通过容器获取personDao实例
            StudentImpl personDao = (StudentImpl) applicationContext
                    .getBean("studentDao");
            // 调用 personDao 的 add ()方法
            personDao.do1();
        }
    }

     

    天生我材必有用,千金散尽还复来
  • 相关阅读:
    专题三--1005
    专题三--1009
    专题三--1017
    背包九讲
    专题三--1003
    专题三--1004
    专题三--1015
    [洛谷P1220]关路灯
    [洛谷P1776]宝物筛选
    [USACO14JAN]Recording the Moolympics
  • 原文地址:https://www.cnblogs.com/ligenyun/p/14743453.html
Copyright © 2020-2023  润新知