如何从类中获取会话对象?

时间:2016-12-05 14:04:34

标签: spring spring-mvc spring-4

有一个课程:

@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)对象?

1 个答案:

答案 0 :(得分:3)

使用Controller类

您可以将HttpSession定义为控制器方法的参数:

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

这也是访问HttpRequest的推荐方式,而不是通过RequestContextHolder,因为它可以更轻松地模拟会话进行测试。

请参阅the Spring MVC docs for more detail.

不使用控制器类

另一种选择是使用@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";
    }

}