spring mvc入门

来源:互联网 发布:mac键盘上撇符号怎么打 编辑:程序博客网 时间:2024/06/05 09:54

spring官网下载地址:http://repo.spring.io/release/org/springframework/spring/

1、导入必要的spring jar

commons-logging-1.2.jar
spring-beans-4.3.5.RELEASE.jar
spring-context-4.3.5.RELEASE.jar
spring-core-4.3.5.RELEASE.jar
spring-expression-4.3.5.RELEASE.jar

2、创建一个对象

Hello.java

package com.spring.test;public class Hello {    public void doHello(){        System.out.println("doHello");    }}

3、创建一个spring xml配置文件

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 definitions here -->    <bean id="hello" class="com.spring.test.Hello"></bean></beans>

4、写个jUnit test

package com.spring.test;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloTest {    @Test    public void test() {        fail("Not yet implemented");    }    @Test    public void testdoHello(){        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        Hello  hello = (Hello)applicationContext.getBean("hello");        hello.doHello();    }}

运行就能看到效果了

2 3
原创粉丝点击