Spring之入门一例
Hibernate最近老是没心情看下去,Struts在等待着新版传进中国,无聊之际,开始spring之旅:我的原创第一此,就这样献给了后院 哈哈
OKlet'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 并没有显示的使用以往常见的PersonInterfacep = 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 编辑 ]
继续
4, bean.xmlSpring中的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 编辑 ] 嗯.谢谢楼主的分享.这是spring IoC入门的例子,简单,实用.我帮楼主整理下发个Myeclipse版的例子.
好啊 THX了
好啊THX了 thanks for share. 帮楼主的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不太了解,没弄懂为什么这么弄.
写在前面的贴子中了
成功!有空请楼主总结下也大致说下原理吧.可能有同学还对spring不太了解,没弄懂为什么这么弄.
我把大致的解释写在前面源码的贴子中了
由于我也是开始接触,可能理解未必正确,
如果说的不够清楚
或者说错的
还请大家版主帮忙修改
在此谢过
呵呵
纠正:
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 ...");
}
}
***********************************************
在此类中,把原来的Personp修改成: PersonInterface p
同时修改set方法
因为Spring是面向接口编程的,以前都是直接写类,所以转不过弯来
呵呵 原帖由 深圳情缘 于 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世界较为通用的约定是这样.
good ! think you
原帖由 wool王 于 2006-7-23 22:32 发表顺便提下,建议楼主命名接口的时候用前缀I,而不是用Interface后缀.比如你这里可以使用IPerson.而对于实现就可以使用后缀Impl.目前java世界较为通用 ...
:victory:! think you 昨天代码评审,我被批斗乱用接口...都是spring惹的祸啊...我现在"一切都是接口"的观念已经根深蒂固了...得慢慢改... 楼上不容易呀~~~~~~~~~~
多发点关于公司的事的帖子,让我们见识见识 外面的世界。。。。。。
严重支持LS
严重支持LS 我今天也试了下 Spring .IHelloWorldService.java
package powerwind;
public interface IHelloWorldService
{
public String getHello();
public void setHello(String hello);
}
HelloWorldService.java
package powerwind;
public class HelloWorldService implements IHelloWorldService
{
private String hello;
public String getHello()
{
return hello;
}
public void setHello(String hello)
{
this.hello=hello;
}
}
HelloWorld.java
package powerwind;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class HelloWorld
{
private IHelloWorldService iHelloWorld;
public HelloWorld()
{
setService();
}
public void setService()
{
Resource resource=new ClassPathResource("appcontext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
iHelloWorld=(IHelloWorldService)factory.getBean("HelloWorld");
}
public IHelloWorldService getService()
{
return iHelloWorld;
}
public static void main(String[]args)
{
HelloWorld helloworld=new HelloWorld();
System.out.println(helloworld.getService().getHello());
}
}
appcontext.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="HelloWorld" class="powerwind.HelloWorldService">
<property name="hello">
<value>Hello,World!This is from spring!</value>
</property>
</bean>
</beans>
build.xml
<?xml version="1.0"?>
<project name="Hello World" default="prepare" basedir=".">
<property name="src.dir" value="${basedir}/src"/>
<property name="spring.lib" value="G:\\spring-framework-1.2.8\\dist"/>
<property name="classes.dir" value="${basedir}/classes"/>
<path id="classpath">
<fileset dir="${spring.lib}">
<include name="*.jar"/>
</fileset>
<pathelement location="${classes.dir}"/>
</path>
<target name="prepare" >
<delete dir="${classes.dir}"/>
<mkdir dir="${classes.dir}"/>
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${classes.dir}" includes="*.java">
<classpath refid="classpath"/>
</javac>
<copy toDir="${classes.dir}/powerwind" file="${src.dir}/appcontext.xml">
</copy>
</target>
<target name="run" depends="compile">
<java classname="powerwind.HelloWorld" fork="yes">
<classpath refid="classpath"/>
</java>
</target>
</project>
输入 ant run 就行了.
---------------------------------------------2006-8-14补充----------------------------------------------------------------------
如果不用XML文件进行配置,还可能 properties 文件
HelloWorld.class=powerwind.HelloWorldService
HelloWorld.hello=Hello,World!This is from spring!
读取Bean的代码改一点:
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader( reg);
reader.loadBeanDefinitions(new ClassPathResource("appcontext.properties"));
iHelloWorld = (IHelloWorldService)((BeanFactory)reg).getBean("HelloWorld");
[ 本帖最后由 powerwind 于 2006-8-15 23:51 编辑 ]
页:
[1]