我創建一個春季啓動應用程序。我想將我的testEntity保存在數據庫中。我遵循本教程:https://spring.io/guides/gs/accessing-data-jpa/ 應該自動創建一個表來保存實體。如何創建實體並將其保存到數據庫?
然而,當我嘗試運行它作爲春季Booot應用即時得到follwoing錯誤:
Error creating bean with name 'demo' defined in backend.Application: Unsatisfied dependency expressed through constructor argument with index 0 of type [test.EntityRepo]: : No qualifying bean of type [test.EntityRepo] found for dependency: 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 [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
請解釋一下我做錯了什麼,以及如何解決?
您可以在下面找到數據源配置和類。
application.properties:
spring.datasource.url=jdbc:oracle:thin://localhost:1521/orcl
spring.datasource.username=HR
spring.datasource.password=orcl
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
testEntity:
package test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.springframework.data.annotation.Id;
@Entity
public class testEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long ID;
private String name;
public testEntity() {}
public testEntity(long iD) {
ID = iD;
}
public testEntity(String name) {
this.name = name;
}
public long getID() {
return ID;
}
public void setID(long iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
entityRepository:
package test;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EntityRepo extends CrudRepository<testEntity, Long>{
List<testEntity> findByName(String name);
}
JPA配置類:
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
class JpaConfiguration {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("test");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
類主要:
@EnableSwagger2
@SpringBootApplication
@EnableMapRepositories
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
沒有幫助,出現相同的錯誤 –
保持您的演示方法自動裝配,如上所述,並從您的接口。清理你的項目並重新運行。 –
如果不行,你可以在這裏使用我的代碼:(注意:它是一個gradle,但你可以將它轉換爲maven)https://drive.google.com/file/d/0B_EVyl90ivXwbHlXZDNwcGg5eHM/view –