Spring 3.1 configuration: environment not injected

时光毁灭记忆、已成空白 提交于 2019-12-01 15:46:57

Not sure why, but using the @Resource annotation worked for me. @Autowired always returned null.

The problem is with the spring security for the remember me feature. if I take this line <code> <remember-me data-source-ref="dataSource" /> </code> out. everything works fine. if this line presents, it will try to load the db before anything else and env was never injected.

If you don't use full Java EE compatible server you have to include javax.inject.jar to your project classpath to add the support of @Inject. You can also try to use spring's native @Autowired annotation.

@jfd,

I don't immediately see anything wrong with your configuration that would cause a failure to inject the Environment.

If you start from scratch with an empty @Configuration class, and then @Inject the Environment, does it work for you?

If yes, then at what point does it begin to fail?

Would you be willing to reduce the example down to the smallest possible configuration that fails and submit it as a reproduction project? The instructions here make this as simple as possible: https://github.com/SpringSource/spring-framework-issues#readme

Thanks!

I've detect a similar error for my project as mentioned here. I've also figure out, that a call of afterproperties is necessary to get the sessionFactory. ... and yes, I'm using Spring Security too (which may be the source of the problem).

My @Configuration annotated class uses @ComponentScan for packages containing Hibernate based DAOs and a @Bean annotated method for creating the SessionFactory used by the DAOs. At runtime, a exception is thrown, mentioned that 'sessionFactory' or 'hibernateTemplate' was not found. It seems that the DAOs are constructed before the SessionFactory was created. One workaround for me was to put the component scan directive back in a XML file () and replace @ComponentScan with @ImportResource of that file.

@Configuration
//@ComponentScan(basePackages = "de.webapp.daocustomer", excludeFilters = {@ComponentScan.Filter(Configuration.class), @ComponentScan.Filter(Controller.class)})
@ImportResource({"classpath*:componentScan.xml","classpath*:properties-config.xml","classpath*:security-context.xml"})
public class AppConfig
{
...
@Bean
public SessionFactory sessionFactory() throws Exception
{
    AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
    bean.setDataSource(dataSource());
    bean.setPackagesToScan(new String[] {"de.webapp"});
    bean.setHibernateProperties(hibernateProps());
    bean.afterPropertiesSet();
    return bean.getObject();
}

Also interesting fact: if @ComponentScan is included, a breakpoint set in method sessionFactory() was never reached !

I also had the similar issue with spring-social-sample app.

After I converted field level @Inject to constructor level inject it worked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!