简体   繁体   中英

How to map beans from given jar file in Spring

I have developed a Spring web application and its working fine. I have bean creation mapping structure as like : My Controller:

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

Now, I don't want to use this structure, I want to create a jar file of all Service Interface, Impl class, DAO Interface and Impl Class that means except controller everything should be in jar file. But when I create a jar file and add in project's class path and run the project that time an exception occurred : Exception is:

 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

So, how can I configure that will map the beans defined in xml file by Spring. My bean configuration is working fine when I run project without using that jar file. Please give a solution. Thanks in advance.

why you are mapping the controller with the annotation and mapping the service and dao classes with the xml,spring searches for annotated objects first and then loads the xml beans, and in your application you should load the dao then service and finally the controllers. Make them all annotated with @Service and @Repository or even with @Component so you don't need to worry about what objects are loaded before the other one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM