Why can't Autowired Repository
hatemismail opened this issue · comments
While trying to configure sub project (By gradle) there are error related of Repository bean factory, below error are appeared:
APPLICATION FAILED TO START
Description:
Field userRepository in com.gs.gsData.controllers.JustTest required a bean of type 'com.gs.gsData.Repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.gs.gsData.Repository.UserRepository' in your configuration.
Below are a case configuration:
Sub project dependencies:
dependencies {
compile project(':shared')
compile('joda-time:joda-time:2.9.4')
compile('com.orientechnologies:spring-boot-orientdb-autoconfigure:0.14rc1')
compile('com.orientechnologies:spring-data-orientdb-commons:0.14rc1')
compile('com.orientechnologies:spring-data-orientdb-object:0.14rc1')
compile('com.orientechnologies:spring-data-orientdb-document:0.14rc1')
compile('org.springframework.boot:spring-boot-starter-social-facebook')
compile('org.springframework.boot:spring-boot-starter-social-twitter')
}
Application:
package com.gs.gsData;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.gs.*"})
public class GsDataApplication {
public static void main(String[] args) {
SpringApplication.run(GsDataApplication.class, args);
}
}
Orient configuration:
package com.gs.gsData.config;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.orient.commons.core.OrientTransactionManager;
import org.springframework.data.orient.object.OrientObjectTemplate;
import org.springframework.data.orient.object.repository.support.OrientObjectRepositoryFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.data.orient.commons.repository.config.EnableOrientRepositories;
import org.springframework.data.orient.object.OrientObjectDatabaseFactory;
import org.springframework.transaction.annotation.Transactional;
@Configuration
@EnableTransactionManagement
@EnableOrientRepositories(basePackages = "com.gs.gsData.domain", repositoryFactoryBeanClass = OrientObjectRepositoryFactoryBean.class)
public class MainDBConfiguration {
@Autowired
private OrientObjectDatabaseFactory factory;
@Bean
public OrientTransactionManager transactionManager() {
return new OrientTransactionManager(factory);
}
@Bean
public OrientObjectTemplate objectTemplate() {
return new OrientObjectTemplate(factory);
}
@PostConstruct
@Transactional
public void registerEntities() {
factory.db().getEntityManager().registerEntityClass(com.gs.gsData.domain.identity.User.class);
}
}
Entity:
package com.gs.gsData.domain.identity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
@Data
@AllArgsConstructor
@JsonIgnoreProperties(value = {"handler"})
public class User{
@Id
private Long id;
@Version
@JsonIgnore
private Long version;
private String detachAll;
private String firstName, lastName, description;
public User(){}
}
Repository:
package com.gs.gsData.Repository;
import com.gs.gsData.domain.identity.User;
import org.springframework.data.orient.object.repository.OrientObjectRepository;
public interface UserRepository extends OrientObjectRepository<User> {
User findByFirstName(String firstName);
User findByLastName(String lastName);
}
Rest service:
package com.gs.gsData.controllers;
import com.gs.gsData.Repository.UserRepository;
import com.gs.gsData.domain.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JustTest {
@Autowired
private UserRepository userRepository;
@RequestMapping("/test-repo")
public String index() {
return userRepository.toString();
}
@RequestMapping("/user-count")
public long count() {
User u = new User();
u.setFirstName("Hatem");
u.setLastName("Ismail");
u.setDescription("just desc");
userRepository.save(u);
return userRepository.count();
}
}
Kindly let me know if there are error at my configuration, or if there are any compatibility issue
Any help is greatly appreciated!
You need to use earlier version of spring-data-commons. Default constructor has been removed from TransactionalRepositoryFactoryBeanSupport, etc, making current version of this lib no longer compatible with latest version of spring data commons.
The following combination works for me:
compile('org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE')
compile('org.springframework.data:spring-data-commons:1.11.2.RELEASE')
I updated the version of spring-data to the latest available. I hope this could help