Spring入门学习——bean属性配置(一)
来源:互联网 发布:c语言的库函数 编辑:程序博客网 时间:2024/06/06 19:20
<bean name="sequenceGenerator"
class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="prefix">
<value>30</value>
</property>
<property name="suffix">
<value>A</value>
</property>
<property name="initial">
<value>1000000</value>
</property>
</bean>
package com.cgy.springrecipes.sequence;
public class SequenceGenerator {
private String prefix;
private String suffix;
private int initial;
private int counter;
public SequenceGenerator() {}
public SequenceGenerator(String prefix, String suffix, int initial) {
this.prefix = prefix;
this.suffix = suffix;
this.initial = initial;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setInitial(int initial) {
this.initial = initial;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefix);
buffer.append(initial + counter++);
buffer.append(suffix);
return buffer.toString();
}
}
SequenceGenerator类中只有setter方法,没有getter方法,因此类中的属性都是通过配置文件中<property>元素来配置的,每一个<property>要求bean包含对应的一个设值方法(setter)。同时可以在<constructor-arg>元素中配置属性,但是没有name属性,因为该顺序基于构造函数参数的位置。
简写定义Bean属性
<bean name="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator">
<property name="prefix" value="30"/>
<property name="suffix" value="A"/>
<property name="initial" value="1001000"/>
</bean>
Spring2.0后缩写形式
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sequenceGenerator" class="com.cgy.springrecipes.sequence.SequenceGenerator"
p:prefix="30" p:suffix="B" p:initial="101000" />
</beans>
Map属性配置
<property name="suffixes">
<map>
<entry>
<key>
<value>type</value>
</key>
<value>A</value>
</entry>
<entry>
<key>
<value>url</value>
</key>
<bean class="java.net.URL">
<constructor-arg value="http"/>
<constructor-arg value="www.cgydawn.com"/>
<constructor-arg value="/"/>
</bean>
</entry>
</map>
</property>
Map简化配置
<map>
<entry key="type" value="A"/>
<entry key="url">
<bean class="java.net.URL">
<constructor-arg value="http"/>
<constructor-arg value="www.cgydawn.com"/>
<constructor-arg value="/"/>
</bean>
</entry>
</map>
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
- Spring入门学习——bean属性配置(一)
- Spring入门学习——bean属性配置(二)
- Spring入门学习——继承Bean配置
- Spring入门学习——外部化Bean配置
- 简化Spring的XML配置(一)——自动装配bean属性
- Spring入门学习——从对象属性中声明Bean
- Spring入门学习——配置文件<beans>属性配置记录
- Spring配置bean属性
- (学习笔记)spring框架入门之配置bean(处理属性中有集合的情况)
- spring—Bean配置
- spring—Bean配置
- Spring入门学习——用XML配置自动装配Bean
- Spring入门——Spring Bean装配之Bean的配置项
- 学习笔记——Spring 中Bean属性的注入
- Spring的bean属性配置
- spring-bean属性配置解析
- spring-bean属性配置解析
- spring Bean属性配置细节
- Win64 GTK+3编程【1】dev-cpp…
- maven在eclipse中使用的小技巧总结
- Win64 GTK+3编程【2】GTK+3完…
- Scala基础知识之函数
- rxjava2 retrofit2 配置
- Spring入门学习——bean属性配置(一)
- 4—IPv6编程*.dll、*.a篇,动态和静…
- 安卓权限配置时出现“Permission is only granted to system apps”
- 研究方向----简洁的美
- Spring入门学习——bean属性配置(二)
- OpenGL 通过glViewport实现2d…
- 【leetcode】Two Sum II
- LinkedList源码阅读
- CentOS给予port端口权限(搭建web…