【原】spring 2.5--注解

          spring 2.5 權威學習資料:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/

          剛剛做了個項目需要spring2.5的注解注入機制,順便把spring2.5注解的配置文件記錄下來。

          一 準備工作
              導入Jar包:


          二 配置文件:
          1 web.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee 
              http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

              
          <!--  Spring ApplicationContext -->
              
          <listener>
                  
          <listener-class>
                      org.springframework.web.context.ContextLoaderListener
                  
          </listener-class>
              
          </listener>
              
          <!--session scope no Spring bean  -->
              
          <listener>
                  
          <listener-class>
                      org.springframework.web.context.request.RequestContextListener
                  
          </listener-class>
              
          </listener>
              
              
          <context-param>
                  
          <param-name>contextConfigLocation</param-name>
                  
          <param-value>
                      classpath:
          /applicationContext.xml
                  
          </param-value>
              
          </context-param>
              
              
              
          <!-- struts2配置文件 -->
              
          <filter>
                  
          <filter-name>struts2</filter-name>
                  
          <filter-class>
                      org.apache.struts2.dispatcher.FilterDispatcher
                  
          </filter-class>
              
          </filter>
              
          <filter-mapping>
                  
          <filter-name>struts2</filter-name>
                  
          <url-pattern>/*</url-pattern>
              </filter-mapping>
              
              <welcome-file-list>
                  <welcome-file>main.jsp</welcome-file>
              </welcome-file-list>
          </web-app>

          2 struts.xml
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
              
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              
          "http://struts.apache.org/dtds/struts-2.0.dtd">

          <struts>


              
          <package name="data_import_manager" extends="struts-default" >
              
                  
          <action name="importData">
                      
          <result>/import_taxdata.jsp</result>
                  
          </action>

                  
          <action name="taxEnterpriseImport" class="taxImportAction"
                      method
          ="taxEnterprise" >
                      
          <result name="success">/import_taxdata.jsp</result>
                  
          </action>
                  
          <action name="taxInfoImport" class="taxImportAction"
                      method
          ="taxInfo" >
                      
          <result name="error">/import_taxdata.jsp</result>
                      
          <result name="success">/success.jsp</result>
                  
          </action>
                  
              
          </package>


          </struts>

          3 application.xml
          <?xml version="1.0" encoding="UTF-8"?>  

          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context
          ="http://www.springframework.org/schema/context" 
              xmlns:aop
          ="http://www.springframework.org/schema/aop"
              xmlns:tx
          ="http://www.springframework.org/schema/tx"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

              
          <context:component-scan base-package="com.btwob.squall.bmis" />


              
          <!-- 數據源 -->
              
          <bean id="default_dataSource"
                  
          class="org.apache.commons.dbcp.BasicDataSource"
                  destroy
          -method="close">
                  
          <property name="driverClassName"
                      value
          ="net.sourceforge.jtds.jdbc.Driver" />
                  
          <property name="url"
                      value
          ="jdbc:jtds:sqlserver://192.168.1.111:1433/BuildingMIS" />
                  
          <property name="username" value="sa" />
                  
          <property name="password" value="root" />
                  
          <property name="maxActive" value="100" />
                  
          <property name="maxIdle" value="30"></property>
                  
          <property name="maxWait" value="500" />
              
          </bean>


              
          <bean id="sessionFactory"
                  
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                  
          <!-- 載入數據源 -->
                  
          <property name="dataSource">
                      
          <ref bean="default_dataSource" />
                  
          </property>
                  
          <property name="hibernateProperties">
                      
          <props>
                          
          <prop key="hibernate.dialect">
                              org.hibernate.dialect.SQLServerDialect
                          
          </prop>
                          
          <prop key="show_sql">true</prop>
                      
          </props>
                  
          </property>
                  
          <property name="mappingResources">
                      
          <value>com/btwob/squall/bmis/model/Tax.hbm.xml</value>
                  
          </property>
              
          </bean>



              
          <!-- hibernateTemplate -->
              
          <bean id="hibernateTemplate"
                  
          class="org.springframework.orm.hibernate3.HibernateTemplate">
                  
          <property name="sessionFactory">
                      
          <ref bean="sessionFactory" />
                  
          </property>
              
          </bean>
              
              
              
          </beans>

          三 注解注入文件:
          1 dao實現類:
          @Scope("singleton")
          @Repository(
          "taxDao")
          public class TaxDao implements ITaxDao {

              @Autowired
              
          private HibernateTemplate hibernateTemplate;

          public void saveTax(Tax tax) throws Exception {
                  hibernateTemplate.save(tax);
                  hibernateTemplate.flush();
              }

          }
          2 serivce實現類:
          @Service("taxService")
          public class TaxService implements ITaxService {

              @Autowired
              @Qualifier(
          "taxDao")
              
          private ITaxDao taxDao;
                  
          public void saveTaxService(Tax tax) {
                  
          try {
                      taxDao.saveTax(tax);
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }
          3 action類:

          @SuppressWarnings("serial")
          @Scope(
          "prototype")
          @Controller(
          "taxImportAction")
          public class TaxImportAction extends BaseAction {

              @Autowired
              @Qualifier(
          "taxService")
              
          private ITaxService taxService;

              
          public TaxImportAction() {
                  
          super();
              }

              @Override
              
          public String execute() {
                  
          return SUCCESS;
              }
              
          public String taxEnterprise() throws Exception {return success;
          }
              @SuppressWarnings(
          "unchecked")
              
          public String taxInfo() throws Exception {return success;
          }
          }



          posted on 2010-01-24 22:16 龍櫻 閱讀(3022) 評論(2)  編輯  收藏 所屬分類: 框架層

          評論

          # re: spring 2.5--注解 2010-01-25 09:42 咖啡妝

          你的項目的jar包也太瘋狂了吧 一大堆,重復的也很多,spring只需要 spring.jar 在spring3.X以后各個模塊裁分開編譯jar包。
          建議用spring2.X 的最后版本spring2.6 。有些功能spring2.5沒有而且有必要。spring3.x的想后兼容性會有問題。  回復  更多評論   

          # re: spring 2.5--注解 2010-02-05 17:32 傀儡守望者

          @咖啡妝
          JAR包,都是項目中需要的,已經把重復的刪除了。
            回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(13)

          隨筆檔案(13)

          文章分類(1)

          文章檔案(1)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 淮南市| 常德市| 枝江市| 富蕴县| 湛江市| 仙桃市| 肇州县| 岳池县| 额尔古纳市| 马龙县| 宁强县| 康马县| 胶州市| 永春县| 开化县| 通化市| 铁岭市| 通辽市| 敖汉旗| 平山县| 石林| 科技| 文山县| 左贡县| 淅川县| 柳河县| 康乐县| 吉首市| 南昌市| 黄平县| 牙克石市| 浮梁县| 沙雅县| 泰安市| 桐乡市| 年辖:市辖区| 海口市| 盐山县| 兖州市| 三门峡市| 巴林右旗|