Spring入门之HelloWorld
2016-04-27 01:08
375 查看
1.新建java项目 Spring
2.导入jar包
2.1 到http://www.springsource.org/download下载spring,然后进行 解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
2.2 spring的核心类库 在spring文档的dist下 dist\spring.jar
2.3 引入的第三方类库 都spring文档的lib下lib\jakarta-commons\commons-logging.jar
3.新建java类HelloWorld.java,位于包spring.helloworld下
4.在包spring.helloworld下新建applicationContext.xml文件,配置如下:
5.新建测试类HelloWorldTest.java
6.运行test方法,可以得到如下结果:
还可以在applicationContext.xml中为java类配置别名,比如:
2.导入jar包
2.1 到http://www.springsource.org/download下载spring,然后进行 解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
2.2 spring的核心类库 在spring文档的dist下 dist\spring.jar
2.3 引入的第三方类库 都spring文档的lib下lib\jakarta-commons\commons-logging.jar
3.新建java类HelloWorld.java,位于包spring.helloworld下
public class HelloWorld { public void hello(){ System.out.println("hello world"); } }
4.在包spring.helloworld下新建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-2.5.xsd"> <!-- beans:把一个类放入到spring容器中,此类就称为bean --> <!-- 一个bean就代表一个类 id就是唯一标识符 --> <bean id="helloWorld" class="spring.helloworld.HelloWorld"></bean> </beans>
5.新建测试类HelloWorldTest.java
public class HelloWorldTest { @Test public void test(){ /** * 1.启动spring容器 * 2.从spring容器中把helloWorld对象取出来 * 3.对象.方法 */ //启动spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("spring/helloworld/applicationContext.xml"); HelloWorld helloworld = (HelloWorld) context.getBean("helloWorld"); helloworld.hello(); } }
6.运行test方法,可以得到如下结果:
hello world
还可以在applicationContext.xml中为java类配置别名,比如:
<alias name="helloWorld" alias="不倒翁"/>
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- C#学习进阶Hello World的17种写法代码分享
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式