简体   繁体   English

Spring找不到bean

[英]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.PersonEntityDaoPersonRepositoryImpl也在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项目,其中Repositorydomain模块中,然后RepositoryImplEntityDaoweb模块中(还有我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM