2014-09-23 9 views
7

ich das Beispiel Frühlings-Boot und Spring-Daten für eine Übung mit, kann aber nicht die Anwendung spielen, der folgende Fehler auftritt:Fehler bei @EnableAutoConfiguration und @@ ComponentScan mit Spring-Boot + Spring Data

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cepRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class br.com.boot.domain.Cep 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1554) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:687) 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109) 
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) 
     at br.com.boot.application.Application.main(Application.java:20) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) 
     at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.IllegalArgumentException: Not an managed type: class br.com.boot.domain.Cep 
     at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219) 
     at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68) 
     at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:88) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:68) 
     at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) 
     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224) 
     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550) 
     ... 21 common frames omitted 

java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) 
     at java.lang.Thread.run(Unknown Source) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cepRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class br.com.boot.domain.Cep 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1554) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:687) 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109) 
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) 
     at br.com.boot.application.Application.main(Application.java:20) 
     ... 6 more 
Caused by: java.lang.IllegalArgumentException: Not an managed type: class br.com.boot.domain.Cep 
     at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219) 
     at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68) 
     at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:88) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:68) 
     at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) 
     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224) 
     at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210) 
     at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550) 
     ... 21 more 

Meine Klasse Cep

@Entity 
public class Cep { 

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

    @Column 
    private String cep; 

    public Cep(String cep) { 
     this.cep = cep; 
     validarCep(cep); 
    } 
    protected Cep(){ 

    } 

meine Klasse CepRepository

public interface CepRepository extends CrudRepository<Cep, Long>{ 

    List<Cep> findbyId(Long id); 
} 

Mein Cl CepController

@RestController 
@RequestMapping("/cep") 
public class CepController { 

    @Autowired 
    private CepRepository repository; 

    @RequestMapping("/lista") 
    public String retornaListaCeps(){ 
     Iterable<Cep> lista = repository.findAll(); 
     return lista.toString(); 
    } 

} 

meine Klasse FactoryData ass

@Service 
public class FabricaDados { 

    @Autowired 
    private CepRepository repository; 

    public void gravarCeps() { 
     repository.save(new Cep("14233-333")); 
     repository.save(new Cep("14530-000")); 
     repository.save(new Cep("14020-060")); 
    } 

    public Iterable<Cep> listarTodos() { 
     Iterable<Cep> ceps = repository.findAll(); 
     return ceps; 
    } 

und meine Klasse Haupt

@Configuration 
@EnableAutoConfiguration 
@ComponentScan(basePackages="br.com.boot") 
@EnableJpaRepositories(basePackages="br.com.boot.repository") 
public class Application { 

    public static void main(String[] args) { 

     ApplicationContext ctx = SpringApplication.run(Application.class, args); 
     FabricaDados fabrica = ctx.getBean(FabricaDados.class); 

     fabrica.gravarCeps(); 
     System.out.println(fabrica.listarTodos()); 

     System.out.println("Let's inspect the beans provided by Spring Boot:"); 

     String[] beanNames = ctx.getBeanDefinitionNames(); 
     Arrays.sort(beanNames); 
     for (String beanName : beanNames) { 
      System.out.println(beanName); 
     } 
    } 

} 

Dank.

Antwort

Verwandte Themen