Spring4之Hello World
来源:互联网 发布:淘宝宝贝视频制作 编辑:程序博客网 时间:2024/06/08 11:26
1、在项目中添加所需的jar:http://blog.csdn.net/mrw456/article/details/49835463
2、编写一个bean类SpringHello
package com.mrw.springtest1;
public class SpringHello {
public void SayHello(){
System.out.println("Hello World!");
}
}
3、新建一个xml文件,命名为beans.xml(xml文件可以随便命名,beans比较大众化吧),把beans.xml放到项目的src目录下,将上面的SpringHello添加到beans.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="SpringHello" name="SpringHello"
class="com.mrw.springtest1.SpringHello">
</bean>
</beans>
4、编写一个测试类Test1
package com.mrw.springtest1;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
@Test
public void test() {
- ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");//这里之所以没有加上路径是因为beans.xml位于src目录下
- BeanFactory context=new XmlBeanFactory(new ClassPathResource("beans.xml"));
//以上两种方式创建Spring context都可以
SpringHello hello=(SpringHello)context.getBean("SpringHello");
hello.SayHello(); }
}
5、运行结果
- Spring4之Hello World
- Spring4--Hello World实例
- Spring4 Hello World!
- Spring4.2.7 - Drools6.4 Hello World
- Spring4学习笔记(一): Hello world
- spring4 mvc hello world example [tomcat7][译]
- hibernate之Hello world
- Spring之Hello World
- Lucene之Hello world
- DWR之HELLO world
- JNI 之 hello world
- ISAPI之Hello world
- Spring之Hello World
- ACE之hello world
- Hello,world之Erlang
- DWR之 Hello world
- android之Hello World!
- Android之Hello World
- 为一个dialog自定义布局文件
- java ssl双向验证httpclient客户端请求工具类
- html5基础知识第四章其他标签
- 侧滑菜单1
- 4412裸机程序之点亮LED
- Spring4之Hello World
- Spark修炼之道(基础篇)——Linux大数据开发基础:第三节:用户和组
- Timus Online Judge 1966 Cycling Roads
- 装饰器模式-JAVA
- poj3468(线段树||树状数组)
- Head First Java勘误
- Unity Sharder帧动画
- 软件使用技巧2:EXCEL工作表保护密码的撤销
- 被驱动表 拼接列无法走索引