配置运行Spring 入门级Demo 和常见故障解决 (Spring in Action)
2010-09-20 21:55
459 查看
在spring demo编写过程中可以遇到以下错误
避免常见故障:
(1)java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Spring依赖于Apache的通用日志组件包,下载并加入到build path
(2) No default constructor found
为实现类田间无参数构造函数
(3) java.io.FileNotFoundException
配置文件的路径有问题,放在工程的根目录可避免
1.编写接口类
package com.springinaction.chapter1.hello;
public interface GreetingService{
void sayGreeting();
}
2.编写实现类
package com.springinaction.chapter1.hello;
public class GreetingServiceImpl implements GreetingService {
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;
}
}
3.编写依赖注入配置文件:
<?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.0.xsd">
<bean id="greetingService"
class="com.springinaction.chapter1.hello.GreetingServiceImpl">
<property name="greeting" value="Buenos Dias!" />
</bean>
</beans>
4..主函数
package com.springinaction.chapter1.hello;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class HelloApp {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"config.xml"));
GreetingService service = (GreetingService) factory
.getBean("greetingService");
service.sayGreeting();
}
}
运行结果:
Buenos Dias!
5.为了使程序能够正常运行需要注意一下几点
(1) 设置好build path:加入spring包的dist下的jar包
(2) 由于spring还依赖于apache的common logging包,还要加入commons-logging包的jar包
(3) 配置文件的位置应该放在工程的根目录,否则文件路径不能这么写,会找不到文件发生IOException.
(4) 要为具体的实现类提供一个无参数的构造函数
配置好以上,一个一个简单的spring demo就完成了.
注意编程方式的特点.首先要编写一个接口类.然后真实的业务逻辑在实现接口的实体类中完成.
运行结果:在主函数中的程序设计完全面向接口编程,和具体实现完全没有依赖关系.依赖的注入通过读取config.xml文件获得.并通过spring的BeanFactory解析得到具体的实现.如果实现类更改了,只需要重新编译实现类和重新配置文件config.xml即可.完全面向接口编程的测试主函数类不需要重新编译.
避免常见故障:
(1)java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Spring依赖于Apache的通用日志组件包,下载并加入到build path
(2) No default constructor found
为实现类田间无参数构造函数
(3) java.io.FileNotFoundException
配置文件的路径有问题,放在工程的根目录可避免
1.编写接口类
package com.springinaction.chapter1.hello;
public interface GreetingService{
void sayGreeting();
}
2.编写实现类
package com.springinaction.chapter1.hello;
public class GreetingServiceImpl implements GreetingService {
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;
}
}
3.编写依赖注入配置文件:
<?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.0.xsd">
<bean id="greetingService"
class="com.springinaction.chapter1.hello.GreetingServiceImpl">
<property name="greeting" value="Buenos Dias!" />
</bean>
</beans>
4..主函数
package com.springinaction.chapter1.hello;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class HelloApp {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"config.xml"));
GreetingService service = (GreetingService) factory
.getBean("greetingService");
service.sayGreeting();
}
}
运行结果:
Buenos Dias!
5.为了使程序能够正常运行需要注意一下几点
(1) 设置好build path:加入spring包的dist下的jar包
(2) 由于spring还依赖于apache的common logging包,还要加入commons-logging包的jar包
(3) 配置文件的位置应该放在工程的根目录,否则文件路径不能这么写,会找不到文件发生IOException.
(4) 要为具体的实现类提供一个无参数的构造函数
配置好以上,一个一个简单的spring demo就完成了.
注意编程方式的特点.首先要编写一个接口类.然后真实的业务逻辑在实现接口的实体类中完成.
运行结果:在主函数中的程序设计完全面向接口编程,和具体实现完全没有依赖关系.依赖的注入通过读取config.xml文件获得.并通过spring的BeanFactory解析得到具体的实现.如果实现类更改了,只需要重新编译实现类和重新配置文件config.xml即可.完全面向接口编程的测试主函数类不需要重新编译.
相关文章推荐
- 配置运行Spring 入门级Demo 和常见故障解决 (Spring in Action)
- 配置运行Spring 入门级Demo 和常见故障解决 (Spring in Action)
- Spring Boot基本配置和常见的运行时错误以及解决
- Spring in Action : MVC 配置(JAVA方式)
- Maven+SpringMVC+Dubbo+zookeeper 简单的入门demo配置
- Spring in Action入门
- 【Spring】【笔记】《Spring In Action》第3章 简化XML配置
- Dwr2+Log4j+Struts1.2+Spring2.0+Hibernate3.1_in_MyEclipse5.5配置问题及解决方法
- Spring in Action : MVC 配置(JAVA方式)
- 《Spring Boot in Action》【3. 自定义配置】
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-001- 配置SpringFlow(flow-executor、flow-registry、FlowHandlerMapping、FlowHandlerAdapter)
- day76_maven02:maven拆分各层配置后,【pom.xml、Spring配置都按层拆分(dao/service/action)】,导致报错解决。
- Spring in Action 入门之面向切面编程AOP
- Spring in Action 入门之面向切面编程AOP
- spring入门——spring in action学习p1-p50
- Maven+SpringMVC+Dubbo 简单的入门demo配置
- spring 打入jar包 xml配置路径,加载异常等常见问题解决方法
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
- spring in action-最小化spring XML配置
- 【笔记】Velocity模板 和 Spring的整合配置 解决页面乱码的最佳方案, 简单Demo了解Velocity