Bean没有在Spring中初始化

时间:2014-02-04 06:49:38

标签: spring spring-mvc dependency-injection struts

请在下面找到我的代码

的web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener               
    </listener-class>
</listener>

我的applicationContext.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"
        xsi:schemaLocation="


     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


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

 bean="sessionFactory" />
        </property>
    </bean> 

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <value>
                    /com/qantas/cardselector/model/config/TcerConfigItem.hbm.xml
                </value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>

                <prop key="hibernate.session_factory_name">
                    HibernateSessionFactory
                </prop>
                <prop
                    key="hibernate.transaction.manager_lookup_class">
                    org.hibernate.transaction.WeblogicTransactionManagerLookup
                </prop>
                <prop key="hibernate.transaction.factory_class">
                    org.hibernate.transaction.JTATransactionFactory
                </prop>
                <prop key="hibernate.current_session_context_class">
                    jta
                </prop>
                <prop key="hibernate.connection.datasource">
                    salesDataSource
                </prop>
                <prop key="hibernate.connection.release_mode">
                    auto
                </prop>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.OracleDialect
                </prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.use_outer_join">true</prop>
                <prop key="hibernate.query.factory_class">
                    org.hibernate.hql.classic.ClassicQueryTranslatorFactory
                </prop>
                <prop key="hibernate.cache.provider_class">
                    org.hibernate.cache.EhCacheProvider
                </prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.query.substitutions">
                    toLowerCase=lower,toUpperCase=upper,
                    today=TRUNC(sysdate),now=sysdate,getDate=TRUNC
                </prop>
                <prop key="hibernate.generate_statistics">true</prop>
            </props>

        </property>
    </bean>


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>


    <bean id="InitialLoad" name="/InitialLoadAction"
        class="com.qantas.cardselector.action.cardslist.CardsListAction">
        <property name="cardManagementService"
            ref="cardManagementService" />
    </bean>

    <bean id="CardsList" name="/cardsList"
        class="com.qantas.cardselector.action.cardslist.CardsListAction">
        <property name="cardManagementService"
            ref="cardManagementService" />
    </bean>

<bean id="transactionProxy"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
        abstract="true">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="cardManagementService" parent="transactionProxy">
        <property name="target">
            <bean class="com.qantas.cardselector.service.impl.CardManagementServiceImpl">
                <property name="cardManagementDAO" ref="cardManagementDAO" />
            </bean>
        </property>
    </bean>

    <bean id="cardManagementDAO" class="com.qantas.cardselector.dao.impl.CardManagementDAOimpl">
        <property name="hibernateTemplate" ref="hibernateTemplate" />       
    </bean>


</beans>

我的行动课就在这里

 package com.qantas.cardselector.action.cardslist;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.qantas.cardselector.form.CardsListForm;
import com.qantas.cardselector.service.CardManagementService;
import com.qantas.cardselector.vo.SearchFilterVO;

public class CardsListAction extends Action {

    public CardManagementService cardManagementService;

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {   
            System.out.println("action started");
            SearchFilterVO searchFilterVO =null;
            if(getCardManagementService()!=null)
            {
                String returnvalue = getCardManagementService().retrieveParams(searchFilterVO);
                System.out.println("returnvalue"+returnvalue);
            }


        } catch (Exception e) {
            System.out.println("Error:" + e.getMessage());
            e.printStackTrace();
        }

        return mapping.findForward("success");
    }

    public CardManagementService getCardManagementService() {
        return cardManagementService;
    }

    public void setCardManagementService(CardManagementService cardManagementService) {
        this.cardManagementService = cardManagementService;
    }

}

这个动作类总是在调用代码中显示的服务对象时抛出空指针异常...请任何人帮忙......

1 个答案:

答案 0 :(得分:0)

伙计我已在下面的问题中找到答案,

刚刚在struts-config.xml

中添加了一段代码
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="csntextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>

并添加了一个空的action-servlet.xml文件