gpt4 book ai didi

java - Jersey + Hibernate 将 SessionFactory 注入(inject) DAO

转载 作者:行者123 更新时间:2023-11-30 06:59:16 25 4
gpt4 key购买 nike

我有一个 jax-rs/jersey Rest 应用程序,它使用 Hibernate 5.2 作为 orm。还有一个 spring 过滤器使用 token 处理身份验证。一切都运行得很好,但有一个小问题。每个 dao 对象都像这样在构造时创建自己的 session 工厂。

public abstract class BaseDAO<T> {

protected SessionFactory sessionFactory = getSessionFactory();
protected final Validator validator = getValidator();

protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}

protected Validator getValidator() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return factory.getValidator();
}

@SuppressWarnings({"hiding", "unchecked"})
public <T> T save(final T o) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
T savedObj = (T) session.save(o);
tx.commit();
session.close();
return savedObj;
}

public void delete(final Object object) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
session.delete(object);
tx.commit();
session.close();
}

@SuppressWarnings("hiding")
public <T> T get(final Class<T> type, final int id) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
T obj = session.get(type, id);
tx.commit();
session.close();
return obj;
}

@SuppressWarnings({"hiding", "unchecked"})
public <T> T merge(final T o) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
T obj = (T) session.merge(o);
tx.commit();
session.close();
return obj;
}

@SuppressWarnings("hiding")
public <T> void saveOrUpdate(final T o) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(o);
tx.commit();
session.close();
}

@SuppressWarnings({ "hiding", "deprecation", "unchecked" })
public <T> List<T> getAll(final Class<T> type) {
final Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
final Criteria crit = session.createCriteria(type);
List<T> results = crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
tx.commit();
session.close();
return results;
}

@SuppressWarnings({ "hiding", "deprecation", "unchecked" })
public <T> List<T> findByExample(final Class<T> type, T instance) {
try {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
List<T> results = session
.createCriteria(type)
.add(Example.create(instance))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
tx.commit();
session.close();
return results;
} catch (RuntimeException re) {
//log.error("find by example failed", re);
throw re;
}
}

这是一个问题,因为如果您创建多个 dao 对象,您很快就会耗尽连接,即使我在每次 dao 调用后调用 session.close() 也是如此。对资源进行过多调用后,我的小型数据库实例提示连接过多。我立即想到的是 session 工厂需要由 Jersey 管理。所以我尝试像这样使用工厂绑定(bind)器:

public class HibernateSessionFactory implements Factory<SessionFactory> {

protected SessionFactory sessionFactory;

@Override
public void dispose(SessionFactory arg0) {
sessionFactory.close();
}

@Override
public SessionFactory provide() {
try {
sessionFactory = (SessionFactory) new Configuration().configure().buildSessionFactory();
return sessionFactory;
} catch (Exception e) {
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}
}

在资源配置中:

    register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(HibernateSessionFactory.class).to(SessionFactory.class);
}
});

并将 BaseDAO 更改为此

@Inject
protected SessionFactory sessionFactory

它似乎不起作用 - session 工厂始终为空。另一个问题是 Spring 过滤器不能使用 Jersey 注入(inject)。过滤器的配置如下。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Order(2)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

private final com.renewedvoices.security.UserService userService;
private final TokenAuthenticationService tokenAuthenticationService;
private final SessionFactory sessionFactory;

public SpringSecurityConfig() {
super(true);
this.userService = new UserService();
this.tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService);
this.sessionFactory = createSessionFactory();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
// Custom Token based authentication based on the header previously given to the client
http
.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService),
UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/rest/auth/**").permitAll()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling().and()
.anonymous().and()
.servletApi().and()
.headers().cacheControl();

}

private SessionFactory createSessionFactory() {
try {
return (SessionFactory) new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
} catch (Exception e) {
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Bean
@Override
public UserService userDetailsService() {
return userService;
}

@Bean
public TokenAuthenticationService tokenAuthenticationService() {
return tokenAuthenticationService;
}

@Bean
public SerssionFactory sessionFactory() {
return sessionFactory;
}
}

无论我在BaseDao中使用@Autowired还是@Inject,sessionFactory总是为null。我无法切换到纯粹的 Spring 。有什么方法可以让这个 sessionFactory 工作吗?这是在休息服务中处理 session 工厂的最佳方法还是有更好的方法不对每个请求打开新 session ?我找到的几乎所有答案都是仅限 Spring 的解决方案。任何帮助是极大的赞赏。

最佳答案

继续我上面的评论

You need to make the DAO a Spring bean so that it can be injected into Spring components. Then for Jersey you need to integrate Jersey to use Spring components so you can inject into Jersey. Have a look at this post.

所以您需要做的就是使 DAO 成为 Spring Bean。你可以这样做

@Configuration
public class DataConfiguration {
@Bean
public MyDao myDao() {
return new MyDao();
}
}

然后在您的 AbstractSecurityWebApplicationInitializer 中,您需要添加配置类,并覆盖 contextConfigLocation 属性,以便 Jersey 不会尝试创建 ContextLoaderListener。这已经由 Spring Security 创建了

@Order(1)
public class DemoSecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer {

public DemoSecurityWebApplicationInitializer() {
super(DataConfiguration.class, SecurityConfig.class);
}

@Override
public void afterSpringSecurityFilterChain(ServletContext servletContext) {
// Set the Jersey used property to it won't load a ContextLoaderListener
servletContext.setInitParameter("contextConfigLocation", "NOOP");
}
}

您需要做的最后一件事是添加 jersey-spring3 依赖项。这是 Jersey 依赖项,允许它与 Spring 组件集成(仅 Spring 到 Jersey,而不是 Jersey 到 Spring)。

<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<exclusions>
<!-- exclude Spring 3 if you want to use Spring 4 -->
</exclusions>
<dependency>

请参阅 this GitHub project 中的完整示例

关于java - Jersey + Hibernate 将 SessionFactory 注入(inject) DAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215354/

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