Spring 入门知识点(1)—— 环境搭建(hello world)

1.spring 体系结构

这里写图片描述
体系结构介绍在这里

2.构建maven 项目
  • 创建maven项目
    这里写图片描述
  • 选择模板
    这里写图片描述
  • 填写参数
    这里写图片描述
  • 项目结构
    这里写图片描述
3. 编写helloworld 类
package hello;

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

public class TestHello {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:Beans.xml");
        HelloWorld helloworld = (HelloWorld) applicationContext.getBean("helloWorld");
        helloworld.setMessage("hello world,my spring first program");
        System.out.println(helloworld.getMessage());

    }

}
4.配置bean
<?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="hello.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>
5.测试类
package hello;

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

public class TestHello {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:Beans.xml");
        HelloWorld helloworld = (HelloWorld) applicationContext.getBean("helloWorld");
        helloworld.setMessage("hello world,my spring first program");
        System.out.println(helloworld.getMessage());    
    }
}
6.结果

这里写图片描述

完!

猜你喜欢

转载自blog.csdn.net/qq_17639593/article/details/80560963