2

I'm new in spring Framework. And actually i was doing an experiment with spring actually.

Look at this HelloWorld.java:

public class HelloWorld {

    private String messageee;

    public void setMessage(String messageee){
        this.messageee=messageee;
    }

    public void show(){
        System.out.println("message: "+messageee);
    }
}

You see in this program, I've one variable which is outside declared as private named as messageee and next variable which is parametrized with setter named as messageee. You see both have same name.

Okay.. Now look at this bean file:

<?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.xsd">

    <bean id="helloWorld" class="com.springframework.HelloWorld">
        <property name="message" value="Hello.. This is Spring Framework example."></property>
    </bean>

</beans>

Here you see inside bean tag. I've declared the property name as message. I don't understand, when i give the name as messageee it gives an error as:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'messageee' of bean class [com.springframework.HelloWorld]: Bean property 'messageee' is not writable or has an invalid setter method. Did you mean 'message'?

But when i give the name as message. It runs successfully. But i don't have any message's method or any kind of variables with this similar name. So, How setter works actually? Will you please elaborate?

Help would be appreciated!

pankaj padiya
  • 133
  • 2
  • 7

3 Answers3

9

You're confusing fields (or instance variables) with properties.

property is a term coming from the Java Beans specification. A property foo of a bean is data that can be accessed using a getter method called getFoo() (or isFoo() for a boolean) and/or set using a setter method called setFoo().

What these methods do internally, whether they get/set a variable or not, whether the variable is also named foo or anything else, is completely irrelevant. What matters is the name of the getter/setter.

So, when you define your bean and tell Spring to set the property named message, Spring will look for a method called setMessage(). It doesn't care about the private fields of your bean class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yes.. You actually get my point!!.. So, when i give the property named as `message` ..Spring will look for a method called `setMessage()`.. Are they `Autowired` by using this sense?.. That's what we say `Ioc container` or `auto-wired` the functionality of Spring? – pankaj padiya Jan 25 '15 at 14:32
  • I can't understand what you're asking. autowiring simply consists in letting Spring discover the bean dependencies thanks to types and annotations rather than by defining everything in XML. – JB Nizet Jan 25 '15 at 14:33
  • 1
    Sorry for my bad english.. I'm a student!.. You know what, when i use `setFoo(String messageee)`.. bean file start laughing at error.. but when i change my property name as `foo`. and finally it stops giving those errors. :) – pankaj padiya Jan 25 '15 at 14:39
0

The Spring IoC container also supports setter injection, which is the preferred method of dependency injection in Spring. Setter injection uses the set* methods in a class file to garner property names that are configurable in the spring XML config.

From a configuration standpoint, setter injection is easier to read because the property name being set is assigned as an attribute to the bean, along with the value being injected.

To determine the property names, Spring follows the JavaBeans Specification.

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
-1

First of all, you are mixing up fields with properties - also your property name in the applicationContext.xml is wrong (it should be messageee)

You need to use @Autowired annotation with either:

1) fields i.e. messageee

or

2) setter i.e. setMessage()

If you are thinking "what is that!!???" Read about Spring's basic features with beans and how Spring is capable of taking POJOs (Plain Old Java Objects) to and configure them using the IoC framework. Read about @Autowired here - How does autowiring work in Spring?

Then you should be fine with this:

<bean id="helloWorld" class="com.springframework.HelloWorld">
    <property name="message" value="Hello.. This is Spring Framework example."></property>
</bean>

BTW...good approach for looking into Spring by using the very basic Java stuff....good luck!

Community
  • 1
  • 1
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108