【问题标题】:Context initialization failed, Bean creation exception上下文初始化失败,Bean创建异常
【发布时间】:2014-06-23 09:02:09
【问题描述】:

我有上述异常的问题,请给出指导方针,这是我的代码:

//Calling Class


 package com.company.product.wsai.qb.ws.endpoint;

//Imports

@Endpoint
public class SendRequestXMLEndpoint implements SendRequestXMLManagementService {

    String strErrorMsg = "";

    // xml logging
    private static final String SEND_REQUEST_XML = "sendRequestXML";
    private static final String SEND_REQUEST_XML_RESPONSE = "sendRequestXMLResponse";

    @Autowired
    com.intuit.developer.ObjectFactory wsObjectFactory;

    @Autowired
    com.intuit.quickbooks.ObjectFactory qbObjectFactory;

    @Autowired
    TestService testService;

    @Autowired
    HistoryServiceImpl historyService;

    public static List<TaCategorySyncEntityDTO> travelAgentCategoryList ;   //Match to customerType in QB

    public static List<TravelAgentSyncEntityDTO> travelAgentList;   //Match to customer in QB

    public static List<VendorSyncEntityDTO> vendorList;   //Match to vendor in QB

    public static List<CurrencySyncEntityDTO> currencyList;   //Match to currency in QB

    public static List<ItemSyncEntityDTO> itemInventoryList; //Match to Item in QB

    public void doTravelAgent(){

        try{

        //Methods
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doTravelAgentCategory(){
            try{

            //Methods 

            }catch (Exception e) {
                e.printStackTrace();
            }
        }


    public void doVendor(){
        try{

          //Methods 
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doCurrency(){
        try{

        //Methods

        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PayloadRoot(localPart = "sendRequestXML", namespace = XmlConfig.QB_WC_NAMESPACE)
    @ResponsePayload
    public SendRequestXMLResponse sendRequestXMLCommon(@RequestPayload SendRequestXML sendRequestXML) {
         doTravelAgentCategory();
         doTravelAgent();
         doVendor();
         doCurrency();
        String sessionTicket = sendRequestXML.getTicket();
        String strCompanyFileName = sendRequestXML.getStrCompanyFileName();

        // log request
        XmlLogManager.logSendRequestXMLEnpoint(sendRequestXML, SEND_REQUEST_XML, sessionTicket);

        SendRequestXMLResponse sendRequestXMLResponse = wsObjectFactory.createSendRequestXMLResponse();

        sendRequestXMLResponse.setSendRequestXMLResult(getSentXMLResult());

        // log response
        XmlLogManager.logSendRequestXMLEnpoint(sendRequestXMLResponse, SEND_REQUEST_XML_RESPONSE, sessionTicket);

        updateHistory();

        return sendRequestXMLResponse;

        }


    private String getSentXMLResult() {

        try{

            //Generate XML for quickBook

        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return stringWriter.toString();
    }       


    public void updateHistory(){

        History taHistory = new History();
    Date dtReturn = StringToDate("19-06-2014");
        System.out.println(dtReturn);
        taHistory.setId(1L);
        taHistory.setEntityEvent('C');
        taHistory.setEntityType("TRAVELAGENT");
        taHistory.setEntityName("TEST");
        taHistory.setHotelCode("BBH");
        taHistory.setRecordTransfered('1');
        taHistory.setCreatedBy("TEST_USER");
        taHistory.setCreatedDate(dtReturn);
        taHistory.setVersion(1);

        try{
            historyService.saveHistory(taHistory);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Date StringToDate(String strDate) {
        Date dtReturn = null;
         return dtReturn;
    }
}


//DAO Implementation class

    package com.company.product.wsai.qb.dao.historyDAO;

//imports

public class HistoryDAOImpl implements HistoryDAO {
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void saveHistory(History history) {
        sessionFactory.getCurrentSession().save(history);
    }
}

//Service implementation 

package com.company.product.wsai.qb.historyService;

//imports 

公共类 HistoryServiceImpl 实现 HistoryService {

@Autowired
private HistoryDAO historyDAO;


public void saveHistory(History history) {
    historyDAO.saveHistory(history);
 }
}

//Config xml

//remove upper part 

<context:component-scan base-package="com.jkcsworld.zhara.wsai.qb.historyService" />
    <context:component-scan base-package=" com.jkcsworld.zhara.wsai.qb.dao.historyDAO" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>/resources/hibernate.cfg.xml</value>
        </property>
     </bean>

     <bean id="historyDAO" class="com.jkcsworld.zhara.wsai.qb.dao.historyDAO.HistoryDAOImpl">
         <property name="sessionFactory" ref="sessionFactory"/>
     </bean>

</beans>

当我在调用类中包含 autowired 时,屏幕截图中出现错误

【问题讨论】:

    标签: java xml spring hibernate spring-orm


    【解决方案1】:

    你的组件扫描也应该涵盖这个包

    package com.company.product.wsai.qb.ws.endpoint;
    

    如下编辑你的config.xml,一个组件扫描就足够了。

    <context:component-scan base-package="com.jkcsworld.zhara.wsai.qb" />
    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>/resources/hibernate.cfg.xml</value>
        </property>
     </bean>
    
     <bean id="historyDAO" class="com.jkcsworld.zhara.wsai.qb.dao.historyDAO.HistoryDAOImpl">
         <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
    

    问题是找不到与SendRequestXMLEndpoint.historyService匹配的bean

    所以将你的服务标记为

    package com.company.product.wsai.qb.historyService;
    //imports
    
    @Service // Mark it as service
    public class HistoryServiceImpl implements HistoryService {
    
        @Autowired
        private HistoryDAO historyDAO;
    
    
        public void saveHistory(History history) {
            historyDAO.saveHistory(history);
        }
    }
    

    【讨论】:

      【解决方案2】:
      @Autowired
          public void setSessionFactory(SessionFactory sessionFactory) {
              this.sessionFactory = sessionFactory;
          }
      

      修改您的 HistoryDAOImpl 类以使用上述方法。它应该可以工作。

      【讨论】:

        猜你喜欢
        • 2014-12-09
        • 1970-01-01
        • 2011-04-13
        • 2018-01-08
        • 1970-01-01
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        • 2017-03-17
        相关资源
        最近更新 更多