ちょっと私はSpring data
からリポジトリを拡張することに問題があります。依存関係のためのタイプUserRepositoryの修飾Beanが見つかりません:この依存関係のautowire候補となる少なくとも1つのbeanが必要です
私はサービス層と話すコントローラがあります:リポジトリ/ DAO層へ
@Service
public class UserService {
@Autowired
public UserRepository repository;
@Transactional
public Iterable<UserEntity> getList() {
return repository.findAll();
}
}
サービス層の会談:ここ
@RestController
public class UserController {
@Autowired
public UserService userService;
@RequestMapping(value = ServerRouting.UserService.getList, method = RequestMethod.GET)
public @ResponseBody Iterable<UserEntity> getList() {
return userService.getList();
}
}
は、サービス層です。リポジトリは、春データからorg.springframework.data.repository;CrudRepository
を拡張するインタフェースである:このCRUDリポジトリに
@Repository
public interface UserRepository extends CrudRepository<UserEntity, Long> {}
は、私が使用したい方法、FE findAll()
あるしかし、私はTomcat上でこのプロジェクトを実行すると、私はエラーを取得しています:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public pl.korbeldaniel.cms.server.service.UserService pl.korbeldaniel.cms.server.controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public pl.korbeldaniel.cms.server.dao.UserRepository pl.korbeldaniel.cms.server.service.UserService.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.korbeldaniel.cms.server.dao.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
私は問題がこれと関連していると思う:No qualifying bean of type [pl.korbeldaniel.cms.server.dao.UserRepository] found for dependency
。 原因私は注入することができ、リポジトリの実装を持っていないが、私にとって、それは春・データを使用してのポイントです:
のように、単純なインターフェイスを作成することが私の永続性構成であります
package pl.korbeldaniel.cms.server.config;
import java.util.Properties;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableJpaRepositories(basePackages = { "pl.korbeldaniel.cms.server;" })
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
class PersistenceContext {
@Resource
private Environment env;
@Bean(destroyMethod = "close")
DataSource dataSource(Environment env) {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
return new HikariDataSource(dataSourceConfig);
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("pl.korbeldaniel.cms.server");
Properties jpaProperties = new Properties();
//Configures the used database dialect. This allows Hibernate to create SQL
//that is optimized for the used database.
jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
//Specifies the action that is invoked to the database when the Hibernate
//SessionFactory is created or closed.
jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
//Configures the naming strategy that is used when Hibernate creates
//new database objects and schema elements
jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
//If the value of this property is true, Hibernate writes all SQL
//statements to the console.
jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
//If the value of this property is true, Hibernate will format the SQL
//that is written to the console.
jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
助けてください。
私はこれが問題だとは思っていませんが、@ Repositoryはバネデータインターフェースリポジトリには使用されていません。実際には、インターフェースがBeanとして使用されないようにするために '@ NoRepositoryBean'があります。 –