Spring学习笔记---Spring入门的第一个程序
来源:互联网 发布:mac一键切换win10 编辑:程序博客网 时间:2024/05/17 02:49
出现的问题:
0) XmlBeanFactory(resource)来加载配置文件,过时了,用什么方法代替呢?
不会,百度一下,找到了以下方法,行得通
ApplicationContext context=new ClassPathXmlApplicationContext("/com/helloworld/ioc-config.xml");
BeanFactory factory=context;
1) 缺少commons-logging.jar包
在网上找了个加入工程中就OK,解决了
2) 为spring配置构建路径时,中途出现了
java.lang.NullPointerExceptionorg.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>的问题
原来是因为勾上了系统库选项,调试程序,出现了的上述错误,后来将选项去掉,重新配置,问题解决。
好了,问题解决是,我的第一个spring程序运行成功
HelloWorld -------第一个spring程序
接口类:HelloWorld.java
package com.helloworld;
public interface HelloWorld {
public void sayHello();
}
JavaBean实体类:HelloWorldBean.java
package com.helloworld;
public class HelloWorldBean implements HelloWorld{
String helloWorld;
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println(helloWorld);
}
}
配置文件:ioc-config.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="helloBean" class="com.helloworld.HelloWorldBean">
<property name="helloWorld">
<value>Hello!My First Spring Program!</value>
</property>
</bean>
</beans>
测试类:HelloWorldTest.java
首先,从classpath路径上装载XML的配置信息;然后由spring框架来创建对象
package com.helloworld;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("/com/helloworld/ioc-config.xml");
BeanFactory factory=context;
HelloWorldBean hello = (HelloWorldBean) factory.getBean("helloBean");
hello.sayHello();
}
}
- Spring学习笔记---Spring入门的第一个程序
- Spring学习笔记(一)第一个Spring程序
- Spring入门04 - 第一个Spring程序
- spring第一个程序笔记
- 跟踪学习Spring2.5笔记 -1- 第一个Spring程序
- Spring的第一个程序
- Spring的第一个程序
- Spring -- 第一个简单的Spring程序
- Spring入门17 - 第一个Spring MVC Web程序
- 第一个Spring程序
- Spring第一个程序
- 第一个spring程序
- 第一个spring程序
- 第一个Spring程序
- Spring第一个程序
- Spring 第一个程序
- 第一个Spring程序
- 第一个Spring程序
- 免安装的Tomcat基本配置和安装
- FreeBSD 8.3 发布
- 第十一期 免费讲座 新浪SAE架构与设计
- Fedora 17 Beta 版发布
- MySQL 企业监控器 2.3.10 正式版发布
- Spring学习笔记---Spring入门的第一个程序
- Apache Cassandra 1.1.0 稳定版发布
- [str函数]只能把看到的记下来啦!
- 11 步教你选择最稳定的 MySQL 版本
- 软件开发中需要专职的 QA 吗?
- 程序员需要谨记的九大安全编码规则
- 你的代码(软件)安全吗?【信息图】
- 简单 3 步实现高效编程
- objective-c基础教程——学习小结