Spring MVC,Spring Security和Hibernate无法自动关联上下文之间的属性
我正在使用Spring MVC 3.0.6和Spring Security 3.0.7.在安全上下文中,无法将RoleDao类自动关联到我的用户类.
I am using Spring MVC 3.0.6 and Spring security 3.0.7. I cannot @Autowire the RoleDao class to my user class when in the security context.
我的web.xml文件:
my web.xml file:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/security-app-context.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
security-app-context.xml:
The security-app-context.xml:
<beans:bean id="chineseCheckersEntryPoint" class="com.nike.golf.security.ChineseCheckersAuthenticationEntryPoint" />
<beans:bean id="chineseCheckersFilter" class="com.nike.golf.security.ChineseCheckersAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager"/>
</beans:bean>
<http use-expressions="true" auto-config="false" entry-point-ref="chineseCheckersEntryPoint">
<intercept-url pattern="/secure/extreme/**" access="hasRole('supervisor')" />
<intercept-url pattern="/user/**" access="permitAll" />
<intercept-url pattern="/profile/**" access="isAuthenticated()" />
<intercept-url pattern="/secure/**" access="isAuthenticated()" />
<custom-filter position="PRE_AUTH_FILTER" ref="chineseCheckersFilter" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider ref="chineseCheckersAuthenticationProvider" />
</authentication-manager>
<beans:bean id="chineseCheckersAuthenticationProvider" class="com.nike.golf.security.ChineseCheckersAuthenticationProvider" />
在我的使用roleDao的用户对象中,它为null.尚未自动接线.从我在线上进行的所有研究来看,它似乎与不同的上下文有关,并且无法自动在它们之间进行布线.
In my user object where it uses the roleDao, it's null. It has not been autowired. From all the research I have done online it seems to be related to the different contexts, and not being able to autowire between them.
有人可以帮助我理解这些上下文以及如何使它们进入相同的上下文吗?
Can someone help me understand these contexts and how I can get them into the same context?
感谢大家的帮助.我设法弄清楚了.
Thank you everyone for your help. I managed to figure this out.
这个问题与我的相似,这使我朝着正确的方向前进: 在父上下文与子上下文中声明Spring Bean
This question was similar to mine and got me moving in the right direction: Declaring Spring Bean in Parent Context vs Child Context
此论坛帖子确实简化了我的想法.
This forum post really simplified the idea for me.
在web.xml文件中,定义servlet上下文和应用程序上下文. 通过以下几段XML配置应用程序上下文:
In your web.xml file you define the servlet context and the application context. The application context is configured through these pieces of XML:
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/security-app-context.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
您传递到context-param> contextConfigLocation中的
任意数量的* context.xml文件都位于应用程序上下文中.这是父上下文.
any number of *context.xml files you pass into the context-param > contextConfigLocation are in the application context. This is the parent context.
通过以下xml在web.xml文件中创建servlet上下文:
The servlet context gets created in the web.xml file by this bit of xml:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
作为子上下文的servlet上下文可以访问应用程序上下文(父).
The servlet context as the child context has access to the application context (parent).
这是我要理解的关键.因此,我将在servlet上下文中拥有的所有配置都移到了应用程序上下文中.
This was the key thing for me to understand. So I moved all configuration I had in the servlet context, up to the application context.
就像我上面链接的另一个问题的答案一样,@ Autowired仍然无法正常工作.有人知道原因吗? 因此,为了解决这个问题,我从我关心的属性一直到sessionFactory一直定义Bean和xml中的属性.
Like the answer in the other question I linked to above says @Autowired still does not work. Anyone know the reason for that? So to get around this I defined the beans and the properties in the xml all the way from the property I was concerned with down to the sessionFactory.
现在的事情是,我可以将xml中所需的bean一直连接到sessionFactory,因为它是在同一上下文中,因为我将它从以前的servlet上下文移到了应用程序上下文中.
The thing is now I could wire up the beans I needed in xml all the way up the hierarchy to sessionFactory because it was in the same context, since I moved it up to the application context from the servlet context where it was before.
在我的问题中,我什至没有发布servlet-context.xml文件,因为我认为不需要触摸它,但是实际上,如果我想连接配置,则需要将配置移到应用程序上下文中.事情取决于我的安全性.
In my question I didn't even post the servlet-context.xml file because I didn't think it needed to be touched, but in fact I needed to move the configuration up to the app context if I wanted to wire things up to my security beans.
我希望这是有道理的.