Results 1 to 3 of 3

Thread: Dependency Injection Failing

  1. #1

    Default Dependency Injection Failing

    Hi,
    I am trying to inject instance of class "SaveCardInfoDAO" into ejb method "insertCardInfo". Following are the code snippets.
    Controller (Stateless session bean) in Project 1
    Code:
    /**
    * Exposed Message to insert the Credit Card Info into the database. 
    */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void insertCardInfo(CreditCardDO ccdo){
    SaveCardInfoDAO saveCardInfo = (SaveCardInfoDAO) ApplicationUtil.getSpringContext().getBean("saveCardInfo");
    //SaveCardInfoDAO saveCardInfo = new SaveCardInfoDAO(ccsDataSource);
    try{
    saveCardInfo.openConnection(false);
    saveCardInfo.insertCardInfo(ccdo);
    System.out.println("Insert Success !!!");
    }
    catch(SQLException e){
    ejbContext.setRollbackOnly();
    System.out.println("Insert Failure !!!");
    }
    finally{
    saveCardInfo.closeConnection();
    }
    }
    ApplicationUtil class (that initializes the Application context) required in above code is in Project 2.

    Code:
    public class ApplicationUtil {
    private static final ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    static{
    init();
    }
    
    public static void init(){
    DOMConfigurator.configure("resource/ccslogger.xml");
    }
    public static ApplicationContext getSpringContext(){
    return context;
    }
    }
    applicationContext.xml is in Project2

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <aop:aspectj-autoproxy />
    <bean id="creditCardAdvice" class="com.fedex.ccs.advice.CCSCreditCardInfoAdvice" />
    <bean id="saveCardInfo" class="com.fedex.ccs.dao.SaveCardInfoDAO" /> 
    <bean id="creditCardDO" class="com.fedex.ccs.domain.CreditCardDO" />
    
    </beans>
    SaveCardInfoDAO class whose instance is to be inserted in the first code snippet is in Project 3.

    Code:
    public class SaveCardInfoDAO extends CCSBaseDAO {
    private PreparedStatement stmt;
    public void setPreparedStatement() throws SQLException{
    stmt=getConnection().prepareStatement(SQLQueriesBase.CCS_Credit_Card_Info.INSERT_CREDIT_CARD_INFO);
    }
    public PreparedStatement getPreparedStatement(){
    return stmt;
    }
    public SaveCardInfoDAO(){
    }
    public SaveCardInfoDAO(DataSource ccsDataSource){
    super(ccsDataSource);
    }
    /**
    * Insert credit card information into the database table.
    * @param ccdo
    * @throws SQLException
    */
    public int insertCardInfo(CreditCardDO ccdo)throws SQLException{
    setPreparedStatement();
    stmt.setObject(1, ccdo.getCreditCardType(), Types.VARCHAR);
    stmt.setObject(2, ccdo.getCreditCardID(), Types.VARCHAR);
    stmt.setObject(3, ccdo.getCreditCardBIN(), Types.VARCHAR);
    stmt.setObject(4, ccdo.getCreditCardExpDt(), Types.VARCHAR);
    stmt.setObject(5, ccdo.getAccountNumber(), Types.VARCHAR);
    int result = stmt.executeUpdate();
    stmt.close();
    return result;
    }
    }
    Project 1 has dependency on project 2 and project 3.
    Project 3 has dendency on Project 2.
    I am using Eclipse and all these projects are eclipse projects.

    When I run my client this gives me following error.
    javax.ejb.EJBTransactionRolledbackException: EJB Exception: : java.lang.NoClassDefFoundError: com/fedex/ccs/util/ApplicationUtil
    at com.fedex.ccs.ejb.session.Controller.insertCardInf o(Controller.java:44)

    Please help me resolve this issue. Let me know if you need something else.

    Regards
    Damodar

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    1) Dependency Injection problems should be discussed in the Container section. This is the AOP section but there is no aop involved;

    2) You are using EJB so yours is a web application. For web application, application context must be loaded through listener and not by doing new classpathxml etc. which is for standalone applications only;

    3) EJBs don't work very well with Spring, especially EJB 2.x. In any case, you should integrate them correctly following instructions in the Spring reference guide;

    4) You're not using Spring jdbc support. I wonder why you are using Spring at all if you don't really use it!

    5) NoClassDefFoundError means either you're missing a library in your project, or you're messing up with classloaders (classic jar hell).

  3. #3

    Default

    Thanks for the reply, it helped me.
    I am closing this topic as my real problem lies in Spring AOP. I will really appreciate if you please have a look at following thread related to Spring AOP, AspectJ amd EJB3.0
    http://forum.springsource.org/showth...430#post363430

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •