Srishti Rawal
Srishti Rawal

Reputation: 654

no such bean exception

Unable to resolve following Exception. Trying an example of spring dependency injection.

I'm getting the error "No bean named 'book' is defined even though i have defined it in the xml file.

I need to print author and title as defined in the xml to console.

Exception is as below:

    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)

Book.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;

        }
    }

Driver.java :

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());
    }
}

XML :

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id = "book" class = "com.Rawal.Srishti.SDI.Book" >
        <constructor-arg index="0" ref="Srishti" />
        <constructor-arg index="1" ref="Rawal"/>
    </bean>

</beans>

Upvotes: 2

Views: 631

Answers (2)

XuSen
XuSen

Reputation: 35

if you still wanna use AnnotationConfigApplicationContext instead, then probably you can change your configuration to Class based instead of XML based.

Class based

package com.samsung;

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

XML based

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

Then as per usual you can inject the bean by the following

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();
   }
}

Upvotes: 0

Arnaud
Arnaud

Reputation: 17534

You don't seem to use the right kind of application context.

The one you are using,AnnotationConfigApplicationContext is annotations based, and its String parameter is a base java package to scan, not a file name.

You should rather use ClassPathXmlApplicationContext (if your file is in the classpath) or FileSystemXmlApplicationContext , e.g :

ApplicationContext ctx = new ClassPathXmlApplicationContext("Rawal_spring.xml");

Upvotes: 2

Related Questions