0
我以前看過很多關於此錯誤的問題,但沒有解決方案適用於我。運行主要方法時沒有定義任何類型的限定bean
我是Spring的新手,但試圖將Spring數據用於Neo4J庫的項目。我決定開始與快速扣球,以確保我知道一切是如何工作的,所以我成立了一個簡單的應用程序類,象這樣一個主要方法:
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
。我誤解了什麼?
不錯,你有一個'@ Component',但它可能不在那裏......'@ Component'並且你的配置類沒有'@ ComponentScan',所以它永遠不會被檢測到... –
As我說,這是我第一個Spring應用程序,所以我沒有意識到這一點。謝謝。把它變成一個答案,我會接受,這讓我解決了這個問題。 – Paul