Okay, I know that there are many questions asked around the same topic. But I cant seem to make anything work. It also might be the case that I am yet to completely understa
You need to enable the JPA repositories in your config class, specify the package that contains the repositories as below
@Configuration
@EnableJpaRepositories(basePackages = {
"com.springBoot.usl.repo"
})
public class ApplicationConfig {
}
Example of ApplicationConfig:
@Configuration
@EnableJpaRepositories(basePackages = {"com.springBoot.usl.repo"})
@EnableTransactionManagement
public class ApplicationConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/xxxx");
dataSource.setUsername("xxxx");
dataSource.setPassword("xxxx");
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("xx.xxxx.xxxx.xxxx.domain");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
I think your application is not able to scan the UserRepository
class, If you are using Spring Boot then you should put the main class on top of your package hierarchy.
You should also use @EnableJpaRepositories
and tell it the base package of your repos to enable the repository functionality
@EnableJpaRepositories(basePackages ={"com.springBoot.usl.repo"})
I am able to run your application with some changes on annotation side.
I have used same classes which are given in question. Please see below structure and configuration used. Directory Structure
I have used packages as below and added your classes,
com.rcmutha.usl.controller
com.rcmutha.usl.repository
@SpringBootApplication
@ComponentScan({"com.rcmutha*"})
@EntityScan("com.rcmutha*")
@EnableJpaRepositories("com.rcmutha*")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
For complete code : click here for code
UserRepository
is simply not defined in the current Spring Context.
Make sure that your class is defined in a package that is scanned by Spring. You will need to use @ComponentScan("org.my.pkg")
in your Configuration class to make Spring scan the package.
If you're using Spring Boot, make sure you locate your main application class which is annotated with @SpringBootApplication
in a root package above other classes, as it contains @ComponentScan
.
I know that the problem was solved, but as it can be problem for someone else like I had, I decided to share my problem and the solution. I got exactly the same error message, but the problem was the lack of the annotation @Service in a class and hence an object of this type couldn't autowire. After putting this annotation, everything worked fine.
Below is the object of the mentioned class:
@Autowired
private JwtUserDetailsService jwtUserDetailsService;
Just this annotation caused the same problem and can pass unnoticed:
@Service // annotation that was missing
public class JwtUserDetailsService implements UserDetailsService { ...