工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 3623|回复: 13

Spring之入门一例

[复制链接]
发表于 2006-7-23 11:08 | 显示全部楼层 |阅读模式
Hibernate最近老是没心情看下去,Struts在等待着新版传进中国,无聊之际,开始spring之旅:
我的原创第一此,就这样献给了后院    哈哈     
OK  let's go

由于俺不大会用IDE工具,昨晚用eclipse试了很久  老是出错,所以还是用editplus吧 :

1, PersonInterface.java  接口

------------------------------------------------------------------------------------------------------
package coffee;

public interface PersonInterface
{
        void help();
}
------------------------------------------------------------------------------------------------------


2,Person.java  实现类
------------------------------------------------------------------------------------------------------
package coffee;

public class Person implements PersonInterface
{
        public void help()
        {
                System.out.println("我是帮助别人还是需要被人帮?");
        }

}
------------------------------------------------------------------------------------------------------

3,JackImpl.java
大家可能会注意到:此类中的 p 并没有显示的使用以往常见的PersonInterface  p = new Person();
也没有使用Person工厂 生产一个Person实例。
这正是Spring中最常见的 依赖注入 , 具体见下面的bean.xml文件中的配置
------------------------------------------------------------------------------------------------------
package coffee;

public class JackImpl
{
        PersonInterface p;
        public void setPerson(PersonInterface p)
        {
                this.p = p;
        }

        public void help()
        {
                p.help();
                System.out.println("Jack help others ...");
        }

}

------------------------------------------------------------------------------------------------------

[ 本帖最后由 深圳情缘 于 2006-7-23 21:05 编辑 ]
 楼主| 发表于 2006-7-23 11:15 | 显示全部楼层

继续

4, bean.xml

Spring中的bean,并不完全等同于以前OO中的bean,在这里,所有的对象都是bean,
下面代码中,
<bean id="person" class="coffee.Person"/>对应于Person类 ,id值person,是Person类实例的名字,大家可以看成是Person类的一个引用;

同理,
<bean id="jack" class="coffee.JackImpl">
       <property name="person">
        <ref local="person"/>
       </property>
</bean>
对应于JackImpl类,

<property name="person">
        <ref local="person"/>
</property>
对应于JackImpl.java中的属性PersonInterface p   , p引用了类Person的名字为person的实例

------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="gb2312"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

        <bean id="person" class="coffee.Person"/>

        <bean id="jack" class="coffee.JackImpl">
                <property name="person"> <!--根据JackImpl.java中的setPerson方法,要用person-->
                        <ref local="person"/> <!--根据上面第8行的bean id值,要用person-->
                </property>
        </bean>

</beans>
------------------------------------------------------------------------------------------------------



5,测试类  Test.java
此类中的代码很简单,大家可以理解为Spring封装了JNDI的lookup方法
------------------------------------------------------------------------------------------------------
package coffee;

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

public class Test
{
    public static void main(String[] args)
    {
        ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
                JackImpl jack = (JackImpl)ctx.getBean("jack");
                jack.help();
    }
}
------------------------------------------------------------------------------------------------------


所需jar包:
spring.jar
commons-logging.jar

[ 本帖最后由 深圳情缘 于 2006-7-23 21:07 编辑 ]
回复

使用道具 举报

发表于 2006-7-23 15:22 | 显示全部楼层
嗯.谢谢楼主的分享.这是spring IoC入门的例子,简单,实用.我帮楼主整理下发个Myeclipse版的例子.
回复

使用道具 举报

 楼主| 发表于 2006-7-23 15:28 | 显示全部楼层

好啊 THX了

好啊  THX了
回复

使用道具 举报

发表于 2006-7-23 15:35 | 显示全部楼层
thanks for share.
回复

使用道具 举报

发表于 2006-7-23 16:13 | 显示全部楼层
帮楼主的SPRING IoC实例弄个Eclipse+Myeclipse版.OK...开始...

1. 打开Eclipse,新建一个java项目(或web项目Ejb项目都可以,这里没多大所谓,因为spring可以用于任何地方),我们命名为helloSpring.如果项目没有源文件夹的话就新建一个命名为src的源文件夹.(方面以后放java程序)

2.右键单击helloSpring项目 -> Myeclipse -> Add Spring Capabilities.在界面里什么都不需要更改,直接点next.在第二个视图里面,为了配合楼主的例子,我们把applicationContex.xml的名字改成bean.xml.其他选项不需要更改.OK,点击finish.下面贴个图,是我新建spring项目后的包结构图.
(图片1)

3.建立源文件.(这里我使用了楼主的源码,包括包命名也和楼主一致.)建立后的包结构图如下:
(图片2)


4.好了.运行下楼主的例子...这里顺便贴下Myeclipse生成的Bean依赖图...
(图片3)

成功!有空请楼主总结下也大致说下原理吧.可能有同学还对spring不太了解,没弄懂为什么这么弄.
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

 楼主| 发表于 2006-7-23 18:01 | 显示全部楼层

写在前面的贴子中了

成功!有空请楼主总结下也大致说下原理吧.可能有同学还对spring不太了解,没弄懂为什么这么弄.


我把大致的解释写在前面源码的贴子中了
由于我也是开始接触,可能理解未必正确,
如果说的不够清楚
或者说错的
还请大家版主帮忙修改
在此谢过
呵呵
回复

使用道具 举报

 楼主| 发表于 2006-7-23 21:10 | 显示全部楼层

纠正:

package coffee;

public class JackImpl
{
        PersonInterface p;
        public void setPerson(PersonInterface p)
        {
                this.p = p;
        }

        public void help()
        {
                p.help();
                System.out.println("Jack help others ...");
        }

}




***********************************************
在此类中,把原来的Person  p修改成  :   PersonInterface p
同时修改set方法

因为Spring是面向接口编程的,  以前都是直接写类,所以转不过弯来
呵呵
回复

使用道具 举报

发表于 2006-7-23 22:32 | 显示全部楼层
原帖由 深圳情缘 于 2006/7/23 21:10 发表
package coffee;

public class JackImpl
{
        PersonInterface p;
        public void setPerson(PersonInterface p)
        {
                this.p = p;
        }

        public void  ...


呵呵.楼主可以编辑下你原来错误的代码啊,这样会更好一点.

顺便提下,建议楼主命名接口的时候用前缀I,而不是用Interface后缀.比如你这里可以使用IPerson.而对于实现就可以使用后缀Impl.目前java世界较为通用的约定是这样.
回复

使用道具 举报

 楼主| 发表于 2006-7-25 19:44 | 显示全部楼层

good ! think you

原帖由 wool王 于 2006-7-23 22:32 发表


顺便提下,建议楼主命名接口的时候用前缀I,而不是用Interface后缀.比如你这里可以使用IPerson.而对于实现就可以使用后缀Impl.目前java世界较为通用 ...


:victory:! think you
回复

使用道具 举报

发表于 2006-7-26 09:02 | 显示全部楼层
昨天代码评审,我被批斗乱用接口...都是spring惹的祸啊...我现在"一切都是接口"的观念已经根深蒂固了...得慢慢改...
回复

使用道具 举报

发表于 2006-7-26 10:42 | 显示全部楼层
楼上不容易呀~~~~~~~~~~

多发点关于公司的事的帖子,让我们见识见识 外面的世界。。。。。。
回复

使用道具 举报

 楼主| 发表于 2006-7-26 10:53 | 显示全部楼层

严重支持LS

严重支持LS
回复

使用道具 举报

发表于 2006-8-9 23:04 | 显示全部楼层
我今天也试了下 Spring .

IHelloWorldService.java
  1. package powerwind;
  2. public interface IHelloWorldService
  3. {
  4.         public String getHello();
  5.         public void setHello(String hello);
  6. }
复制代码


HelloWorldService.java
  1. package powerwind;
  2. public class HelloWorldService implements IHelloWorldService
  3. {
  4.         private String hello;
  5.         public String getHello()
  6.         {
  7.                 return hello;
  8.         }
  9.         public void setHello(String hello)
  10.         {
  11.                 this.hello=hello;
  12.         }
  13. }
复制代码


HelloWorld.java
  1. package powerwind;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.core.io.*;

  5. public class HelloWorld
  6. {
  7.         private IHelloWorldService iHelloWorld;
  8.         public HelloWorld()
  9.         {
  10.                 setService();
  11.         }
  12.         public void setService()
  13.         {
  14.                 Resource resource=new ClassPathResource("appcontext.xml");
  15.                 BeanFactory factory=new XmlBeanFactory(resource);
  16.                 iHelloWorld=(IHelloWorldService)factory.getBean("HelloWorld");
  17.         }
  18.         public IHelloWorldService getService()
  19.         {
  20.                 return iHelloWorld;
  21.         }
  22.         public static void main(String[]args)
  23.         {
  24.                 HelloWorld helloworld=new HelloWorld();
  25.                 System.out.println(helloworld.getService().getHello());
  26.         }
  27. }
复制代码


appcontext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4.         <bean id="HelloWorld" class="powerwind.HelloWorldService">
  5.                 <property name="hello">
  6.                         <value>Hello,World!This is from spring!</value>
  7.                 </property>
  8.         </bean>
  9. </beans>
复制代码


build.xml

  1. <?xml version="1.0"?>
  2. <project name="Hello World" default="prepare" basedir=".">
  3.   <property name="src.dir" value="${basedir}/src"/>
  4.   <property name="spring.lib" value="G:\\spring-framework-1.2.8\\dist"/>
  5.   <property name="classes.dir" value="${basedir}/classes"/>

  6.   <path id="classpath">
  7.         <fileset dir="${spring.lib}">
  8.             <include name="*.jar"/>
  9.         </fileset>
  10.         <pathelement location="${classes.dir}"/>
  11.   </path>

  12.   <target name="prepare" >
  13.         <delete dir="${classes.dir}"/>
  14.     <mkdir dir="${classes.dir}"/>
  15.   </target>

  16.   <target name="compile" depends="prepare">
  17.     <javac srcdir="${src.dir}" destdir="${classes.dir}" includes="*.java">
  18.             <classpath refid="classpath"/>
  19.     </javac>
  20.         <copy toDir="${classes.dir}/powerwind" file="${src.dir}/appcontext.xml">
  21.         </copy>
  22.   </target>

  23.   <target name="run" depends="compile">
  24.     <java classname="powerwind.HelloWorld" fork="yes">
  25.       <classpath refid="classpath"/>
  26.     </java>
  27.   </target>

  28. </project>
复制代码


输入 ant run 就行了.

---------------------------------------------2006-8-14补充----------------------------------------------------------------------
如果不用XML文件进行配置,还可能 properties 文件

  1. HelloWorld.class=powerwind.HelloWorldService
  2. HelloWorld.hello=Hello,World!This is from spring!
复制代码


读取Bean的代码改一点:

  1. BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
  2. PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader( reg);
  3. reader.loadBeanDefinitions(new ClassPathResource("appcontext.properties"));
  4. iHelloWorld = (IHelloWorldService)((BeanFactory)reg).getBean("HelloWorld");
复制代码

[ 本帖最后由 powerwind 于 2006-8-15 23:51 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-4-30 20:20

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表