首页 > 代码库 > Spring入门第一课

Spring入门第一课

今天先不讲Spring是什么。

Spring网址:http://projects.spring.io/spring-framework/

Eclipse 安装开发IDE

在Eclipse Marketplace搜索spring,然后直接安装。

技术分享

技术分享

技术分享

技术分享

下载spring的Jar包

http://repo.spring.io/simple/libs-release-local/org/springframework/spring/

下载4.3.8的zip包

 

先直接看代码。

目录结构

技术分享

代码

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">
    <bean id="helloService" class="com.study.spring.a_quickstart.HelloServiceImpl">
    </bean>

</beans>
package com.study.spring.a_quickstart;

public interface HelloService {
    
    public void sayHello();

}
package com.study.spring.a_quickstart;

public class HelloServiceImpl implements HelloService {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("hello spring!");

    }

}
package com.study.spring.a_quickstart;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HelloServiceImpl service = new HelloServiceImpl();
        service.sayHello();
        ApplicationContext ext = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloService helloService = (HelloService) ext.getBean("helloService");
        helloService.sayHello();

    }

}

下面是输出结果:

hello spring!
五月 17, 2017 10:13:04 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Wed May 17 22:13:04 CST 2017]; root of context hierarchy
五月 17, 2017 10:13:04 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
hello spring!

 

Spring入门第一课