0

Consider the following configuration

 public class MainApp {
        public static void main(String args[]){

            ApplicationContext ac=new ClassPathXmlApplicationContext("src/Beans.xml");
            HelloWorld obj1=(HelloWorld) ac.getBean("helloWorld");
            obj1.setMessage("OBJ1");
            HelloWorld obj2=(HelloWorld) ac.getBean("helloWorld");
            //obj2.setMessage("OBJ2");
            System.out.println(obj1.getMessage());
            System.out.println(obj2.getMessage());
        }
    }

    @Scope("prototype")

    public class HelloWorld {
    String message;
        public String getMessage() {
            return "Your Message:"+message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        }


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
               <context:annotation-config />
               <context:component-scan base-package="SpringDemo.src"  />
                 <bean id="helloWorld" class="src.HelloWorld">
           </bean>
          </beans>

If i am not wrong it is showing the behavior of a Singleton scope. can someone let me know why it is not behaving as a "Prototype" scope?

Aashish
  • 31
  • 1
  • 1
  • 7
  • class name `src.HelloWorld` is wrong. You do not need to specify `src` directory as a `package` – Nandkumar Tekale May 17 '13 at 12:44
  • If i remove it, it shows an error Cannot find class [HelloWorld] for bean with name 'helloWorld' defined in class path resource [src/Beans.xml]; I am getting the output, but its for singleton scope and not for prototype – Aashish May 17 '13 at 12:48
  • @Aashish what exactly is the behavior you're seeing? – soulcheck May 17 '13 at 12:59
  • Its printing Your Message:OBJ1 Your Message:OBJ1 while it should print Your Message:OBJ1 Your Message:null – Aashish May 17 '13 at 13:04

1 Answers1

4

You have this <bean id="helloWorld" class="src.HelloWorld"> in the xml configuration. When no scope is specified the scope defaults to singleton. The xml configuration overrides the annotation. Remove @Scope("prototype") and add scope="prototype" in the xml.

JJ Zabkar
  • 3,792
  • 7
  • 45
  • 65
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • But Evgeny, I want to define the scope using annotations – Aashish May 17 '13 at 13:33
  • Then use only annotations and remove the xml bean definition – Evgeni Dimitrov May 17 '13 at 13:34
  • If i am removing it, i am getting an error Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloWorld' is defined – Aashish May 17 '13 at 13:35
  • I'm not very familiar with annotation declaration, but I think you need @Component along with @Scope("prototype") – Evgeni Dimitrov May 17 '13 at 13:36
  • Refer to this http://stackoverflow.com/questions/10126537/how-to-define-a-spring-bean-using-annotation-instead-of-xml and this http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-stereotype-annotations – Evgeni Dimitrov May 17 '13 at 13:38
  • I have tried adding that @Evgeny, didnt work. Thanks for the help – Aashish May 17 '13 at 13:38
  • this looks wrong to me. the value should be the package of the class – Evgeni Dimitrov May 17 '13 at 13:41
  • That was it. Thanks. But we are not defining the bean in application contxt.xml then how it is identifying the "helloWorld" in ac.getBean("helloWorld")..my understanding is that it is because of the @component annotation – Aashish May 20 '13 at 06:19
  • Yes. tells Spring to scan these packages for annotated classes. – Evgeni Dimitrov May 20 '13 at 06:55
  • It's a good practice to keep the packages to scan as narrow as posible(only the package with the annotated classes). Too many classes to scan will cause the app to start slowly – Evgeni Dimitrov May 20 '13 at 06:59
  • Just to add on, how it is retrieving the name "helloWorld" for the bean. We are no where mentioning this name – Aashish May 22 '13 at 06:17
  • Not sure about it but I think it's the default. 'public class HelloWorld {' default bean id is helloWorld. You can override it with the annotation value. – Evgeni Dimitrov May 22 '13 at 06:58
  • I'd upvote this answer 10x if I could. I was banging my head against the computer for 3 days, and the problem was simply some forgotten configuration on a xml file. – Cotta May 11 '16 at 20:53