Spring开发步骤(入门篇)

Spring开发步骤

1<o:p></o:p>

编写依赖注入类:包含SETTERGETTER,如以下代码:<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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值