gpt4 book ai didi

java - OpenEntityManagerInViewFilter - 未定义的EntityManagerFactory

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:56 25 4
gpt4 key购买 nike

为了克服 LazyInitializationException,我决定使用 OpenEntityManagerInViewFilter - 这是我的 AppInitializer 类中实现 WebApplicationInitializer 的代码:

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerListener(servletContext);
registerDipsatcherServlet(servletContext);
registerOpenEntityManagerInViewFilter(servletContext);
}

private void registerListener(ServletContext servletContext) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// Manage the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
}

private void registerDipsatcherServlet(ServletContext servletContext) {
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(MvcConfiguration.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
customizeRegistration(dispatcher);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}

private void registerOpenEntityManagerInViewFilter(ServletContext servletContext) {
FilterRegistration.Dynamic registration = servletContext.addFilter("openEntityManagerInView", new OpenEntityManagerInViewFilter());
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
}

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}

@Override
protected Class<?>[] getRootConfigClasses() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected Class<?>[] getServletConfigClasses() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected String[] getServletMappings() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

这是定义 bean 的 @Configuration 类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager", basePackages = "podraza.piotr.eshopper.repository")
public class PersistenceConfiguration {

@Autowired
private JpaVendorAdapter jpaVendorAdapter;

@Value("${jpa.show-sql:false}")
private boolean showSql;

@Value("${jpa.datasource.url}")
private String databaseUrl;

@Value("${jpa.datasource.username}")
private String username;

@Value("${jpa.datasource.password}")
private String password;

@Value("${jpa.datasource.driverClassName}")
private String driverClassName;
/*
@Value("${jpa.hibernate.ddl-auto}")
private String hibernateDdlAuto;
*/

@Bean
public HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(showSql);
//hibernateJpaVendorAdapter.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", hibernateDdlAuto);
hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
return hibernateJpaVendorAdapter;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseUrl, username, password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}

@Bean
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}

@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("podraza.piotr.eshopper.entity");
lef.setPersistenceUnitName("defaultPersistenceUnit");
lef.afterPropertiesSet();
return lef.getObject();
}

@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}

}

如何为过滤器指定此 bean?

最佳答案

您正在使用 Spring 类,但您要做的第一件事就是不使用它。将您的 AppInitializer 替换为以下内容。

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {PersistenceConfiguration.class}
}

@Override
protected Class<?>[] getServletConfigClasses() {
throw new Class[] {MvcConfiguration.class};
}

@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}

protected Filter[] getServletFilters() {
return return new Filter[] {new OpenentityManagerInViewFilter()};
}

}

还要确保在您的 MvcConfiguration 中您没有再次加载 PersistenceConfiguration 类,因为这会导致 Bean 重复。

关于java - OpenEntityManagerInViewFilter - 未定义的EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com