首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在运行main方法时,没有定义类型的限定bean。

在运行main方法时,没有定义类型的限定bean。
EN

Stack Overflow用户
提问于 2016-05-12 13:29:15
回答 1查看 603关注 0票数 0

我以前见过很多关于这个错误的问题,但是没有一个对我有用的解决方案。

我对Spring很陌生,但我尝试将Spring数据用于项目的Neo4J库。我决定从一个快速的高峰开始,以确保我知道一切是如何工作的,所以我设置了一个简单的App类,其主要方法如下:

代码语言:javascript
复制
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());
    }
}

我还设置了配置类:

代码语言:javascript
复制
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。我误会什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-12 15:04:32

如果Spring没有扫描类包,那么放置@Component注释并不重要。您可以在配置类中添加@ComponentScan注释,并将其配置为扫描App类所在的包。或者,您可以删除@Component注释,并在configuration类中声明App类型的Bean。

希望这能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37188426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档