你最愿意做的哪件事,才是你的天赋所在

0%

Spring-Studyday-1

用Spring写一个HelloWorld

环境

  1. jdk 1.7
  2. eclipse 2017
  3. spring 4.2.5
  4. common-logging 1.2

    创建项目

    创建eclipse文件,导入必要的jar包
    image.png

    编写程序

    编写HelloWorld与xml文件以及MainAPP文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //HelloWorld.java
    package cn.suock.spring;
    public class HelloWorld {
    private String message;
    public void setMessage(String message){
    this.message = message;
    }
    public void getMessage(){
    System.out.println("Your Message : " + message);
    }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
//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-3.0.xsd">

<bean id="helloWorld" class="cn.suock.spring.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

</beans>
1
2
3
4
5
6
7
8
9
10
11
12
//MainAPP
package cn.suock.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}

运行程序

image.png

-------------你最愿意做的哪件事才是你的天赋所在-------------