001. Spring HelloWorld

来源:互联网 发布:监控员工上网的软件 编辑:程序博客网 时间:2024/06/15 15:37

1、创建Java项目:File -> New -> Java Project

2、引入必要jar包,项目结构如下
项目结构

3、创建HelloWorld服务类HelloWorld.java

package com.spring.service;public class HelloWorld {    public void welcome() {        System.out.println("Welcome to spring framework!");    }}

4、创建Spring配置文件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="helloWorld" class="com.spring.service.HelloWorld"></bean></beans>

5、创建Spring测试类SpringUnit.java

package com.spring.junit;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.service.HelloWorld;public class SpringUnit {    @Test    public void test() {        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");        helloWorld.welcome();        ctx.close();    }}

6、测试结果

... 省略Spring日志信息 ...Welcome to spring framework!... 省略Spring日志信息 ...
原创粉丝点击