Spring mvc 对其他包隐藏一个包

Spring mvc hide one package from others

如图所示,我的ComponentScan设置为整个项目。但是不知何故 'config' 包看不到任何其他包,反之亦然。

如果我 运行 我的项目,我在启动时收到一个错误,没有可用的 'com.rjproject.dao.UserDao' 类型的合格 bean,并且对于所有其他 bean 都是一样的,在这个包中自动装配。但它们在所有其他包中自动装配得很好。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'myUserDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rjproject.service.MyUserDetailsService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

但是这个 bean 存在并且可以在任何其他包中自动装配。

这里是 webSecurityConfig

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MyUserDetailsService myUserDetailsService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/admin/**")
                .access("hasRole('ADMIN')").and().formLogin()
                .loginPage("/login").failureUrl("/login?error")
                .usernameParameter("username")
                .passwordParameter("password")
                .and().logout().logoutSuccessUrl("/login?logout")
                .and().csrf()
                .and().exceptionHandling().accessDeniedPage("/403");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }
}

这里是 MyUserDetailService

@Service
public class MyUserDetailsService implements UserDetailsService {

@Autowired
private UserDao userDao;

@Transactional(readOnly=true)
@Override
public UserDetails loadUserByUsername(final String username)
        throws UsernameNotFoundException {

    com.rjproject.entities.User user = userDao.findByUserName(username);
    List<GrantedAuthority> authorities =
            buildUserAuthority(user.getAuthorities());

    return buildUserForAuthentication(user, authorities);

}

private User buildUserForAuthentication(com.rjproject.entities.User user,
                                        List<GrantedAuthority> authorities) {
    return new User(user.getUsername(), user.getPassword(),
            user.isEnabled(), true, true, true, authorities);
}

private List<GrantedAuthority> buildUserAuthority(Set<Authorities> userRoles) {

    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

    // Build user's authorities
    for (Authorities userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole.getAuthority()));
    }

    List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

    return Result;
}

}

UPD

正如我发现的那样,问题是当我想在配置中自动装配它时 spring bean 还没有创建。但是每个在线示例都说它应该有效。

它没有隐藏。您只需要为所有配置 classes 设置 @ComponentScan 注释。每个配置 class 都应该有自己的 @ComponentScan