[英]Spring cannot find bean
I do not know if it will be helpful but I need to tell you the full genesis of my problem - how it started.我不知道它是否会有所帮助,但我需要告诉你我的问题的全部起源——它是如何开始的。
Everything was correct until I started build the security
.一切都是正确的,直到我开始构建
security
。
The first problem I had was with my SecretKey
bean
from @Configuration
class
which Spring
couldn't find.我遇到的第一个问题是
Spring
找不到的@Configuration
class
SecretKey
bean
。 I solved this by creating init
method with @PostConstruct
annotation.我通过使用
@PostConstruct
注释创建init
方法解决了这个问题。
Then the second problem occured which I hadn't had before -然后出现了我以前没有遇到过的第二个问题-
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
I solved this by adding (exclude={DataSourceAutoConfiguration.class})
to @SpringBootApplication
annotation in my main
class
我通过在我的
main
class
添加(exclude={DataSourceAutoConfiguration.class})
到@SpringBootApplication
注释来解决这个问题
And then the third and currently last problem occured which I also hadn't had before.然后出现了第三个也是目前最后一个问题,我以前也没有遇到过。
***************************
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
}
Paths are like that: PersonRepository
is in domain
module
in package: com.app.model.person.repository.PersonRepository
, then PersonEntityDao
is in web
module
in package: com.app.persistence.dao.PersonEntityDao
and PersonRepositoryImpl
is also in web
module
in package: com.app.persistence.repository.PersonRepositoryImpl
.路径是这样的:
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
。 The main class
is in web module in package: com.app.App
main class
位于包中的 web 模块中: com.app.App
I really do not know why there is the problem with this beans because I haven't changed anything in code, just added new directory com.app.security
where I even do not use any of these classes
above.我真的不知道为什么这个 bean 有问题,因为我没有更改任何代码,只是添加了新目录
com.app.security
,我什至不使用上面的任何这些classes
。
This project is multi-module
project where Repository
is in domain
module, then RepositoryImpl
and EntityDao
are in web
module (there is also my main class
)这个项目是
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);
}
}
then I wanted to use this in my TokensService
class like this:然后我想在我的
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();
}
and this error occured: Error creating bean with name 'secretKey'
并发生此错误:
Error creating bean with name 'secretKey'
so I created init
method like this:所以我创建了这样的
init
方法:
@PostConstruct
private void init() {
var context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
secretKey = context.getBean("secretKey", SecretKey.class);
}
and the bean error was solved, but error with DataSource: 'url'
occured which I had not had before并且 bean 错误已解决,但
DataSource: 'url'
发生错误,这是我以前从未有过的
It seems like the entity is not initialize, i suspect that since you excluded the dbautoconfig : (exclude={DataSourceAutoConfiguration.class})实体似乎没有初始化,我怀疑是因为你排除了 dbautoconfig : (exclude={DataSourceAutoConfiguration.class})
Try correcting the url error that you were getting due to this.尝试更正由此导致的 url 错误。
Try doing this in PersonRepositoryImpl尝试在 PersonRepositoryImpl 中执行此操作
private final PersonEntityDao personEntityDao;
@Autowire
public PersonRepositoryImpl(PersonEntityDao personEntityDao){
this.personEntityDao = personEntityDao;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.