简单spring实例
第一步:下载spring.jar,commons-logging.jar包。并导入工程
导入步骤:右击工程--》Build Path--》Add External Archiyes...
第二步:工程中新建三个java文件
文件一:
package com.springination.chapter01.hello;
public interface IGreetingService {
public void sayGreeting();
}
文件二:
package com.springination.chapter01.hello;
public class GreetingServiceImpl implements IGreetingService {
private String greeting;
public GreetingServiceImpl(){}
public GreetingServiceImpl(String greeting){
this.greeting = greeting;
}
public void sayGreeting(){
System.out.println(greeting);
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
文件三:
package com.springination.chapter01.hello;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Helloworld {
/**
* @param args
*/
public static void main(String[] args)
{
//方法一 FileSystemResource类,默认xml文件的路径在工程目录下
Resource resource = new FileSystemResource("hello.xml");
BeanFactory factory = new XmlBeanFactory(resource);
GreetingServiceImpl greetingService = (GreetingServiceImpl) factory.getBean("greetingService");
greetingService.sayGreeting();
//方法二 ClassPathXmlApplicationContext类,默认的寻找xml文件的路径就在src目录下
// ApplicationContext ac = new ClassPathXmlApplicationContext("hello.xml");
// GreetingServiceImpl greetingService = (GreetingServiceImpl) ac.getBean("greetingService");
// greetingService.sayGreeting();
//方法三 FileSystemXmlApplicationContext类,默认的寻找xml文件的路径就在src目录下
// ApplicationContext ac = new FileSystemXmlApplicationContext("hello.xml");
// GreetingServiceImpl greetingService = (GreetingServiceImpl) ac.getBean("greetingService");
// greetingService.sayGreeting();
}
}
第三步:新建hello.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="greetingService"
class="com.springination.chapter01.hello.GreetingServiceImpl">
<property name="greeting">
<value>hello world</value>
</property>
</bean>
</beans>
hello.xml文件的具体存放位置,要看在第二步中helloworld.java文件用到的获取xml文件里的类。上面列举了三种。都有存放位置的注释。
遇到的问题一:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
原因,没导入commons-logging.jar包
问题二:
Caused by: java.io.FileNotFoundException: hello.xml (系统找不到指定的文件。)
hello.xml文件的存放位置不正确:具体见第二步中文件三里的注释。
分享到:
相关推荐
Spring、SpringMVC和Mybatis是Java开发中最常用的三大开源框架,它们的整合使用,通常被称为SSM框架。这个框架组合提供了完整的后端服务解决方案,包括依赖注入(DI)、面向切面编程(AOP)、模型-视图-控制器(MVC...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
java *spring工具类 方便在非spring管理环境中获取beanjava *spring工具类 方便在非spring管理环境中获取beanjava *spring工具类 方便在非spring管理环境中获取beanjava *spring工具类 方便在非spring管理环境中获取...
Spring Integration + Spring WS 整合 在 Java 领域中,Spring Integration 和 Spring WS 是两个常用的框架,它们分别负责集成系统和 Web 服务。今天,我们将探讨如何将这两个框架整合在一起,实现一个完整的 Web ...
包含spring 3.0.5的所有jar文件: org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE...
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...