Spring开发步骤
步1:<o:p></o:p>
编写依赖注入类:包含SETTER,GETTER,如以下代码:<o:p></o:p>
/**<o:p></o:p>
* <o:p></o:p>
*/<o:p></o:p>
package com.model;<o:p></o:p>
<o:p> </o:p>
/**<o:p></o:p>
* @author gyc<o:p></o:p>
*<o:p></o:p>
*/<o:p></o:p>
public class ModelBean {<o:p></o:p>
private String msg;<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
public String getMsg() {<o:p></o:p>
return msg;<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
public void setMsg(String msg) {<o:p></o:p>
this.msg = msg;<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
public void sayHello()<o:p></o:p>
{<o:p></o:p>
System.out.println(msg);<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
<o:p> </o:p>
步2:<o:p></o:p>
在配置文件bean.xml配置:<o:p></o:p>
xml version="1.0" encoding="gb2312"?><o:p></o:p>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><o:p></o:p>
<beans><o:p></o:p>
其中id="hello"为注入类的唯一标识ID;class="com.model.ModelBean"为注入类所在位置;<property name="msg">为指定注入类的属性,必要与SETTER(如setMsg)命名保持一致,以让Spring可以显式调用; <value>Hello,Spring!value>元素中,Hello,Spring!为注入类需要注入的属性值--><o:p></o:p>
<bean id="hello" class="com.model.ModelBean"><o:p></o:p>
<property name="msg"><o:p></o:p>
<value>Hello,Spring!value><o:p></o:p>
property><o:p></o:p>
bean><o:p></o:p>
beans><o:p></o:p>
<o:p></o:p>
<o:p> </o:p>
步3:<o:p></o:p>
编写Spring配置类:<o:p></o:p>
/**<o:p></o:p>
* <o:p></o:p>
*/<o:p></o:p>
package com.spring;<o:p></o:p>
<o:p> </o:p>
import java.io.FileInputStream;<o:p></o:p>
import java.io.InputStream;<o:p></o:p>
<o:p> </o:p>
import org.springframework.beans.factory.BeanFactory;<o:p></o:p>
import org.springframework.beans.factory.xml.XmlBeanFactory;<o:p></o:p>
import org.springframework.core.io.InputStreamResource;<o:p></o:p>
<o:p> </o:p>
/**<o:p></o:p>
* @author gyc<o:p></o:p>
*用singleton实现,以一次初始化配置文件,提高效率<o:p></o:p>
*/<o:p></o:p>
public class ConfigImp_Spring implements ConfigIf {<o:p></o:p>
<o:p> </o:p>
/**<o:p></o:p>
* <o:p></o:p>
*/<o:p></o:p>
private static ConfigImp_Spring instance=null;<o:p></o:p>
private static String key="key";//对象锁,考虑到多线程情形<o:p></o:p>
private BeanFactory beanFactory=null;<o:p></o:p>
<o:p></o:p>
private ConfigImp_Spring() {<o:p></o:p>
// TODO Auto-generated constructor stub<o:p></o:p>
}<o:p></o:p>
//双检模式<o:p></o:p>
public static ConfigImp_Spring getInstance(String configxml)<o:p></o:p>
{<o:p></o:p>
if(instance==null)<o:p></o:p>
{<o:p></o:p>
synchronized(key)<o:p></o:p>
{<o:p></o:p>
if(instance==null)<o:p></o:p>
{<o:p></o:p>
instance=new ConfigImp_Spring();<o:p></o:p>
instance.init(configxml);<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
return instance;<o:p></o:p>
<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
public void init(String configxml)<o:p></o:p>
{<o:p></o:p>
try<o:p></o:p>
{<o:p></o:p>
InputStream is=new FileInputStream(configxml);<o:p></o:p>
InputStreamResource ir=new InputStreamResource(is);<o:p></o:p>
beanFactory=new XmlBeanFactory(ir);<o:p></o:p>
System.out.println("Now in ConfigImp_Spring,init() ,new a beanFactory!");<o:p></o:p>
}<o:p></o:p>
catch(Exception ex)<o:p></o:p>
{<o:p></o:p>
System.out.println("配置文件初始化错误: "+ex);<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
public Object getBean(String id) {<o:p></o:p>
// TODO Auto-generated method stub<o:p></o:p>
return beanFactory.getBean(id);<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
<o:p> </o:p>
步4:(并非必要)写一个代理类,对外界提供唯一一个Spring代理入口<o:p></o:p>
/**<o:p></o:p>
* <o:p></o:p>
*/<o:p></o:p>
package com.spring;<o:p></o:p>
<o:p> </o:p>
/**<o:p></o:p>
* @author gyc<o:p></o:p>
*<o:p></o:p>
*/<o:p></o:p>
public class ConfigProxy {<o:p></o:p>
<o:p></o:p>
public static Object getBean(String id)<o:p></o:p>
{<o:p></o:p>
ConfigIf ci=ConfigImp_Spring.getInstance("bean.xml");<o:p></o:p>
<o:p></o:p>
return ci.getBean(id);<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
<o:p> </o:p>
步5:<o:p></o:p>
写一个测试类,用于测试<o:p></o:p>
/**<o:p></o:p>
* <o:p></o:p>
*/<o:p></o:p>
package com.model;<o:p></o:p>
import com.spring.*;<o:p></o:p>
<o:p> </o:p>
/**<o:p></o:p>
* @author gyc<o:p></o:p>
*<o:p></o:p>
*/<o:p></o:p>
public class TestB {<o:p></o:p>
<o:p> </o:p>
/**<o:p></o:p>
* @param args<o:p></o:p>
*/<o:p></o:p>
public static void main(String[] args) {<o:p></o:p>
// TODO Auto-generated method stub<o:p></o:p>
ModelBean mb=(ModelBean)ConfigProxy.getBean("hello");<o:p></o:p>
mb.sayHello();<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
(完,祝您愉快:D!)<o:p></o:p>