How to inject value into bean properties in Spring

In Spring, there are three ways to inject value into bean properties.

  • Normal way
  • Shortcut
  • “p” schema

See a simple Java class, which contains two properties – name and type. Later you will use Spring to inject value into the bean properties.


package com.mkyong.common;

public class FileNameGenerator 
{
	private String name;
	private String type;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
}

1. Normal way

Inject value within a ‘value’ tag and enclosed with ‘property’ tag.


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

	<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
		<property name="name">
			<value>mkyong</value>
		</property>
		<property name="type">
			<value>txt</value>
		</property>
	</bean>
</beans>

2. Shortcut

Inject value with “value” attribute.


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

	<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
		<property name="name" value="mkyong" />
		<property name="type" value="txt" />
	</bean>
	
</beans>

3. “p” schema

Inject value by using “p” schema as an attributes.


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

	<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator" 
             p:name="mkyong" p:type="txt" />
	
</beans>

Remember declares the xmlns:p=”http://www.springframework.org/schema/p in the Spring XML bean configuration file.

Conclusion

Which methods to use is totally base on personal preference, it will not affect the value inject into the bean properties.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
14 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
16 Comment authors
sachinCarlo Pmohd faizanMahesh Umashareef Recent comment authors
newest oldest most voted
Carlo P
Guest
Carlo P

Remember to put getters and setters on your bean class.

sachin
Guest
sachin

Did you copy that beans xmlns tag and if u did where did you copied from or you have to write that everytime in new project

mohd faizan
Guest
mohd faizan

sir tell me the use of property tag .

Mahesh Uma
Guest
Mahesh Uma
shareef
Guest
shareef

cool i like the third type

Bryant
Guest
Bryant

I think you need to take out “p” name space in the tutorial. xmlns:p=”http://www.springframework.org/schema/p” is no longer available.

Sonjake
Guest
Sonjake

And where is the Injection??? THIS IS JUST *** THAT ARE JUST SCHMEMAS!

Phongbv
Guest
Phongbv

Please read previous articles

soniya
Guest
soniya

Just superb tutorial!!! Amazine tutorial content

Ricky
Guest
Ricky

why does the http://www.springframework.org/schema/p url point to nothing? I want eclipse to show me the options for p when I hit control space and right now it doesn’t work. I’m guessing because http://www.springframework.org/schema/p doesn’t point to anything??

sishao_uestc
Guest
sishao_uestc

yes,point to nothing ,i just wonder why too.

balaji
Guest
balaji

@Bebiton

can be written in p name space as

Mao
Guest
Mao

Hello,
It is first time for me to read here, you are doing a great job.

Beniton
Guest
Beniton

Super Article

Makes life easy

One small doubt how to i set ref- properties with p schema.

Thanks

balaji
Guest
balaji
 
<bean id="john-classic" class="com.example.Person"> 
    <property name="name" value="John Doe"/> 
    <property name="spouse" ref="jane"/> 
</bean> 
<bean name="jane" class="com.example.Person"> 
    <property name="name" value="John Doe"/> 
</bean>
<bean id="john-classic" class="com.example.Person" 
    p:name="John Doe" 
    p:spouse-ref="jane"/> 
</bean> 
<bean name="jane" class="com.example.Person" 
    p:name="John Doe"/> 
</bean>
Beniton
Guest
Beniton

Thank you Balaji

dinesh tiwari
Guest
dinesh tiwari

We simple say WOW WOW WOW !! Man you are doing wonderful job again and again ….
We are your fan now…

trackback
Spring Tutorials | Tutorials

[…] Define bean properties in Spring There are three ways to e ways to define the bean properties in Spring. […]