【问题标题】:Why I can't inject this bean annoted with @Service into a bean declared into the spring-security.xml configuration file?为什么我不能将这个带有 @Service 注释的 bean 注入到 spring-security.xml 配置文件中声明的 bean 中?
【发布时间】:2015-08-21 08:33:05
【问题描述】:

我是 Spring 的新手,尝试将 bean 注入名为 CustomUserDetailsMapper 的类时遇到了一些问题(我认为这可能取决于范围问题)。

这是我的 CustomUserDetailsMapper 的代码,这个类扩展了 LdapUserDetailsMapper,它在 LDAP 身份验证后自动调用,并由 LDAP 身份验证提供程序用于创建一个 LDAP 用户对象。 (不过不重要,唯一重要的是Spring会自动调用)

package it.myCompany.miur.gestioneUtenze.security;

// IMPORT REMOVED FROM THE CODE SNIPPET

public class CustomUserDetailsMapper extends LdapUserDetailsMapper  {

    private static final Logger _logger = Logger.getLogger(CustomUserDetailsMapper.class.getName());

 @Autowired
    private ProgettoService progettoService;


    @Override
    public UserDetails mapUserFromContext(DirContextOperations dirContextOperations, String userName, Collection<? extends GrantedAuthority> authorities) {
        ...............................................
        ...............................................
        ...............................................
        USE progettoService VARIABLE
        ...............................................
        ...............................................
        ............................................... 
    }

}

正如您在前面的代码中看到的,我试图通过以下方式注入 ProgettoService(这是一个在我的数据库上执行一些查询的服务)对象:

@Autowired
private ProgettoService progettoService;

问题是,当执行 ma​​pUserFromContext() 方法时,该字段为 null,我无法在该方法中使用它。

在我看来这很奇怪,因为我以类似的方式(构造函数注入)将这个 bean 注入到控制器类中,以这种方式(构造函数注入):

package it.myCompany.miur;

@Controller
public class HomeController {

    @Autowired
    public HomeController(ProgettoService progettoService,ScuolaService scuolaService) {
        this.progettoService = progettoService;
        this.scuolaService=scuolaService;
    }

    ...........................................
    ...........................................
    ...........................................
}

在第二种情况下,它工作正常,我可以正确使用我的 ProgettoService 对象。

所以,我的应用程序的配置是由这些 XML 文件完成的:

1) web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:/META-INF/log4j.properties</param-value>
    </context-param>


    <!-- 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>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<!-- 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/spring-security.xml
            /WEB-INF/spring/root-context.xml
        </param-value>
    </context-param>

    <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>   

</web-app>

正如您所见,其中定义了以下其他 XML 配置文件:

2) servlet-context.xml:用于 MVC 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="it.myCompany.miur" />

</beans:beans>

正如您在此文件中看到的,定义了:

<annotation-driven />

启用 Spring MVC @Controller 编程模型,并指定要扫描的包(在其中搜索 @Controller 和派生注释):

<context:component-scan base-package="it.myCompany.miur" />

这个推理正确吗?

3) spring-security.xml:安全配置在哪里定义:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/security 
                        http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <ldap-server id="ldapServer"
        url="${ldap.connection.url}"
        manager-dn="${ldap.connection.admin.user}"
        manager-password="${ldap.connection.admin.password}"/>

    <http auto-config="true"  use-expressions="true" authentication-manager-ref="authenticationManager">
        <intercept-url pattern="/" access="permitAll" />
    <!--<intercept-url pattern="/resetPassword" access="permitAll" />
        <intercept-url pattern="/nuovaPassword" access="permitAll" />
        <intercept-url pattern="resources/css/style.css" access="permitAll"/>
        <intercept-url pattern="resources/img/*" access="permitAll"/>-->
        <intercept-url pattern="/home" access="isAuthenticated()" /> 
        <logout logout-success-url="/" logout-url="/logout" />
        <form-login  login-page="/"  
                     authentication-failure-url="/?error=true"
                    default-target-url="/home"
                    username-parameter="username"
                    password-parameter="password"
                    login-processing-url="/j_spring_security_check"/>
        <csrf disabled="true"/>

    </http> 

    <!-- Authenticator -->
    <beans:bean class="org.springframework.security.ldap.authentication.BindAuthenticator" id="ldapBindAuthenticator">
        <beans:constructor-arg ref="ldapServer"/>
        <beans:property name="userDnPatterns">
            <beans:list><beans:value>cn={0},ou=Users,dc=miur,dc=it</beans:value>
                <beans:value>ou=mailusers,o=mpi.it</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean>

    <beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
        <beans:constructor-arg>
            <beans:list>
                <beans:ref bean="ldapAuthProvider" />
            </beans:list>
        </beans:constructor-arg>
    </beans:bean>

    <beans:bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
        <beans:constructor-arg ref="ldapBindAuthenticator"/>
        <beans:property name="userDetailsContextMapper" ref="customUserContextMapper"/>
    </beans:bean>

    <beans:bean id="customUserContextMapper" class="it.myCompany.miur.gestioneUtenze.security.CustomUserDetailsMapper"/>

</beans:beans>

正如您在此 XML 配置文件中看到的那样,我的 CustomUserDetailsMapper(我无法注入 ProgettoService bean 的 bean)由以下人员定义:

<beans:bean id="customUserContextMapper" class="it.myCompany.miur.gestioneUtenze.security.CustomUserDetailsMapper"/>

3) 最后我有了 root-context.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:ldap="http://www.springframework.org/schema/ldap"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
        http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap-2.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <!-- Root Context: defines shared resources visible to all other web components -->

<!--<context:property-placeholder
    location="file:///${jboss.modules.dir}/system/layers/base/it/myCompany/gestioneUtenze/main/gestioneUtenze.properties classpath*:gestioneUtenze_local.properties" />-->
<context:property-placeholder
    location="classpath*:wifi.properties" />

 <jpa:repositories base-package="it.myCompany.miur.wifipnsd.repository"
    entity-manager-factory-ref="entityManagerFactory"
    transaction-manager-ref="transactionManager"/> 

<bean id="entityManagerFactory" name="wifipnsd"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datasource" /> 
    <property name="persistenceUnitName" value="wifipnsdPU" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >             
            <property name="showSql" value="false" />
            <property name="generateDdl" value="false" />
            <property name="database" value="MYSQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Data Source -->
<jee:jndi-lookup jndi-name="java:jboss/datasources/wifiDS"
    id="datasource" expected-type="javax.sql.DataSource" />


<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="message" />
</bean>

数据源和休眠配置在哪里配置。

那么为什么,使用 @Autowired,我可以将 ProgettoService 对象注入到 HomeController 控制器类中,但我不能这样做ProgettoService 类?我错过了什么?如何解决此问题并将其也注入 ProgettoService 类?

也许问题可能取决于在子上下文(servlet-context)中声明的 bean 可以看到在父上下文中声明的 bean,反之亦然?我不知道如何尝试解决它。你能帮帮我吗?

【问题讨论】:

  • 看起来你错过了
  • @Jens 哪里不见了?我该把它放在哪里?
  • 在 root-context.xml 中并将ProgettoService 实现注释为@Service。
  • @Jens 似乎有效。如果你把它写成答案(如果你能给我更多关于它为什么起作用的信息,请告诉我)我会接受你的答案并给你+1 :-)
  • 我已将我的命令添加为分析器。希望这会有所帮助。

标签: java spring spring-mvc spring-security autowired


【解决方案1】:

您必须将&lt;context:component-scan ...&gt; 添加到root-context.xml 并将ProgettoService 实现注释为@Service

spring 需要&lt;context:component.scan ../&gt; 才能知道必须在哪里寻找注释。 @Service注解是spring管理bean生命周期和bean可以自动装配的必要注解。

更多信息参见@Service的javadoc

【讨论】:

  • 这将在两个上下文中复制类。
【解决方案2】:

您需要了解两种不同的上下文

ContextLoaderListener 创建的根上下文,以及DispatcherServlet 创建的子上下文

由于CustomUserDetailsMapper 没有任何注释,例如:@Service 它不是通过扫描servlet-context.xml 组件扫描中提到的。因此不在调度程序 servlet 的子上下文中。但是对于 Homecontroller,您有 @Controller 以及您的 ProgettoService 在组件扫描包路径中。

CustomUserDetailsMapper 存在于根上下文中,因为 spring-security.xml 中的 bean 定义。这就是最初没有注入 bean 的原因。

我建议从spring-security.xml 中删除bean 定义,并使用@Service 或@Component 对CustomUserDetailsMapper 类进行适当的注释。这将在CustomUserDetailsMapper 位于组件扫描路径中时起作用。

通过实施 Jens 的答案,您基本上在两种情况下都复制了 bean。

【讨论】:

  • 这很糟糕吗?究竟是什么意思我复制了 bean?
  • 您在根应用程序上下文和 Web 应用程序上下文中都有相同的 bean。这不是好的设计,我会避免在两种情况下重叠 bean 定义。
  • 你也可以阅读here
猜你喜欢
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
相关资源
最近更新 更多