2012-06-27 78 views
0

我開發了一個Spring web應用程序,它的工作正常。我有bean創建映射結構,如: 我的控制器:如何在Spring中從給定的jar文件映射beans

@Controller 
@RequestMapping("appointmentDiary") 
public class AppointmentDiaryController { 
    private IAppointmentDiaryService appointmentDiaryService; 
    public IAppointmentDiaryService getAppointmentDiaryService() { 
     return appointmentDiaryService; 
    } 
    public void setAppointmentDiaryService(IAppointmentDiaryService appointmentDiaryService) { 
     this.appointmentDiaryService = appointmentDiaryService; 
    } 
} 

My Service Interface: 
public interface IAppointmentDiaryService 
{ 
    public Integer getAppointmentDiaryNo(); 
} 

My Impl Class: 
public class AppointmentDiaryServiceImpl implements IAppointmentDiaryService{ 
    private IAppointmentDiaryDAO appointmentDiaryDAO; 
    public IAppointmentDiaryDAO getAppointmentDiaryDAO(){ 
     return appointmentDiaryDAO; 
    } 
    public void setAppointmentDiaryDAO(IAppointmentDiaryDAO appointmentDiaryDAO) { 
     this.appointmentDiaryDAO = appointmentDiaryDAO; 
    } 
    public Integer getAppointmentDiaryNo(){  
     InternalResultsResponse<Object> objResponse = getAppointmentDiaryDAO().getAppointmentDiaryNo(); 
     return objResponse; 
    } 
My DAO Interface: 
public interface IAppointmentDiaryDAO extends IGenericDAO 
{ 
    public InternalResultsResponse<Object> getAppointmentDiaryNo(); 
} 

My DAO Impl calss: 
public class AppointmentDiaryDAOImpl extends GenericDAOImpl implements 
     IAppointmentDiaryDAO { 
    public InternalResultsResponse<Object> getAppointmentDiaryNo() { 
     InternalResultsResponse<Object> response = new InternalResultsResponse<Object>(); 
     String sql = SqlProperties.getSQLStatement("getAppointmentDiaryNo"); 
     Session session = getSession(); 
     Transaction tr = session.beginTransaction(); 
     response = HibernateUtil.executeSQLQuery(session, sql); 
     tr.commit(); 
     return response; 
    } 
} 

現在,我不想用這個結構,我想創建的所有服務接口,實現類,DAO接口的jar文件, Impl類除了控制器之外的所有東西都應該在jar文件中。但是,當我創建一個jar文件,並添加項目的類路徑和運行項目時發生了異常: 的例外是:

org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.nmmc.cess.service.impl.AppointmentDiaryServiceImpl] for bean with name 'appointmentDiaryServiceImpl' defined in ServletContext resource [/WEB-INF/config/cess-service-application-context.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: com/nmmc/cess/service/IAppointmentDiaryService 

所以,我怎麼能配置將映射在XML文件中定義的豆彈簧。 當我運行不使用該jar文件的項目時,我的bean配置工作正常。 請給一個解決方案。 在此先感謝。

+3

你必須把這個jar放到lib文件夾中。 –

+0

@SachinJ謝謝,它的工作... – Balasaheb

回答

0

爲什麼你將控制器映射到註釋並使用xml映射服務和dao類,Spring首先搜索帶註釋的對象,然後加載xml bean,然後在應用程序中加載dao然後加載服務,最後控制器。讓他們都與@Service@Repository甚至與@Component註釋,所以你不必擔心其他人之前被加載哪些對象。