之前我已经看过很多关于这个错误的问题,但没有对我有用的解决方案。
我是Spring的新手,但是尝试将Spring Data用于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
。我有什么误会?
答案 0 :(得分:1)
如果Spring没有扫描你的类包,你是否放了@Component注释并不重要。您可以在配置类中添加@ComponentScan批注,并将其配置为扫描App类所在的包。或者,您可以删除@Component注释,并在配置类中声明类型为App的Bean。
希望这可以提供帮助。