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.
<?xml version="1.0"?>
<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.
<?xml version="1.0"?>
<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.
<?xml version="1.0"?>
<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.
相關文章
- How to update BOL entity property value via ABAP code
- [Spring]BeanSpringBean
- Spring Bean容器SpringBean
- 【Spring】Bean管理SpringBean
- spring引入properties變數報錯Spring變數
- Spring bean 裝配SpringBean
- Spring Bean 綜述SpringBean
- Spring Bean作用域SpringBean
- spring boot factory beanSpring BootBean
- 淺談Spring BeanSpringBean
- Spring基礎(Bean)SpringBean
- Spring Bean 詳解SpringBean
- Spring裝配Bean(六)Bean的作用域SpringBean
- Spring裝配Bean(四)SpringBean
- Spring裝配Bean(一)SpringBean
- Spring裝配Bean(二)SpringBean
- 二、Spring裝配BeanSpringBean
- Spring系列.Bean簡介SpringBean
- 《Spring》(六)---- Bean的scopeSpringBean
- Spring Bean 生命週期SpringBean
- Spring Bean 標籤解析SpringBean
- Spring Bean生命週期SpringBean
- Spring IOC 一——Spring容器裝配BeanSpringBean
- Spring 學習筆記(2) Spring BeanSpring筆記Bean
- Spring 原始碼(17)Spring Bean的建立過程(8)Bean的初始化Spring原始碼Bean
- 「Spring認證」Spring Bean 定義教學SpringBean
- Spring bean詳細介紹SpringBean
- 獲取spring裡的beanSpringBean
- 【Spring】BeanFactory 解析 bean 詳解SpringBean
- Spring中bean的含義SpringBean
- 【Spring 註解】@Configuration和@BeanSpringBean
- spring getBean(String) No bean named '' is definedSpringBean
- Spring Bean 的一生SpringBean
- Spring IoC bean 的建立(上)SpringBean
- Spring Bean的生命週期SpringBean
- 關於Spring的bean注入SpringBean
- spring bean 擴充套件方式SpringBean套件
- Spring Bean 定義繼承SpringBean繼承
- Spring中Bean的作用域SpringBean