第一个Spring程序
2012-02-07 11:31
204 查看
转自:http://www.blogjava.net/wshsdlau/archive/2011/04/28/349181.html
如下分成5个步骤
1,建立xml文件
2,建立bean的接口
3,建立bean
4,写测试程序
5,测试
准备工作
环境配置如下,需要spring.jar和common-logging.jar两个jar文件
开始
1,建立xml文件
文件名:beans.xml
文件位置:src目录下
文件内容:
<?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">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean id="sayhello" class="test.service.impl.HelloBean"/>
</beans>
2,建立bean的接口
文件名:Hello.java
文件内容:
package test.service;
public interface Hello {
public void sayHello();
}
3,建立bean
文件名:HelloBean.java
文件内容:
package test.service.impl;
import test.service.Hello;
public class HelloBean implements Hello {
/* (non-Javadoc)
* @see test.service.impl.Hello#sayHello()
*/
public void sayHello() {
System.out.println("这是一个测试程序");
}
}
4,写测试程序
文件名:FirstSpring.java
文件内容:
package test.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.service.Hello;
public class FirstSpring {
public static void main(String[] args) {
testHello();
}
public static void testHello() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Hello hello =(Hello) applicationContext.getBean("sayhello");
hello.sayHello();
}
}
5,测试
运行FirstSpring.java文件,得到输出结果如下:
2009-6-30 3:33:58 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2009-6-30 3:33:59 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@7259da]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7820
2009-6-30 3:33:59 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7820: defining beans [sayhello]; root of factory hierarchy
这是一个测试程序
上面红字是spring输出的调试信息,蓝字是hellobean实际输入的内容。
简单总结:
1,环境配置中不要忘记了common-logging.jar文件,我最开始忘记了,还是用junit测试的,结果就是不通过。出错的原因也不明白。后来直接改成普通的main方法测试,才明白原因。
2,bean的接口和实现的分离在spring中被贯彻执行。同时理解一下IOC(控制反转)的概念。
3,spring中的bean,应该指的是执行各种业务的业务bean才是。不同于strut的formbean和对应db表对象的valuebean。
如下分成5个步骤
1,建立xml文件
2,建立bean的接口
3,建立bean
4,写测试程序
5,测试
准备工作
环境配置如下,需要spring.jar和common-logging.jar两个jar文件
开始
1,建立xml文件
文件名:beans.xml
文件位置:src目录下
文件内容:
<?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">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean id="sayhello" class="test.service.impl.HelloBean"/>
</beans>
2,建立bean的接口
文件名:Hello.java
文件内容:
package test.service;
public interface Hello {
public void sayHello();
}
3,建立bean
文件名:HelloBean.java
文件内容:
package test.service.impl;
import test.service.Hello;
public class HelloBean implements Hello {
/* (non-Javadoc)
* @see test.service.impl.Hello#sayHello()
*/
public void sayHello() {
System.out.println("这是一个测试程序");
}
}
4,写测试程序
文件名:FirstSpring.java
文件内容:
package test.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.service.Hello;
public class FirstSpring {
public static void main(String[] args) {
testHello();
}
public static void testHello() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Hello hello =(Hello) applicationContext.getBean("sayhello");
hello.sayHello();
}
}
5,测试
运行FirstSpring.java文件,得到输出结果如下:
2009-6-30 3:33:58 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2009-6-30 3:33:59 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@7259da]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7820
2009-6-30 3:33:59 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7820: defining beans [sayhello]; root of factory hierarchy
这是一个测试程序
上面红字是spring输出的调试信息,蓝字是hellobean实际输入的内容。
简单总结:
1,环境配置中不要忘记了common-logging.jar文件,我最开始忘记了,还是用junit测试的,结果就是不通过。出错的原因也不明白。后来直接改成普通的main方法测试,才明白原因。
2,bean的接口和实现的分离在spring中被贯彻执行。同时理解一下IOC(控制反转)的概念。
3,spring中的bean,应该指的是执行各种业务的业务bean才是。不同于strut的formbean和对应db表对象的valuebean。
相关文章推荐
- 第一个Spring程序
- 一起学spring--我的第一个Spring程序,简单粗暴易懂
- springframework(二)第一个spring程序
- Spring_01_第一个Spring程序
- 第一个structs+spring+hibernate的web程序
- Spring的第一个小程序
- (一)SpringBoot之简介和安装插件以及HelloWorld第一个程序
- Spring -- 第一个简单的Spring程序
- SpringBoot入门——我的第一个SpringBoot小程序
- 跟踪学习Spring2.5笔记 -1- 第一个Spring程序
- 搭建Spring开发环境并编写第一个Spring小程序
- 第一章 第一个spring boot程序
- Spring学习之第一个Spring MVC程序(IDEA开发环境)
- Activemq+spring的第一个程序(入门程序--内嵌Broker--消息队列)
- Spring4笔记2--Spring的第一个程序
- 第一个Spring程序
- Spring第一个程序使用容器demo(Idea)
- SpringBoot笔记1---基于SpringBoot的第一个程序
- 第一个 spring-boot 程序
- 第一个 Spring Boot 程序