没有这样的bean异常

时间:2018-09-21 09:15:54

标签: java spring

无法解决以下异常。 尝试弹簧依赖注入的示例。

我收到错误消息“即使在xml文件中定义了名为'book'的bean,也没有定义。

我需要打印xml中定义的作者和标题才能进行控制台。

异常如下:

db.ensureIndex

Book.java:

    Sep 21, 2018 3:05:23 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@37bba400: startup date [Fri Sep 21 03:05:23 MDT 2018]; root of context hierarchy
    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'book' is defined
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:701)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
        at com.Rawal.Srishti.SDI.Driver_new.main(Driver_new.java:10)

Driver.java:

    package com.Rawal.Srishti.SDI;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    @Component
    class Book extends Media {
        private static int order = 1;
        private String author;
        @Autowired
        public Book(String title, String author) {
            this.title = title;
            this.author = author;
        }

        public String getAuthor() {
            return author;
        }
        public String getTitle() {
            return title;
        }
        static public int getOrder() {
            return order;
        }
        @Override
        protected String compareString() {
            return getOrder() + getTitle() + getAuthor();
        }
        public String toString() {
            return getTitle() + " by " + getAuthor();
        }

        public int compareTo(Media obj) {
            if(obj instanceof Book) {
                return -(this.getTitle().compareTo(obj.getTitle()));
            } 

            return 444;

        }
    }

XML:

package com.Rawal.Srishti.SDI;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Driver_new {
    public static void main(String[] args) {
          @SuppressWarnings("resource")
        ApplicationContext ctx = 
             new AnnotationConfigApplicationContext("Rawal_spring.xml");
          Book m = (Book) ctx.getBean("book", Book.class);
          System.out.println(m.getTitle());
          System.out.println(m.getAuthor());
    }
}

2 个答案:

答案 0 :(得分:2)

您似乎没有使用正确的应用程序上下文。

您正在使用的standalone是基于注释的,其AnnotationConfigApplicationContext参数是要扫描的基本Java程序包,而不是文件名。

您应该使用String(如果文件在类路径中)或ClassPathXmlApplicationContext,例如:

FileSystemXmlApplicationContext

答案 1 :(得分:0)

如果您仍然想使用AnnotationConfigApplicationContext,则可以将配置更改为基于类而不是基于XML。

基于类

package com.samsung;

@Configuration
public class SpringConfig {
   @Bean 
   public HardDrive hardDrive(){
      return new HardDrive();
   }
}

基于XML

<beans>
   <bean id = "hardDrive" class = "com.samsung.HardDrive" />
</beans>

然后按照通常的方式,您可以通过以下方式注入bean

public class HardDrive {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("manufactured by : " + message);
   }
}

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx = 
         new AnnotationConfigApplicationContext(SpringConfig.class);

      HardDrive hardDrive = ctx.getBean(HardDrive.class);
      hardDrive.setMessage("Samsung");
      hardDrive.getMessage();
   }
}