我刚开始this教程,工作很好。 (春季靴子)
然后我决定更进一步,实现Observer和Observable。 observableIml有一个List,所有观察者都在监听Observable。
我的目标是我可以存储ObservableImpl。将其列表放在数据库中。
为此我实际上真的复制了&粘贴了教程中的代码。我几乎没有改变任何东西,也没有因为依赖而改变。但突然间Intellij抱怨 UnsatisfiedDependencyException
这是我的代码:
HibernateApplication
@SpringBootApplication
public class HibernateApplication {
private static final Logger log = LoggerFactory.getLogger(HibernateApplication.class);
public static void main(String[] args) {
SpringApplication.run(HibernateApplication.class);
}
@Bean
public CommandLineRunner demo(CustomerRepository repository) {
return (args) -> {
// worked fine, now continue with addObservables implementation
// useCustomerRep(repository);
};
}
@Bean
public CommandLineRunner addObservables(ObservableRepository repository) {
return (args) -> {
useObserver(repository);
// fetch all customers
log.info("Observables found with findAll():");
log.info("-------------------------------");
for (ObservableImpl customer : repository.findAll()) {
log.info(customer.toString());
}
log.info("");
};
}
private void useObserver(ObservableRepository customerRepository) {
ObservableImpl observable = new ObservableImpl("1");
observable.addObserver(new ObserverImpl("firstObserver"));
observable.addObserver(new ObserverImpl("secondObserver"));
observable.addObserver(new ObserverImpl("thirdObserver"));
customerRepository.save(observable);
}
private void useCustomerRep(CustomerRepository repository) {
// save a couple of customers
save(repository);
System.out.println(repository.count());
// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
for (HibernateCustomer customer : repository.findAll()) {
log.info(customer.toString());
}
log.info("");
// fetch an individual customer by ID
HibernateCustomer customer = repository.findOne(1L);
log.info("Customer found with findOne(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");
// fetch customers by last name
log.info("Customer found with findByLastName('Bauer'):");
log.info("--------------------------------------------");
for (HibernateCustomer bauer : repository.findByLastName("Bauer")) {
log.info(bauer.toString());
}
log.info("");
}
private void save(CustomerRepository repository) {
repository.save(new HibernateCustomer("Jack", "Bauer"));
repository.save(new HibernateCustomer("Chloe", "O'Brian"));
repository.save(new HibernateCustomer("Kim", "Bauer"));
repository.save(new HibernateCustomer("David", "Palmer"));
repository.save(new HibernateCustomer("Michelle", "Dessler"));
}
}
ObserverRepository
public interface ObservableRepository extends CrudRepository<ObservableImpl, Long> {
}
ObservableImpl
@Entity
@Data
public class ObservableImpl extends Observable {
private static AtomicInteger idGenerator = new AtomicInteger();
@Id
private final int ID;
private final String name;
@OneToOne
private final List<Observer> observerList = new LinkedList<>();
public ObservableImpl(String name) {
this.name = name;
this.ID = idGenerator.getAndIncrement();
}
public ObservableImpl() {
this.ID = idGenerator.getAndIncrement();
name = "none";
}
@Override
public synchronized void addObserver(Observer o) {
super.addObserver(o);
observerList.add(o);
}
@Override
public synchronized void deleteObserver(Observer o) {
super.deleteObserver(o);
observerList.remove(o);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ObservableImpl that = (ObservableImpl) o;
return ID == that.ID;
}
@Override
public int hashCode() {
return ID;
}
@Override
public String toString() {
return "ObservableImpl{" +
"ID=" + ID +
'}';
}
}
ObserverImpl
@Entity
public class ObserverImpl implements Observer {
private static AtomicInteger idGenerator = new AtomicInteger();
@Id
private final int ID;
private final String name;
public ObserverImpl(String name) {
this.ID = idGenerator.getAndIncrement();
this.name = name;
}
public ObserverImpl() {
this.ID = idGenerator.getAndIncrement();
name = "none";
}
@Override
public String toString() {
return "ObserverImpl{" +
"ID=" + ID +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ObserverImpl observer = (ObserverImpl) o;
return ID == observer.ID;
}
@Override
public int hashCode() {
return ID;
}
@Override
public void update(Observable o, Object arg) {
}
}
application.properties
spring.datasource.url=jdbc:h2:file:~/test1;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.username=admin
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
错误消息
:: Spring Boot :: (v1.5.3.RELEASE)
2017-06-10 14:40:02.052 INFO 22184 --- [ main] c.e.Hibernate.HibernateApplication : Starting HibernateApplication on DESKTOP-JN403B9 with PID 22184 (C:\Dropbox\Projects\h2TestEnv\target\classes started by Gaming in C:\Dropbox\Projects\h2TestEnv)
2017-06-10 14:40:02.053 INFO 22184 --- [ main] c.e.Hibernate.HibernateApplication : No active profile set, falling back to default profiles: default
2017-06-10 14:40:02.076 INFO 22184 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4516af24: startup date [Sat Jun 10 14:40:02 CEST 2017]; root of context hierarchy
2017-06-10 14:40:02.774 INFO 22184 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$cabbaf72] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-10 14:40:02.970 INFO 22184 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-10 14:40:02.976 INFO 22184 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-06-10 14:40:02.977 INFO 22184 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-06-10 14:40:03.031 INFO 22184 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-10 14:40:03.031 INFO 22184 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 956 ms
2017-06-10 14:40:03.117 INFO 22184 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-10 14:40:03.119 INFO 22184 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-10 14:40:03.119 INFO 22184 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-10 14:40:03.119 INFO 22184 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-10 14:40:03.119 INFO 22184 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-10 14:40:03.327 INFO 22184 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-06-10 14:40:03.334 INFO 22184 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-06-10 14:40:03.366 INFO 22184 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-06-10 14:40:03.367 INFO 22184 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-06-10 14:40:03.367 INFO 22184 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-06-10 14:40:03.387 INFO 22184 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-06-10 14:40:03.437 INFO 22184 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-06-10 14:40:03.679 INFO 22184 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2017-06-10 14:40:03.704 INFO 22184 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-10 14:40:03.771 WARN 22184 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addObservables' defined in com.example.Hibernate.HibernateApplication: Unsatisfied dependency expressed through method 'addObservables' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'observerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.Obs.ObservableImpl
2017-06-10 14:40:03.771 INFO 22184 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-06-10 14:40:03.774 INFO 22184 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-06-10 14:40:03.780 INFO 22184 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-10 14:40:03.785 ERROR 22184 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addObservables' defined in com.example.Hibernate.HibernateApplication: Unsatisfied dependency expressed through method 'addObservables' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'observerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.Obs.ObservableImpl
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at com.example.Hibernate.HibernateApplication.main(HibernateApplication.java:27) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'observerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.Obs.ObservableImpl
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.Obs.ObservableImpl
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:153) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:100) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:82) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.3.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.3.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
... 30 common frames omitted
Process finished with exit code 1
你可以向我解释一下我在这里做错了什么以及如何解决这个问题?
非常感谢。
答案 0 :(得分:3)
Spring Boot配置以这种方式工作:
@SpringBootApplication 从当前包开始映射包中的bean和组件。例如:
如果你有包裹:
com.example
com.example.obs
您的@SpringBootApplication位于 com.example 包中,它将映射所有子包。但是,你有这个:
com.example.hibernate
com.example.obs
如果你只是将软件包重命名为com.example,它将会起作用(在这个项目中你会遇到错误的hibernate映射问题,但配置会有效)。
理论上,@ ComponentScan应该解决这些问题,但也许是因为项目的组织和多种配置(HibernateApplication / DemoApplication),它不是。
顺便说一句,当你这样做时,只允许 com.example 包与配置文件。
编辑:但是,如果你想以这种方式工作,你应该这样做:
@SpringBootApplication
@ComponentScan(basePackages="com.example")
@EntityScan(basePackages="com.example")
@EnableJpaRepositories(basePackages="com.example")
public class HibernateApplication { ... }