gpt4 book ai didi

spring - 如何从类中获取一般 session 对象?

转载 作者:行者123 更新时间:2023-12-01 19:43:12 24 4
gpt4 key购买 nike

有一个类:

@Configuration
@ComponentScan("com.ambre.pta")
@EnableTransactionManagement
@PropertySources({
@PropertySource("classpath:fr/global.properties"),
@PropertySource("classpath:fr/main.properties"),
@PropertySource("classpath:fr/admin.properties"),
@PropertySource("classpath:fr/referentiel.properties"),
@PropertySource("classpath:fr/departement.properties"),
@PropertySource("classpath:fr/exercice.properties"),
@PropertySource("classpath:fr/defi.properties")
})
public class ApplicationContextConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
return new PropertySourcesPlaceholderConfigurer();
}

@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}

@Bean(name = "dataSource")
public DataSource getDataSource() {

BasicDataSource bds = new BasicDataSource();

bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
bds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");

bds.setUsername("pta");
bds.setPassword("pta");

return bds;

}

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {

LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

sessionBuilder.scanPackages("com.ambre.pta.model");

return sessionBuilder.buildSessionFactory();

}

@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {

HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

return transactionManager;

}

@Autowired
@Bean(name = "utilisateurDao")
public UtilisateurDAO getUtilisateurDao(SessionFactory sessionFactory) {
return new UtilisateurDAOImpl(sessionFactory);
}

}

我们可以通过以下方式获取此类中的request对象:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

那么有没有办法获取session(HttpSession)对象?

最佳答案

使用 Controller 类

您可以将 HttpSession 定义为 Controller 方法的参数:

@RequestMapping("/my-path")
public String doStuff(HttpSession session) {
// do stuff...
}

这也是访问 HttpRequest 的推荐方法,而不是通过 RequestContextHolder,因为它更容易模拟 session 以进行测试。

参见the Spring MVC docs for more detail.

不使用 Controller 类

另一种选择是使用 @SessionScope bean 并相应地改变它:

@Component
@SessionScope
public class MySessionScopedBean {
String attribute;
}

public class MyOtherClass {
@Autowired
private MySessionScopedBean myBean;

public void doStuff() {
// myBean accesses a bean instance isolated in the user session
myBean.attribute = "test";
}

}

关于spring - 如何从类中获取一般 session 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40976098/

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