- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将 Spring Data Neo4J 存储库 @Autowire 到 Spring MVC 转换器(在 Spring Boot 中),但 MVC 配置在数据服务启动之前启动。这会导致@Autowired not found 问题。 如何让数据服务在 MVC 之前启动,以便它找到符合条件的 bean?
我有一个项目正在从 XML Spring 配置转换为 Spring Boot。除 MVC 转换器外,一切正常。他们无法@Autowire Neo4J Repository 类。
如果删除存储库的@Autowire 和值中的硬代码,则黑客攻击会按预期工作。服务中的其他操作使用存储库就好了。似乎 MVC 配置在 Neo4J 管道开始之前就开始了,然后找不到合适的组件来连接。我看过,但我不知道如何在 MVC 配置之前启动数据配置。
这是我的基本配置:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Toyfiles {
public static void main(String[] args) throws Exception {
SpringApplication.run(Toyfiles.class, args);
}
}
@Configuration
public class MVCBeans extends WebMvcConfigurerAdapter {
@Autowired
private StringToBrand stringToBrand;
@Autowired
private BrandToString brandToString;
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(stringToBrand);
registry.addConverter(brandToString);
super.addFormatters(registry);
}
@Component
public class StringToBrand implements Converter<String, Brand> {
@Autowired
BrandRepository brandRepository;
@Override
public Brand convert(String s) {
return brandRepository.findBrandByName(s);
}
}
@Configuration
@Profile("localEmbeddedDBServer")
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = "com.toyfiles.dataservices.")
public class LocalDBConfig extends Neo4jConfiguration {
public LocalDBConfig() {
setBasePackage("com.toyfiles");
}
@Bean
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("target/graph.db");
}
}
public interface BrandRepository extends GraphRepository<Brand> {
@Query(value = "MATCH (brand:Brand {name:{0}})-[:PART_OF]->line RETURN line")
public List<Line> getLinesForBrand(String name);
@Query(value = "MATCH (brand:Brand {name:{0}}) DELETE brand")
public void deleteBrandByName(String name);
public Brand findBrandByName(String name);
}
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandRepository': Cannot resolve reference to bean 'neo4jTemplate' while setting bean property 'neo4jTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jTemplate' defined in class path resource [com/toyfiles/configuration/LocalDBConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.Neo4jTemplate org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jMappingContext' defined in class path resource [com/toyfiles/configuration/LocalDBConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d49af10: startup date [Mon Sep 29 09:48:25 PDT 2014]; root of context hierarchy
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1457)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1198)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
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.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:964)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)
... 116 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jTemplate' defined in class path resource [com/toyfiles/configuration/LocalDBConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.Neo4jTemplate org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jMappingContext' defined in class path resource [com/toyfiles/configuration/LocalDBConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d49af10: startup date [Mon Sep 29 09:48:25 PDT 2014]; root of context hierarchy
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 129 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.support.Neo4jTemplate org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTemplate() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jMappingContext' defined in class path resource [com/toyfiles/configuration/LocalDBConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d49af10: startup date [Mon Sep 29 09:48:25 PDT 2014]; root of context hierarchy
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
... 138 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jMappingContext' defined in class path resource [com/toyfiles/configuration/LocalDBConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d49af10: startup date [Mon Sep 29 09:48:25 PDT 2014]; root of context hierarchy
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.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:288)
at com.toyfiles.configuration.LocalDBConfig$$EnhancerBySpringCGLIB$$321222c6.mappingInfrastructure(<generated>)
at org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTemplate(Neo4jConfiguration.java:135)
at com.toyfiles.configuration.LocalDBConfig$$EnhancerBySpringCGLIB$$321222c6.CGLIB$neo4jTemplate$23(<generated>)
at com.toyfiles.configuration.LocalDBConfig$$EnhancerBySpringCGLIB$$321222c6$$FastClassBySpringCGLIB$$12e74a61.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
at com.toyfiles.configuration.LocalDBConfig$$EnhancerBySpringCGLIB$$321222c6.neo4jTemplate(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
... 139 more
Caused by: java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d49af10: startup date [Mon Sep 29 09:48:25 PDT 2014]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:346)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:307)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.addPersistentEntity(Neo4jMappingContext.java:69)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.addPersistentEntity(Neo4jMappingContext.java:49)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:181)
at org.springframework.data.neo4j.mapping.RelationshipInfo.<init>(RelationshipInfo.java:65)
at org.springframework.data.neo4j.mapping.RelationshipInfo.fromField(RelationshipInfo.java:79)
at org.springframework.data.neo4j.support.mapping.Neo4jPersistentPropertyImpl.extractRelationshipInfo(Neo4JPersistentPropertyImpl.java:128)
at org.springframework.data.neo4j.support.mapping.Neo4jPersistentPropertyImpl.<init>(Neo4JPersistentPropertyImpl.java:80)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.createPersistentProperty(Neo4jMappingContext.java:161)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.createPersistentProperty(Neo4jMappingContext.java:49)
at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.createAndRegisterProperty(AbstractMappingContext.java:449)
at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.doWith(AbstractMappingContext.java:427)
at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:606)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:295)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.addPersistentEntity(Neo4jMappingContext.java:69)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.addPersistentEntity(Neo4jMappingContext.java:49)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:181)
at org.springframework.data.neo4j.mapping.RelationshipInfo.<init>(RelationshipInfo.java:65)
at org.springframework.data.neo4j.mapping.RelationshipInfo.fromField(RelationshipInfo.java:79)
at org.springframework.data.neo4j.support.mapping.Neo4jPersistentPropertyImpl.extractRelationshipInfo(Neo4JPersistentPropertyImpl.java:128)
at org.springframework.data.neo4j.support.mapping.Neo4jPersistentPropertyImpl.<init>(Neo4JPersistentPropertyImpl.java:80)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.createPersistentProperty(Neo4jMappingContext.java:161)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.createPersistentProperty(Neo4jMappingContext.java:49)
at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.createAndRegisterProperty(AbstractMappingContext.java:449)
at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.doWith(AbstractMappingContext.java:427)
at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:606)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:295)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.addPersistentEntity(Neo4jMappingContext.java:69)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.addPersistentEntity(Neo4jMappingContext.java:49)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:257)
at org.springframework.data.mapping.context.AbstractMappingContext.initialize(AbstractMappingContext.java:373)
at org.springframework.data.neo4j.support.mapping.Neo4jMappingContext.initialize(Neo4jMappingContext.java:111)
at org.springframework.data.mapping.context.AbstractMappingContext.afterPropertiesSet(AbstractMappingContext.java:363)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
... 160 more
最佳答案
我为存储库使用了@Lazy 注释来延迟对存储库的引用,因为存储库在启动的后期才正确启动。这允许将存储库引用延迟到需要且可用为止。
@Autowired
@Lazy
BrandRepository brandRepository;
关于spring-mvc - Spring Boot MVC 转换器无法 Autowiring Neo4J 数据存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26105148/
这更像是一个最佳实践类型的问题。 我听过很多次: a) 在 Spring 中 Autowiring 时,最佳做法是 Autowiring 接口(interface)“而不是”实现。 和.. b) 我还
我正在查看工作区中的一些旧示例。我看不出怎么样由于没有 @Autowired, Autowiring 完成。 Spring boot + facebook 默认配置。 @Controller @Req
事实似乎并非如此。我曾经认为 XML 配置是为了覆盖注释。但是当我在XML配置中设置autowire =“no”时,bean的@Autowired注释属性仍然有效。我不再确定 XML autowire
为什么需要 Autowiring ? Autowiring 概念的解释是什么?@autowired Spring Framework 中的注释. 最佳答案 不需要 Autowiring ,只是方便。
来自this Spring documentation我知道当我使用@Bean时,默认值已经相当于: @Bean(autowire = Autowire.NO) (Default) No autowi
遇到了一个奇怪的要求。我需要将唯一的错误 ID 附加到 log4j 消息并将该消息 ID 返回给接口(interface)。所以,我虽然让我们创建一个 spring 服务,就像这样 public cl
这个问题已经有答案了: @Autowire failing with @Repository (3 个回答) 已关闭 4 年前。 我有一个类“ReportEverythingForm”,它拒绝通过自动
我是 Spring 的新手。我正面临 Spring-Boot 的问题。我正在尝试将一个字段从外部配置文件 Autowiring 到一个 Autowiring 的 bean 中。我有以下类(class)
我有一个带有存储库的 Spring Boot 应用程序。 我还使用@Service并扩展其中的存储库。 当我尝试 @Autowired 我拥有的服务时: Caused by: org.springfr
我有一个接口(interface)C,想要访问另外两个类中的getClassName()。访问 a.getClassName() 时,method1() 中出现异常。 public interface
我遇到了一个奇怪的问题,其中注入(inject)了 @Autowire 的 Component 在一个类中可用,但在另一个类中不可用。 我在Account和Agreement类的属性network中使
考虑以下示例代码: public class SmallCar { private CarEngine carEngine; @Autowired public SmallCa
autowire = "no"和 autowire = "default"有什么区别?如果它们相同,那么为什么我们有这 2 个选项。 最佳答案 Beans The default is "defaul
我已将项目更改为使用注释而不是 xml 文件,但这会增加应用程序部署时间。现在我正在寻找减少它的方法。 按类型 Autowiring 和按名称 Autowiring 之间有性能差异吗? 热烈欢迎任何其
我有一个与 Web 插件一起使用的 spring boot 应用程序。 在一节课中我有: package com.test.company @Component @RestController pub
我有一个可以执行某些操作的系统。该系统使用以下方法为每个对象创建一个单独的线程: stp.scheduleWithFixedDelay((EditSite) ctx.getBean("EditSite
我正在尝试自动连接存储库,但它无法工作。我已经为此苦苦挣扎了一个星期,但我似乎无法弄清楚。有趣的是,当我注释掉人员存储库的 Autowiring 时,程序可以正常工作并正确编译,但是一旦我尝试 Aut
意味着如果具有所需类型的 bean 不超过 1 个,bean 的所有字段将自动注入(inject)依赖项。 问题是当使用注解时它是如何工作的,它到底能不能工作。 我的测试表明即使我使用 @Resou
我有一个 Autowiring 其他 bean 的组件: @Component public class MyComponent { @Autowired private Enviro
这是我的类代码,其中有 @Autowired 字段: 测试A @ContextConfiguration("classpath:spring.xml") public abstract class T
我是一名优秀的程序员,十分优秀!