[英]Issues injecting autowired dependency in Spring Boot
我在下面的代码中收到@Autowired AccountRepository的NoSuchBeanDefinitionException并出现错误消息: No qualifying bean of type [com.brahalla.PhotoAlbum.dao.AccountRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
。 我已将包含存储库的软件包添加到@ComponentScan,但是由于某种原因它仍然没有看到它。 依赖注入在我项目的其他任何地方都有效,只是在这个特定文件中没有。
package com.brahalla.PhotoAlbum.configuration;
import com.brahalla.PhotoAlbum.dao.AccountRepository;
import com.brahalla.PhotoAlbum.domain.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@Configuration
@ComponentScan("com.brahalla.PhotoAlbum.dao")
public class GlobalAuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Autowired
AccountRepository accountRepository;
@Override
public void init(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.userDetailsService(userDetailsService());
}
@Bean
UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = accountRepository.findByUsername(username);
if(account != null) {
return new User(
account.getUsername(),
account.getPassword(),
true, true, true, true,
AuthorityUtils.createAuthorityList("USER")
);
} else {
throw new UsernameNotFoundException("could not find the user '" + username + "'");
}
}
};
}
}
这是存储库:
package com.brahalla.PhotoAlbum.dao;
import com.brahalla.PhotoAlbum.domain.entity.Account;
import org.springframework.data.repository.CrudRepository;
public interface AccountRepository extends CrudRepository<Account, Long> {
public Account findByUsername(String username);
}
和主要的应用程序配置:
package com.brahalla.PhotoAlbum.configuration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationConfiguration {
public static void main(String[] args) {
SpringApplication.run(ApplicationConfiguration.class, args);
}
}
这是完整的日志和堆栈跟踪: http : //pastebin.com/PKvd8rXV
编辑 这是git存储库 ,有问题的代码在分支develop
。
带有@ controller,@ service,@ component,@ Repository等批注的类将是自动布线的候选类,而其他类则不是。因此请考虑相应地对您的类进行批注,以进行正确的自动布线。
@ Component->任何Spring管理的组件的通用构造型
@Repository- >持久层的构造型
@Service- >服务层的构造型
@Controller- >表示层的构造型(spring-mvc)
下面的代码应该工作
@Repository
public interface AccountRepository extends CrudRepository<Account, Long> {
public Account findByUsername(String username);
}
我找到了解决问题的方法,我只是想确保将其发布在这里。
首先,我将所有Web配置选项移到了一个扩展WebSecurityConfigurerAdapter的类中。 其次,我必须将AuthenticationManagerBuilder初始化的注释更改为@Autowired而不是@Override。 第三,我必须将UserDetailsService bean公开:
package com.brahalla.PhotoAlbum.configuration;
import com.brahalla.PhotoAlbum.dao.AccountRepository;
import com.brahalla.PhotoAlbum.domain.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
AccountRepository accountRepository;
@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.userDetailsService(userDetailsService());
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.anyRequest().fullyAuthenticated()
//.and().authorizeUrls()
/*.and().formLogin()
.loginPage("/login")
.permitAll()
.and().logout()
.permitAll()*/
.and().httpBasic()
.and().csrf()
.disable();
}
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = accountRepository.findByUsername(username);
if(account != null) {
return new User(
account.getUsername(),
account.getPassword(),
true, true, true, true,
AuthorityUtils.createAuthorityList("USER")
);
} else {
throw new UsernameNotFoundException("could not find the user '" + username + "'");
}
}
};
}
}
我认为您在ApplicationConfiguration
类中缺少@EnableJpaRepositories
。
您可以发布应用程序的调试日志吗? 它将给出是否创建该bean的想法。 尝试将componentscan放置在applicationconfiguration类中,并使用@component或相关注释标记GlobalAuthenticationConfiguration
您可以尝试以下代码段:
package com.brahalla.PhotoAlbum.dao;
import com.brahalla.PhotoAlbum.domain.entity.Account;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.CrudRepository;
@Repository
public interface AccountRepository extends CrudRepository<Account, Long> {
public Account findByUsername(String username);
}
另外,用@EnableJpaRepositories
注释您的ApplicationConfiguration
1)您能否尝试将您的类ApplicationConfiguration
类放在根包com.brahalla.PhotoAlbum
而不是com.brahalla.PhotoAlbum.configuration
?
实际上, @SpringBootApplication
扫描它所在位置的子包。
2)由于对方说,把@EnableJpaRepositories
在你的类GlobalAuthenticationConfiguration
使用这两种配置,它应该可以工作
我遇到了同样的问题,这个问题正是由于Spring Boot ApplicationConfiguration.java是在特定包中而不是在根目录中创建的。 我将其移至根目录。
即最初是在com.myproject.root.config中,我将其移至com.myproject.root
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.