我的Spring(4.3.9-RELEASE)配置有问题。@EnableTransactionManagement在RootConfig中不起作用
我有一个WebMVC应用程序正在运行,并且当前配置一切正常。
我会省略很多,由于并发症的整体复杂性,我认为这并不重要。请让我知道你是否错过了重要的事情。
@WebServlet
@PropertySource("classpath:myAppConf.properties")
public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {MainAppConfig.class } ;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected String getServletName() {
return "myServletFooBar";
}
}
MainAppConfig:
@Configuration
@Import({HibernateConfig.class})
@EnableGlobalMethodSecurity(prePostEnabled=true)
@ComponentScan(basePackages = { "my.app.security"})
public class MainAppConfig {
...
}
WebAppConfig:
@EnableWebMvc()
@Configuration()
@ComponentScan(basePackages = { "my.app.controller" })
@Import({ServiceConfig.class})
@EnableTransactionManagement
public class WebAppConfig extends WebMvcConfigurerAdapter{
...
}
我有我的所有控制器和服务中(标有@Transactional
)我WebAppConfiguration
(其注有@EnableTransactionManagement
)。我的Hibernate配置(事务管理器,数据源等)在我的MainAppConfiguration
中。
交易对这种配置工作正常。
现在我需要有交易,我MainAppConfiguration
-Context(用于定制Spring的安全的AuthenticationProvider(从DaoAuthenticationProvider
延伸)。
这不起作用,因为@EnableTransactionManagement
是我WebAppConfig
。当试图运行一个交易从我的自定义身份验证提供我碰到下面的错误。
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
所以,我想我所有的服务搬到我MainAppConfig
(也@EnableTransactionManagement
注释),但后来我得到我Controlloe相同的消息r调用服务的方法。
我也尝试将我的MainAppConfig中的@EnableGlobalMethodSecurity(prePostEnabled=true)
和@ComponentScan(basePackages = { "my.app.security"})
注释移动到我的WebAppConfig(这在我看来更有意义)。但后来Spring-Security抱怨说有No bean named 'springSecurityFilterChain' available
我的配置中存在哪些缺陷?
缺陷是您的配置被传播并且由于组件扫描而导致事情被加载两次。 –
我看不到任何东西在这里加载两次(你能指出我在哪里,你认为什么是加载两次?) 我也不高兴有RootConfig和WebAppConfig传播,但将所有内容放入WebAppConfig根本不起作用看到我上面描述的'SpringSecurityFilterChain'错误)。 – Tarator
你是组件扫描的东西(不确定'ServiceConfig'在哪个软件包以及该扫描什么软件)......另外'ServletInitializer'上的@ webServlet和@ProfileSource没有任何意义。 –