如何在Spring中从给定的jar文件映射bean

时间:2012-06-27 10:27:42

标签: java spring java-ee spring-mvc spring-webflow

我开发了一个Spring Web应用程序,它的工作正常。我有bean创建映射结构,如: 我的控制器:

@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;
    }
}

现在,我不想使用这个结构,我想创建一个所有Service Interface,Impl类,DAO接口和Impl类的jar文件,这意味着除了控制器外,一切都应该在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

那么,我该如何配置它将映射Spring在xml文件中定义的bean。 当我在不使用该jar文件的情况下运行项目时,我的bean配置工作正常。 请给出一个解决方案。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

为什么要使用注释映射控制器并使用xml映射服务和dao类,spring首先搜索带注释的对象然后加载xml bean,然后在应用程序中加载dao然后加载服务,最后是控制器。使用 @Service @Repository 或者使用 @Component 对它们进行全部注释,这样您就不必担心之前加载了哪些对象另一个。