Spring介绍、安装、helloworld

Spring 4.0

描述

Spring(官网:projects.spring.io)是一个框架,为了简化企业级应用开发而生,使用Spring可以使简单的javabean实现以前只有gjb才能实现的功能。

安装

下载spring的jar包,在eclipse中选择help–>install new software–>add–>archive,然后找到下载的安装包,选中,在选择功能中选择带有spring IDE的项目,安装重启即可.
若在window–> preperences可以找到Spring选项,表示安装成功

hello world

1、新建Java project
2、导入
commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
五个包
3、新建一个bean类

public class Person {

    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void run(){
        System.out.println(name+ " running");
    }
}

4、创建一个Spring配置文件,即new–>Spring Bean Configuration File

<?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 = "person" class="com.spring.test.Person">
    <property name="name" value = "spring"></property>

    </bean>
</beans>

5、编写main

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

public class Main {

    public static void main(String[] args) {
        ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) application.getBean("person");
        person.run();
    }

}

6、运行

一月 09, 2018 10:16:10 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@28fa1b85: startup date [Tue Jan 09 22:16:10 CST 2018]; root of context hierarchy
一月 09, 2018 10:16:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
spring running
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值