[英]Spring cannot find bean
我不知道它是否會有所幫助,但我需要告訴你我的問題的全部起源——它是如何開始的。
一切都是正確的,直到我開始構建security
。
我遇到的第一個問題是Spring
找不到的@Configuration
class
SecretKey
bean
。 我通過使用@PostConstruct
注釋創建init
方法解決了這個問題。
然后出現了我以前沒有遇到過的第二個問題-
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
我通過在我的main
class
添加(exclude={DataSourceAutoConfiguration.class})
到@SpringBootApplication
注釋來解決這個問題
然后出現了第三個也是目前最后一個問題,我以前也沒有遇到過。
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.app.persistence.repository.PersonRepositoryImpl required a bean of type 'com.app.persistence.dao.PersonEntityDao' that could not be found.
Action:
Consider defining a bean of type 'com.app.persistence.dao.PersonEntityDao' in your configuration.
public interface PersonRepository extends CrudRepository<Person, Long> {
Optional<Person> findByName(String name);
Optional<Person> findByAddress(Address address);
}
public interface PersonEntityDao extends JpaRepository<PersonEntity, Long> {
Optional<PersonEntity> findByName(String name);
}
@Repository
@RequiredArgsConstructor
public class PersonRepositoryImpl implements Repository {
private final PersonEntityDao personEntityDao;
//methods overriden from Repository
}
路徑是這樣的: PersonRepository
在包中的domain
module
中: com.app.model.person.repository.PersonRepository
,然后PersonEntityDao
在包中的web
module
中: com.app.persistence.dao.PersonEntityDao
和PersonRepositoryImpl
也在web
module
在包中: com.app.persistence.repository.PersonRepositoryImpl
。 main class
位於包中的 web 模塊中: com.app.App
我真的不知道為什么這個 bean 有問題,因為我沒有更改任何代碼,只是添加了新目錄com.app.security
,我什至不使用上面的任何這些classes
。
這個項目是multi-module
項目,其中Repository
在domain
模塊中,然后RepositoryImpl
和EntityDao
在web
模塊中(還有我的main class
)
@Configuration
@ComponentScan("com.app")
public class AppConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
public SecretKey secretKey() {
return Keys.secretKeyFor(SignatureAlgorithm.ES512);
}
}
然后我想在我的TokensService
類中使用它,如下所示:
@Service
@RequiredArgsConstructor
public class TokensService {
private final SecretKey secretKey;
public TokensDto createTokens(Authentication authentication) {
//here I used this secretKey in
//Jwts.builder().[...].signWith(secretKey).build();
}
並發生此錯誤: Error creating bean with name 'secretKey'
所以我創建了這樣的init
方法:
@PostConstruct
private void init() {
var context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
secretKey = context.getBean("secretKey", SecretKey.class);
}
並且 bean 錯誤已解決,但DataSource: 'url'
發生錯誤,這是我以前從未有過的
實體似乎沒有初始化,我懷疑是因為你排除了 dbautoconfig : (exclude={DataSourceAutoConfiguration.class})
嘗試更正由此導致的 url 錯誤。
嘗試在 PersonRepositoryImpl 中執行此操作
private final PersonEntityDao personEntityDao;
@Autowire
public PersonRepositoryImpl(PersonEntityDao personEntityDao){
this.personEntityDao = personEntityDao;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.