0

我已经开发了一个 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;
    }
}

现在,我不想使用这种结构,我想创建一个包含所有服务接口、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 配置工作正常。请给出解决方案。提前致谢。

4

1 回答 1

0

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

于 2012-07-17T09:05:47.360 回答