why does annotate class as @Service do not create bean?

前端 未结 4 1929
予麋鹿
予麋鹿 2021-01-25 04:19

I has class like this:

@Service(\"userDetailsService\") 
public class MyUserDetailsService implements UserDetailsService {
    ...

and trying t

相关标签:
4条回答
  • 2021-01-25 04:51

    just import the other xml file in the spring-security.xml by using <beans:import resource="" />

    another thing u can do is that load all the xml files in the web.xml file using

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    path to the xml files separated by commas
    </param-value>
    </context-param>
    
    0 讨论(0)
  • 2021-01-25 04:57

    If you use annotations to specify your beans, you need to have add an entry to your config to scan the classpath for them.

    <context:component-scan base-package="org.example"/>
    
    0 讨论(0)
  • 2021-01-25 05:03

    You are missing schema location for context.

    So your xml should start with:

    <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"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/jdbc
               http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
               http://www.springframework.org/schema/security
               http://www.springframework.org/schema/security/spring-security-3.0.xsd
               http://www.springframework.org/schema/context 
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    0 讨论(0)
  • 2021-01-25 05:05

    @Service extends @Component which allows for classpath scanning.

    You can enable both classpath scanning and annotations

    <context:annotation-config />
    <context:component-scan base-package="com.package.a,com.b" />
    

    I don't know what version are you using. Try this.

    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService">
            <!-- <password-encoder hash="md5" /> -->
        </authentication-provider>
    </authentication-manager>
    

    Unless you provide, as you do, a name, it will be the class name. But you provide the same name it would be but stating another in the config file.

    If you @Service with no name then it would be fine.

    0 讨论(0)
提交回复
热议问题