码迷,mamicode.com
首页 > 编程语言 > 详细

Spring入门第一课

时间:2017-05-17 23:31:35      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:nbsp   auto   star   res   第一课   结果   stat   and   project   

今天先不讲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入门第一课

标签:nbsp   auto   star   res   第一课   结果   stat   and   project   

原文地址:http://www.cnblogs.com/LoganChen/p/6869824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!