user15076617
user15076617

Reputation:

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.

The first problem I had was with my SecretKey bean from @Configuration class which Spring couldn't find. I solved this by creating init method with @PostConstruct annotation.

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

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. The main class is in web module in package: 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.

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)

@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:

@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'

so I created init method like this:

 @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

Upvotes: 1

Views: 2711

Answers (2)

manish
manish

Reputation: 161

Try doing this in PersonRepositoryImpl

private final PersonEntityDao personEntityDao;

@Autowire
public PersonRepositoryImpl(PersonEntityDao personEntityDao){
 this.personEntityDao = personEntityDao;
}

Upvotes: 1

mohneesh_d
mohneesh_d

Reputation: 45

It seems like the entity is not initialize, i suspect that since you excluded the dbautoconfig : (exclude={DataSourceAutoConfiguration.class})

Try correcting the url error that you were getting due to this.

Upvotes: 0

Related Questions