如何在SpringMVC中的不同类中创建动态bean

如何在SpringMVC中的不同类中创建动态bean,spring,hibernate,spring-bean,hibernate-generic-dao,Spring,Hibernate,Spring Bean,Hibernate Generic Dao,我需要在运行时使用动态bean工厂为不同的条件创建具有不同类的动态bean。它是用于一般DAO实现的。如何使用Java配置实现它 MVC初始化器类 使用原型bean配置 import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationI

我需要在运行时使用动态bean工厂为不同的条件创建具有不同类的动态bean。它是用于一般DAO实现的。如何使用Java配置实现它

MVC初始化器类

使用原型bean配置

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class SpringMvcInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(AppConfig.class);
     /*   serviceA.setEntityClass((Class<?>) Education.class);
        IGenericDao ff=appContext.getBean(IGenericDao.class,"IGenericDao");*/

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        appContext.setServletContext(servletContext);
        appContext.refresh();
        //appContext.getBean("IGenericDao");
       // Services serviceA = new Services(Education.class);
        Services<?> serviceA = (Services<?>)appContext.getBean("IGenericDao");
        serviceA.setEntityClass((Class<?>) Education.class);
        // serviceA = (Services)appContext.getBean("IGenericDao");
        //serviceA.setEntityClass((Class<?>) Education.class);
       // serviceA.setEntityClass(Employee.class);
        serviceA.setName("hellooo");
        serviceA.getName();

        //appContext.
        //serviceA=new Services(T clazz);
    }
}
import javax.servlet.ServletContext;
导入javax.servlet.ServletException;
导入javax.servlet.ServletRegistration;
导入org.springframework.web.WebApplicationInitializer;
导入org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
导入org.springframework.web.servlet.DispatcherServlet;
公共类SpringMVCiInitializer实现WebApplicationInitializer{
启动时公共void(ServletContext ServletContext)引发ServletException{
AnnotationConfigWebApplicationContext appContext=新的AnnotationConfigWebApplicationContext();
register(AppConfig.class);
/*serviceA.setEntityClass((Class)Education.Class);
IGenericDao ff=appContext.getBean(IGenericDao.class,“IGenericDao”)*/
ServletRegistration.Dynamic dispatcher=servletContext.addServlet(“SpringDispatcher”,新DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(“/”);
setServletContext(servletContext);
appContext.refresh();
//getBean(“IGenericDao”);
//服务服务A=新服务(教育类);
Services serviceA=(Services)appContext.getBean(“IGenericDao”);
serviceA.setEntityClass((Class)Education.Class);
//serviceA=(Services)appContext.getBean(“IGenericDao”);
//serviceA.setEntityClass((Class)Education.Class);
//serviceA.setEntityClass(Employee.class);
服务a.设置名称(“hellooo”);
serviceA.getName();
//appContext。
//服务A=新服务(T类);
}
}
试试这个代码

    BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) appContext.getBeanFactory();
    beanFactory.registerBeanDefinition("IGenericDao",
            BeanDefinitionBuilder.genericBeanDefinition(Employee.class)           
                    .getBeanDefinition()
    );  
试试这个代码

    BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) appContext.getBeanFactory();
    beanFactory.registerBeanDefinition("IGenericDao",
            BeanDefinitionBuilder.genericBeanDefinition(Employee.class)           
                    .getBeanDefinition()
    );  

你应该包括更多的细节,并展示你失败的努力,以便更容易理解你想要实现的目标。你应该包括更多的细节,并展示你失败的努力,以便更容易理解你想要实现的目标。