Doesn't Spring 4 support scope attribute?

I was trying to use the bean scope of Spring 4 as prototype in one of my programs to check if different objects are getting created per request or not. For it I was using the following snippet:

<bean id="television" class = "org.java.springcore.Television"  scope="prototype">
    <property name="model" value="Samsung_6970"/>
    <property name="yearOfManufature" value="2016"/>
    <property name="diameter" value="55"/>      
</bean>

Then I initialised the triangle object in my Main class as following:

public class TelevisionUser {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // BeanFactory factory = new XmlBeanFactory(new
        // FileSystemResource("spring.xml"));
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        context.registerShutdownHook();

        Television television1 = (Television) context.getBean("television");

        television1.setMsg("Setting messege for Television");
        System.out.println("The message for television 1 is: "+television1.getMsg());
        Television television2 = (Television) context.getBean("television");

        System.out.println("The message for television 2 is: "+television2.getMsg());

    }
}

And my Television class is as follows:

public class Television implements InitializingBean
{
    private Integer yearOfManufature;
    private String model;
    private Integer diameter;   
    private String msg;

    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public Integer getYearOfManufature() {
        return yearOfManufature;
    }
    public void setYearOfManufature(Integer yearOfManufature) {
        this.yearOfManufature = yearOfManufature;
    }
    public Integer getDiameter() {
        return diameter;
    }
    public void setDiameter(Integer diameter) {
        this.diameter = diameter;
    }   
    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }
    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Initialising the Bean Television");

    }
}

I have 2 questions:

  1. When am using the scope attribute, the XML validator is Throwing an error, "Attribute "scope" must be declared for element type "bean". " Is scope attribute no more in use for Spring 4?

  2. If I use the attribute singleton and set it's value to false, then my program is behaving strangely. i.e. the output is coming to:

    Initialising the Bean Television The message for television 1 is: Setting messege for Television The message for television 2 is: Setting messege for Television

The bean is being initialised only once as is apparent from the output, even though I am setting singleton="false". Hence the message is also being set up for the object television1 and is being reflected for television 2 as well.

I don't understand where I'm going wrong.

Upvotes: 1

Views: 1058

Answers (1)

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18825

scope attribute on bean element is still supported in Spring 4, see http://www.springframework.org/schema/beans/spring-beans-4.3.xsd beans schema.

singleton is gone however, and that's why your example is probably failing. It's been gone since Spring 2.0 though still supported internally and maybe removed completely later.

About why the validator fails on your bean definition:

  • Check whether you have proper schema location: xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"

  • I see some double space in your example, check whether there are just spaces or tabs and no weird character from Unicode.

Upvotes: 3

Related Questions