我以前见过很多关于这个错误的问题,但是没有一个对我有用的解决方案。
我对Spring很陌生,但我尝试将Spring数据用于项目的Neo4J库。我决定从一个快速的高峰开始,以确保我知道一切是如何工作的,所以我设置了一个简单的App类,其主要方法如下:
package org.example.neo4jSpike;
import org.example.neo4jSpike.domain.Actor;
import org.example.neo4jSpike.repositories.ActorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
/**
* Hello world!
*
*/
@Component
public class App
{
@Autowired
private ActorRepository actors;
@SuppressWarnings("resource")
public static void main( String[] args )
{
ApplicationContext context = new AnnotationConfigApplicationContext(SpikeConfiguration.class);
App a = context.getBean(App.class);
a.init();
}
private void init(){
Actor michaelDouglas = actors.save(new Actor("Michael Douglas"));
System.out.println( "Hello World!" );
System.out.println(michaelDouglas.getId());
System.out.println("Total people: " + actors.count());
}
}我还设置了配置类:
package org.example.neo4jSpike;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableNeo4jRepositories(basePackages = "org.example.neo4jSpike.repositories")
@EnableTransactionManagement
public class SpikeConfiguration extends Neo4jConfiguration{
@Bean
public SessionFactory getSessionFactory() {
// with domain entity base package(s)
return new SessionFactory("org.example.neo4jSpike.domain");
}
// needed for session in view in web-applications
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}
}如果需要,我将为我的存储库和域类添加代码,但它们都是以类似的方式设置的,而且都非常简单。
但是,当我尝试运行main时,
No qualifying bean of type [org.example.neo4jSpike.App] is defined
我不明白它是如何定义的,它就在那里,定义为一个@Component。我误会什么了?
发布于 2016-05-12 15:04:32
如果Spring没有扫描类包,那么放置@Component注释并不重要。您可以在配置类中添加@ComponentScan注释,并将其配置为扫描App类所在的包。或者,您可以删除@Component注释,并在configuration类中声明App类型的Bean。
希望这能帮上忙。
https://stackoverflow.com/questions/37188426
复制相似问题