2016-07-08 35 views
-2

我正在学习Spring JPA。 我在访问存储库vehicleRepository时遇到service - vehicleService类中的错误。请找到下面的代码片段。在JPA资源库中找不到依赖的类型的合格bean

Vehicle.java - 实体类属性定义

@Entity 
@Table(name = "tkz_vehicle") 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
@Document(indexName = "vehicle") 
public class Vehicle extends AbstractAuditingEntity implements Serializable{ 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @NotNull 
    @Column(name="company", length =50) 

    private String company; 

    @NotNull 
    @Column(name="vehicle_code",length = 50) 
    private String vehicleCode; 

    @NotNull 
    @Column(name="regn_no", length = 50) 
    private String regn_no; 


    @Column(name="vehicle_name", length = 50) 
    private String vehicleName; 

    @NotNull 
    @Column(name="vehicle_type", length = 50) 
    private String vehicleType; 

    @Column(name = "manufacturer", length = 60) 
    private String manufacturer; 

    @Column(name = "manufacture_year") 
    private int manufactureYear; 

    @Column(name="phone", length=50) 
    private String phone; 

    @Column(name="email", length = 100) 
    private String email; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getCompany() { 
     return company; 
    } 

    public void setCompany(String company) { 
     this.company = company; 
    } 

    public String getVehicleCode() { 
     return vehicleCode; 
    } 

    public void setVehicleCode(String vehicleCode) { 
     this.vehicleCode = vehicleCode; 
    } 

    public String getRegn_no() { 
     return regn_no; 
    } 

    public void setRegn_no(String regn_no) { 
     this.regn_no = regn_no; 
    } 

    public String getVehicleName() { 
     return vehicleName; 
    } 

    public void setVehicleName(String vehicleName) { 
     this.vehicleName = vehicleName; 
    } 

    public String getVehicleType() { 
     return vehicleType; 
    } 

    public void setVehicleType(String vehicleType) { 
     this.vehicleType = vehicleType; 
    } 

    public String getManufacturer() { 
     return manufacturer; 
    } 

    public void setManufacturer(String manufacturer) { 
     this.manufacturer = manufacturer; 
    } 

    public int getManufactureYear() { 
     return manufactureYear; 
    } 

    public void setManufactureYear(int manufactureYear) { 
     this.manufactureYear = manufactureYear; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 


} 

VehicleService.java - 以私有财产为vehicleRepository服务类。

@Service 
@Transactional 
public class VehicleService { 

    private final Logger log = LoggerFactory.getLogger(VehicleService.class); 

    @Inject 
    private VehicleRepository vehicleRepository; 

    public Vehicle createVehicle(VehicleDTO vehicleDTO){ 

     Vehicle vehicle = new Vehicle(); 
     vehicle.setVehicleCode(vehicleDTO.getvehicleCode()); 
     vehicle.setVehicleName(vehicleDTO.getvehicleName()); 
     vehicle.setVehicleType(vehicleDTO.getvehicleType()); 
     vehicle.setManufacturer(vehicleDTO.getManufacturer()); 
     vehicle.setManufactureYear(vehicleDTO.getManufactureYear()); 
     vehicle.setRegn_no(vehicleDTO.getRegn_no()); 
     vehicle.setCompany(vehicleDTO.getCompany()); 
     vehicle.setEmail(vehicleDTO.getEmail()); 
     vehicle.setPhone(vehicleDTO.getPhone()); 

     vehicleRepository.save(vehicle); 


     return vehicle; 


    } 

} 

VehicleRepository

@Repository 
public interface VehicleRepository extends CrudRepository<Vehicle,Long> { 


    Optional<Vehicle> findOneById(String vehicleId); 

} 

最后的春天引导配置类 - Application.java

@Configuration 
@ComponentScan 
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class }) 
@EnableConfigurationProperties({ JHipsterProperties.class, LiquibaseProperties.class }) 
@EnableJpaRepositories(basePackages = "trackz", considerNestedRepositories = true) 
public class Application { 

    private static final Logger log = LoggerFactory.getLogger(Application.class); 

    @Inject 
    private Environment env; 

    /** 
    * Initializes trackz. 
    * <p/> 
    * Spring profiles can be configured with a program arguments --spring.profiles.active=your-active-profile 
    * <p/> 
    * <p> 
    * You can find more information on how profiles work with JHipster on <a href="http://jhipster.github.io/profiles.html">http://jhipster.github.io/profiles.html</a>. 
    * </p> 
    */ 
    @PostConstruct 
    public void initApplication() throws IOException { 
     if (env.getActiveProfiles().length == 0) { 
      log.warn("No Spring profile configured, running with default configuration"); 
     } else { 
      log.info("Running with Spring profile(s) : {}", Arrays.toString(env.getActiveProfiles())); 
      Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles()); 
      if (activeProfiles.contains(Constants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(Constants.SPRING_PROFILE_PRODUCTION)) { 
       log.error("You have misconfigured your application! " + 
        "It should not run with both the 'dev' and 'prod' profiles at the same time."); 
      } 
      if (activeProfiles.contains(Constants.SPRING_PROFILE_PRODUCTION) && activeProfiles.contains(Constants.SPRING_PROFILE_FAST)) { 
       log.error("You have misconfigured your application! " + 
        "It should not run with both the 'prod' and 'fast' profiles at the same time."); 
      } 
      if (activeProfiles.contains(Constants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(Constants.SPRING_PROFILE_CLOUD)) { 
       log.error("You have misconfigured your application! " + 
        "It should not run with both the 'dev' and 'cloud' profiles at the same time."); 
      } 
     } 
    } 

    /** 
    * Main method, used to run the application. 
    */ 
    public static void main(String[] args) throws UnknownHostException { 
     SpringApplication app = new SpringApplication(Application.class); 
     SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args); 
     addDefaultProfile(app, source); 
     Environment env = app.run(args).getEnvironment(); 
     log.info("Access URLs:\n----------------------------------------------------------\n\t" + 
       "Local: \t\thttp://127.0.0.1:{}\n\t" + 
       "External: \thttp://{}:{}\n----------------------------------------------------------", 
      env.getProperty("server.port"), 
      InetAddress.getLocalHost().getHostAddress(), 
      env.getProperty("server.port")); 

    } 

    /** 
    * If no profile has been configured, set by default the "dev" profile. 
    */ 
    private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) { 
     if (!source.containsProperty("spring.profiles.active") && 
       !System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) { 

      app.setAdditionalProfiles(Constants.SPRING_PROFILE_DEVELOPMENT); 
     } 
    } 
} 

现在我得到如下错误。

java.lang.UnsupportedOperationException:空 在com.zaxxer.hikari.util.FastList.iterator(FastList.java:209)〜[HikariCP-2.4.1.jar:NA] 在org.apache.catalina (WebappClassLoaderBase.java:2195)[tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks(WebappClassLoaderBase.java:2105).loader.WebappClassLoaderBase.loadedByThisOrChild(WebappClassLoaderBase.java:2195) [tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalsForLeaks(WebappClassLoaderBase.java:2060)[tomcat-embed-core-8.0.30.jar: 8.0.30] at org.apache.catalina.loader.WebappClassLoaderBase.clearReferences(WebappClassLoaderBase.java:1575)[tomcat-embed-core-8.0.30.jar:8.0.3 0] at org.apache.catalina.loader.WebappClassLoaderBase.stop(WebappClassLoaderBase.java:1521)[tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.loader.WebappLoader .stopInternal(WebappLoader.java:447)[tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)[tomcat-embed -core-8.0.30.jar:8.0.30] at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5517)[tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)[tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.core.ContainerBase $ StopChild .call(ContainerBase.java:1424)[tomcat-embed-core-8.0.30.jar:8.0.30] at org.apache.catalina.core.ContainerBase $ StopChild.call(Cont ainerBase.java:1413)[tomcat-embed-core-8.0.30.jar:8.0.30] at java.util.concurrent.FutureTask.run(FutureTask.java:266)[na:1.8.0_66] at (ThreadPoolExecutor.java:617)[na:1.8.0_66] at java.lang.Thread.run(Thread.java:745)[na:1.8.0_66]

2016-07-08 12:44:36.412错误14020 --- [restartedMain] osboot.SpringApplication:应用程序启动失败

org.springframework.beans.factory.BeanCreationException:创建名为'vehicleService'的bean时出错:注入autowire d依赖关系失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private com.a2b.trackz.admin.asset_mgmnt.vehicle.repository.VehicleRepository com.a2b.trackz.admin。asset_mgmnt.vehicle.service.VehicleService.vehicleRepository;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合要求的[com.a2b.trackz.admin.asset_mgmnt.vehicle.repository.VehicleRepository]类型的合格bean:期望至少1个符合自动装配候选人的bean这种依赖性。依赖注释:{@ javax.inject.Inject()} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)〜[spring-beans-4.2.4.RELEASE.jar:4.2 .4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org .springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support .AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework。 beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry .getSingleton(DefaultSingletonBeanRegistry.java:230)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302 )〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)〜[spring-beans-4.2 .4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)〜[spring-beans-4.2.4.RELEASE.jar:4.2 .4.RELEASE] at org.springframework.context.support.Abst ractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)〜[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 〜[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)〜[spring-boot-1.3。 1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764)〜[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357)〜[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.run( SpringApplication.java:305)〜[spring-boot-1.3.1.RELEASE。 jar:1.3.1.RELEASE] at com.a2b.trackz.Application.main(Application.java:77)[classes /:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8 (DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_66] .0_66] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_66] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_66 ] at java.lang.reflect.Method.invoke(Method.java:497)〜[na:1.8.0_66] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)[ spring-boot-devtools-1.3.1.RELEASE.jar:1.3.1.RELEASE] 原因:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private com.a2b.trackz.admin.asset_mgmnt。 vehicle.repository.VehicleRepository com.a2b.trackz.admi n.asset_mgmnt.vehicle.service.VehicleService.vehicleRepository;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合要求的[com.a2b.trackz.admin.asset_mgmnt.vehicle.repository.VehicleRepository]类型的合格bean:期望至少1个符合自动装配候选人的bean这种依赖性。依赖注释:{@ javax.inject.Inject()} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)〜[spring-beans-4.2.4.RELEASE.jar :4.2.4。发布] at org.springframework。org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 。 beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... 20个常见框架遗漏 引起:org。 springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合条件的[com.a2b.trackz.admin.asset_mgmnt.vehicle.repository.VehicleRepository]类型的符合条件bean:期望至少1个符合此依赖关系自动装配候选资格的bean。依赖注释:{@ javax.inject.Inject()} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)〜[spring-beans-4.2.4.RELEASE.jar:4.2 .4.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org .springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.annotation .AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... 22常见帧被省略

2016-07-08 12:44:36.428 WARN 14020 --- [restartedMain] osboot.SpringApplication:错误处理失败(在类路径资源[org/springframework/security中定义了名为'delegatingApplicationListener'的bean时创建错误/config/annotation/web/configuration/WebSecurityConfiguration.class]:Bean实例化之前的BeanPostProcessor失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.cache.annotation.ProxyCachingConfiguration'的bean时出错:Bean初始化失败;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry'的bean)

请关于解决方案的建议。

+2

由于'基地= -package'在'@ EnableJpaRepositories'是错误的。 –

回答

0
@EnableJpaRepositories(basePackages = "trackz", considerNestedRepositories = true) 

我想你需要在这里有全包的名称,以便尽量

@EnableJpaRepositories(basePackages = "com.a2b.trackz", considerNestedRepositories = true) 
+0

现在我得到另一个说,没有财产getOne类型车找到!请让我知道你的想法。 –

+0

我想你应该使用findOne(),而不是根据http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-concepts – Stan

相关问题