初识spring,通过简单实例快速认识spring

版权声明:作者junsan,本文为本人原创文档,保留本文的一切权力。欢迎转载,请保留此信息。 http://www.netspy.com.cnhttp://www.inspiresky.com

                相关软件下载: http://www.netspy.com.cn/thread-27-1-1.html
                开发环境配置: http://www.netspy.com.cn/thread-35-1-1.html

        下面我们用Spring来做一个经典的例子,那就是Hello World!。但是这次使用了Spring的一些基本特性。
1新建Spring工程

        File->New->Project…,打开新建工程对话框,找到Spring,选中Spring Project,点击Next>按钮。Project name为HelloSpring,其他保持默认。点击Finish按钮,即可。

2创建HelloBean

        右键点击工程树中HelloSpring下的src节点,右键菜单New ->Package,在对话框中Name填入com.netspy.hello,点击Finish按钮即可。

        然后右键点击包com.netspy.hello,右键菜单New->Class,Name为HelloBean,其他默认。建立了类后,会提示java.lang.Object不存在的错误,是因为没有设置工程的classpath。

        菜单Project->properties,打开工程的属性对话框,如果properties不可用,请先选中HelloSpring工程。

        在对话框中选中Java Build Path,在右边的Tab中选择Libraries,点击Add Variable按钮,在对话框中选中JRE_LIB变量,一路OK即可。

        现在编辑HelloBean,最终代码如下:
       
        package com.netspy.hello;

/*
* author:junsan
* http://www.netspy.com.cn
* 欢迎转载请保留此信息
*/

public class HelloBean {
        private String helloWord = "Hi,you can change me!";

        public void setHelloWord(String helloWord) {
                this.helloWord = helloWord;
        }

        public String getHelloWord() {
                return helloWord;
        }
}

好了,到这里,我们的基本Bean就已经写好了。下面创建一个测试的类。
3创建调用Bean

我们先来看看传统的调用方法,怎么调用HelloBean。

按照创建HelloBean的方法,创建类OldCallTest类。编辑代码如下:

package com.netspy.hello;

/*
* author:junsan
* http://www.netspy.com.cn
* 欢迎转载请保留此信息
*/

public class OldCallTest {
       
        public static void main(String[] args){
                HelloBean hello = new HelloBean();
                hello.setHelloWord("Hello world!");
                System.out.println(hello.getHelloWord());
        }
       
}

我们右键点击该test类,右键菜单Run as->Java Applicationg,可以在控制台中看到输出了

Hello world!

说明我们用传统的方法调用HelloBean成功。

下面我们再来看Spring的调用方式。

我们首先在src下创建一个xml文档,方法和创建类类似,只是选择类型为xml就行了,命名为RefBean.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.netspy.hello.HelloBean">
                <property name="helloWord">
                        <value>Hello World!</value>
                </property>
        </bean>
</beans>


打开工程的properties对话框,找到在左边树中找到Spring节点,选择Config Files,点击Add按钮,将RefBean.xml加入。确定后发现HelloBean.java和RefBean.xml上增加了一个字母S,说明已经被包含进Spring框架。而OldCallBean则不在Spring的框架管理之下。

下面我们看看Spring具体怎么调用HelloBean。

创建SpringCallTest类,方法同上面类的建立方法。编辑代码如下:

package com.netspy.hello;

import java.io.IOException;

/*
* author:junsan
* http://www.netspy.com.cn
* 欢迎转载请保留此信息
*/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringCallTest {
        public static void main(String[] args) throws IOException {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("RefBean.xml");

                HelloBean hello = (HelloBean) ctx.getBean("helloBean");
                System.out.println(hello.getHelloWord());
        }
}

此时,会提示错误,我们只需要在工程属性Java Bulid Path的libriaries中,通过Add External JARs,增加spring.jar和commons-logging.jar。

这些类包都在spring-framework-2.0.2-with-dependencies.zip中有,解开后分别在spring-framework-2.0.2/dist和spring-framework-2.0.2/lib/jakarta-commons下。后面我们会详细讲解解压后各个文件架下内容的作用,还有各个包的作用。

好了,现在按照前面的运行方式,来运行SpringCallTest类。结果大概如下:

2007-1-19 19:29:27 org.springframework.core.CollectionFactory
信息: JDK 1.4+ collections available
2007-1-19 19:29:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [RefBean.xml]
2007-1-19 19:29:27 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=9443463]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [helloBean]; root of BeanFactory hierarchy
2007-1-19 19:29:27 org.springframework.context.support.AbstractApplicationContext refresh
信息: 1 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=9443463]
2007-1-19 19:29:27 org.springframework.context.support.AbstractApplicationContext initMessageSource
信息: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1430b5c]
2007-1-19 19:29:27 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
信息: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@b166b5]
2007-1-19 19:29:27 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [helloBean]; root of BeanFactory hierarchy]
Hello World!

前面的信息都是Spring加载配置和初始化的过程,最后的Hello World! 就是我们通过设置器注入的值。后面我们会详细讲解,请继续关注。
 论坛交流地址: http://www.netspy.com.cn/thread-36-1-1.html   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值