Issue
It seems like i have disable jpa repositories. Have this error:
SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [C:\Users\jasiu\workspace2.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BuyMyTime\WEB-INF\classes\web\UserController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [data.UserRepository] found for dependency [data.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [data.UserRepository] found for dependency [data.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
Config:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"data"})
@ComponentScan(basePackages={"data"},
excludeFilters={
@Filter(type=FilterType.ANNOTATION, value = EnableWebMvc.class)
})
public class RootConfig {
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf, DataSource dataSource){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
transactionManager.setDataSource(dataSource);
return transactionManager;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/HelloWorld");
dataSource.setUsername("login");
dataSource.setPassword("haslo");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("data");
em.setJpaVendorAdapter(jpaVendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
Properties additionalProperties() {
Properties properties = new Properties();
//properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter(){
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
//adapter.setDatabase();
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
adapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
return adapter;
}
}
Repo is in package "data":
@Repository
@Transactional
public interface UserRepository extends JpaRepository<User, Long>{
User findByNick(String nick);
}
Controller:
@Controller
public class UserController {
@Autowired
protected UserRepository userRepository;
...
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.app</groupId>
<artifactId>BuyMyTime</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>BuyMyTime Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.0.ALPHA03</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>BuyMyTime</finalName>
</build>
</project>
Please for any help.
Solution
What is your project structure? I copied your code on a project with UserRepository in basepackage.data and the repository was discovered.
I think it is because you have config and data children under default package. I got it to work by moving your data, web and config packages under a common package.
Here are my configuration annotations
package basepackage.config;
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages={"basepackage.data"})
@EntityScan(basePackageClasses=User.class)
@ComponentScan(basePackages={"basepackage"})
public class MyConfig {
@Autowired
private UserRepository repo;
@PostConstruct
public void init(){
for(int i=0; i<10; i++){
repo.save(new User("u-"+i));
}
}
}
You can also define your scanning by using basePackageClasses.
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackageClasses={UserRepository.class})
@EntityScan(basePackageClasses=User.class)
@ComponentScan(basePackages={"basepackage"})
Hope that helps.
Answered By - Francois Mottard