gpt4 book ai didi

java - 如何以编程方式配置 TransactionManager

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:24 26 4
gpt4 key购买 nike

我这里有一个基于 Spring MVC 的应用程序。我的同事(顺便说一句,他不在这里)以编程方式配置了它,一切似乎都正常,除了 TransactionManager。我从来没有像这样配置过 Spring Web 应用程序,我不知道该怎么做,也找不到任何关于如何配置这样的 Web 应用程序的文档。

我将只向您展示“AppInitializer”和“EntityManagerConfig”。

AppInitializer:

public class AppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context;
ServletRegistration.Dynamic dispatcherServletRegistration;
FilterRegistration.Dynamic encodingFilterRegistration, compressionFilterRegistration;
Set<SessionTrackingMode> sessionTrackingModes = new HashSet<SessionTrackingMode>();

sessionTrackingModes.add(SessionTrackingMode.SSL);

context = new AnnotationConfigWebApplicationContext();
context.setServletContext(servletContext);
context.scan("de.devbliss.doc");

servletContext.addListener(new ContextLoaderListener(context));
servletContext.addListener(new Log4jConfigListener());

dispatcherServletRegistration = servletContext.addServlet("main", new DispatcherServlet(context));
dispatcherServletRegistration.setLoadOnStartup(1);
dispatcherServletRegistration.addMapping("/*");

encodingFilterRegistration = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
encodingFilterRegistration.setInitParameter("encoding", "UTF-8");
encodingFilterRegistration.setInitParameter("forceEncoding", "true");
encodingFilterRegistration.addMappingForUrlPatterns(null, false, "/*");

compressionFilterRegistration = servletContext.addFilter("compressionFilter", GzipFilter.class);
compressionFilterRegistration.addMappingForUrlPatterns(null, false, "/*");

compressionFilterRegistration = servletContext.addFilter("springSecurityFilterChain",
DelegatingFilterProxy.class);
compressionFilterRegistration.addMappingForUrlPatterns(null, false, "/*");

servletContext.setSessionTrackingModes(sessionTrackingModes);
}
}

实体管理器配置:

@Configuration
@PropertySource("classpath:/db.properties")
public class EntityManagerConfig {

@Bean
public DataSource dataSource(Environment env) {
BasicDataSource ds = new BasicDataSource();

ds.setUrl(env.getProperty("url", "localhost"));
ds.setUsername(env.getProperty("user", "blissdoc"));
ds.setPassword(env.getProperty("password", "s3cret"));

return ds;
}

@Bean
@Inject
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();

factory.setDataSource(dataSource);

return factory;
}

@Bean
public HibernateTransactionManager transactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager tm = new HibernateTransactionManager(
sessionFactory);
return tm;
}

@SuppressWarnings("unchecked")
@Bean
@Inject
public LocalContainerEntityManagerFactoryBean entityManager(
DataSource dataSource, AbstractEnvironment env) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
HibernateJpaDialect jpaDialect = new HibernateJpaDialect();
org.springframework.core.env.PropertySource<?> source;
Iterator<org.springframework.core.env.PropertySource<?>> sources;

// jpaVendorAdapter.setDatabase(Database.MYSQL);

jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setShowSql(true);

Properties jpaProperties = new Properties();

sources = env.getPropertySources().iterator();
while (sources.hasNext()) {
source = sources.next();
if (source.getSource() instanceof Map) {
for (Map.Entry<String, String> property : ((Map<String, String>) source
.getSource()).entrySet()) {
jpaProperties.put(property.getKey(), property.getValue());
}
}
}

em.setJpaProperties(jpaProperties);

em.setDataSource(dataSource);
em.setPersistenceUnitName("blissdoc-unit");
em.setPackagesToScan("de.devbliss.doc.model");
em.setJpaDialect(jpaDialect);
em.setJpaVendorAdapter(jpaVendorAdapter);

return em;
}

// @Bean
// @Inject
// public JpaTransactionManager jpaTransactionManager(
// EntityManagerFactory entityManagerFactory) {
// JpaTransactionManager tm = new JpaTransactionManager(
// entityManagerFactory);
// return tm;
// }

@Bean
@Inject
public JpaRepositoryFactory jpaRepositoryFactory(
EntityManagerFactory entityManagerFactory) {
JpaRepositoryFactory factory = new JpaRepositoryFactory(
entityManagerFactory.createEntityManager());
return factory;
}

@Bean
@Inject
public UserRepository userRepository(
JpaRepositoryFactory jpaRepositoryFactory) {
return jpaRepositoryFactory.getRepository(UserRepository.class);
}

@Bean
@Inject
public ProjectRepository projectRepository(
JpaRepositoryFactory jpaRepositoryFactory) {
return jpaRepositoryFactory.getRepository(ProjectRepository.class);
}
}

---更新PersistenceJPAConfig(以前的 EntityManagerConfig):

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:/db.properties")
public class PersistenceJPAConfig {

@Inject
private Environment env;

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setPackagesToScan(new String[] { "de.devbliss.doc" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter() {
{
// JPA properties
}
};
factoryBean.setJpaVendorAdapter(vendorAdapter);

return factoryBean;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(env.getProperty("url", "localhost"));
dataSource.setUsername(env.getProperty("user", "blissdoc"));
dataSource.setPassword(env.getProperty("password", "s3cret"));
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean()
.getObject());

return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}

最佳答案

@Configuration -自定义 XML 元素的特定对应物,例如 <tx:annotation-driven>@Enable...注释。

为了启用对 @Transactional 的支持你需要注释你的 @Configuration@EnableTransactionManagement上课:

@Configuration 
@PropertySource("classpath:/db.properties")
@EnableTransactionManagement
public class EntityManagerConfig { ... }

另请参阅:

关于java - 如何以编程方式配置 TransactionManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971007/

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