Spring Framework 5 Simple bean示例-为什么出现NoSuchBeanDefinitionException

时间:2018-11-19 02:43:19

标签: java spring

我是注解式Spring框架的新手,并且配置了一个简单的bean,无法使@ComponentScan正常工作。 Bean和应用程序都在同一包中。

package springdemo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Pet {
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Bean
    public Pet fido() {
        Pet p = new Pet();
        p.setName("fido");
        return p;
    }
}

应用程序:

package springdemo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class BeanApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.refresh();
        ctx.start();
        Pet dog = (Pet) ctx.getBean("fido");
        System.out.println(dog.getName());
        ctx.stop();
    }
}

运行此程序时,应用程序找不到Bean(我假设组件扫描也可以拾取Bean ...):

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fido' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1212)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    at springmvc.bean.BeanApplication.main(BeanApplication.java:15)

我可以通过scan()方法而不是通过注释使组件扫描在代码中起作用。

1 个答案:

答案 0 :(得分:3)

您需要传递一个配置类,在其中提供将由Spring容器管理的bean。

没有Spring Boot的经典Spring应用程序示例

var first = db.collection("....").orderBy("price", "desc").limitTo(20);

return first.get().then(function (documentSnapshots) {
  // Get the last visible document
  var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
  console.log("last", lastVisible);

  // Construct a new query starting at this document,
  // get the next 25 cities.
  var next = db.collection("....")
          .orderBy("price", "desc")
          .startAfter(lastVisible)
          .limit(20);
});

春季启动示例:

public class SpringApplication {

    public static void main(String[] args){
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        Pet dog = (Pet)ctx.getBean("fido");
        System.out.println(dog.getName());
    }

}


@Configuration
class AppConfig {

    @Bean
    public Pet fido() {
        Pet p = new Pet();
        p.setName("fido");
        return p;
    }
}


class Pet {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}