使用XML装配Bean需要定义对于的XML,需要引入对应的XML模式(XSD)文件,这些文件会定义配置Spring Bean的一些元素,简单的配置如下:

<?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"> </beans>

创建角色类Role

类构造器有参与无参

代码:

Role:

package com.wbg.springxmlbean.entity;

public class Role {
private int id;
private String roleName;
private String note; @Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", note='" + note + '\'' +
'}';
} public Role() {
} public Role(int id, String roleName, String note) {
this.id = id;
this.roleName = roleName;
this.note = note;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getRoleName() {
return roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} public String getNote() {
return note;
} public void setNote(String note) {
this.note = note;
}
}

User:

package com.wbg.springxmlbean.entity;

public class User {
private int id; private Role role; @Override
public String toString() {
return "User{" +
"id=" + id +
", role=" + role +
", name='" + name + '\'' +
", age=" + age +
'}';
} public Role getRole() {
return role;
} public void setRole(Role role) {
this.role = role;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} private String name;
private int age;
}

xml进行配置:

1、装备简易值

  <!--
id:属性是Spring找到的这个Bean的编号,不是必须的,如果没有Spring会采用:
"全限定名#{number}"的格式生成编号
列如: <bean class="com.wbg.springxmlbean.entity.Role">
Spring会生成编号为:"com.wbg.springxmlbean.entity.Role#1"
class:是一个类的全限定名
-->
<bean id="role1" class="com.wbg.springxmlbean.entity.Role">
<!-- property元素是定义类的属性,name属性定义的是属性名称 value是值
相当于:
Role role=new Role();
role.setId(1);
role.setRoleName("高级工程师");
role.setNote("重要人员");-->
<property name="id" value="1"/>
<property name="roleName" value="高级工程师"/>
<property name="note" value="重要人员"/>
</bean>
<bean id="rolew" class="com.wbg.springxmlbean.entity.Role">
<!-- constructor-arg元素,index代表参数索引, value是值
相当于:
Role role=new Role(1,"高级工程师","重要人员");-->
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="高级工程师"/>
<constructor-arg index="2" value="重要人员"/>
</bean>
<bean id="user" class="com.wbg.springxmlbean.entity.User">
<property name="id" value="1"/>
<property name="age" value="18"/>
<property name="name" value="韦邦杠"/>
<!--name是属性名称 ref是对应的Bean-->
<property name="role" ref="role1"/>
</bean>
 

测试:

public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Role.xml");
UserService userService= (UserService) applicationContext.getBean("userService");
userService.setUser((User)applicationContext.getBean("user"));
System.out.println(userService.getUser());
}

2、装配集合

定义类:

package com.wbg.springxmlbean.entity;

import java.util.*;

public class ComplexAssembly {
private Long id;
private List<String> list;
private Map<String,String> map;
private Properties properties;
private Set<String> set;
private String[] array; @Override
public String toString() {
return "ComplexAssembly{" +
"id=" + id +
", list=" + list +
", map=" + map +
", properties=" + properties +
", set=" + set +
", array=" + Arrays.toString(array) +
'}';
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} public Set<String> getSet() {
return set;
} public void setSet(Set<String> set) {
this.set = set;
} public String[] getArray() {
return array;
} public void setArray(String[] array) {
this.array = array;
}
}

xml:

<bean id="complexAssembly" class="com.wbg.springxmlbean.entity.ComplexAssembly">
<property name="id" value="1"/>
<property name="list">
<!--List属性对应list元素进行装配,然后通过多个value设值-->
<list>
<value>value-list-1</value>
<value>value-list-2</value>
<value>value-list-3</value>
<value>value-list-4</value>
</list>
</property>
<property name="map">
<!--Map属性对应map元素进行装配,然后通过多个entry设值,只是entry包含有key和value值设值-->
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
<entry key="key4" value="value4"/>
</map>
</property>
<property name="properties">
<!--Properties属性,对应props进行装配,然后通过prop元素数值,只是prop有一个必填的key,然后设值-->
<props>
<prop key="prop1">value-prop-1</prop>
<prop key="prop2">value-prop-2</prop>
<prop key="prop3">value-prop-3</prop>
<prop key="prop4">value-prop-4</prop>
</props>
</property>
<property name="set">
<!--Set属性对应set元素进行装配,然后通过多个value设值-->
<set>
<value>value-set-1</value>
<value>value-set-2</value>
<value>value-set-3</value>
<value>value-set-4</value>
</set>
</property>
<property name="array">
<!--Array属性对应array元素进行装配,然后通过多个value设值-->
<array>
<value>value-array-1</value>
<value>value-array-2</value>
<value>value-array-3</value>
<value>value-array-4</value>
</array>
</property>
</bean>

测试:

  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Role.xml");
ComplexAssembly complexAssembly= (ComplexAssembly) applicationContext.getBean("complexAssembly");
System.out.println(complexAssembly);

3、装配用户和角色

类:MapUserRole

package com.wbg.springxmlbean.entity;

import java.util.Map;

public class MapUserRole {
private Map<User,Role> map; @Override
public String toString() {
return "MapUserRole{" +
"map=" + map +
'}';
} public Map<User, Role> getMap() {
return map;
} public void setMap(Map<User, Role> map) {
this.map = map;
}
}

xml:

 <bean id="u2" class="com.wbg.springxmlbean.entity.User">
<property name="id" value="1"/>
<property name="name" value="小邦哥"/>
<property name="age" value="20"/>
</bean>
<bean id="u1" class="com.wbg.springxmlbean.entity.User">
<property name="id" value="2"/>
<property name="name" value="邦杠"/>
<property name="age" value="21"/>
</bean>
<bean id="r1" class="com.wbg.springxmlbean.entity.Role">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="中级工程师"/>
<constructor-arg index="2" value="普通人员"/>
</bean>
<bean id="r2" class="com.wbg.springxmlbean.entity.Role">
<constructor-arg index="0" value="2"/>
<constructor-arg index="1" value="高级工程师"/>
<constructor-arg index="2" value="重要人员"/>
</bean>
<bean id="mapUserRole" class="com.wbg.springxmlbean.entity.MapUserRole">
<property name="map">
<map>
<entry key-ref="u1" value-ref="r1"/>
<entry key-ref="u2" value-ref="r2"/>
</map>
</property>
</bean>

测试:

demo:https://github.com/weibanggang/springXmlBean

Spring 通过XML配置装配Bean的更多相关文章

  1. Spring基础篇——通过Java注解和XML配置装配bean

    自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...

  2. Spring基础篇——通过Java注解和XML配置装配bean(转载)

      作者:陈本布衣 出处:http://www.cnblogs.com/chenbenbuyi 本文版权归作者和博客园共有,欢迎转载分享,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留 ...

  3. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  4. Spring实战2:装配bean—依赖注入的本质

    主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...

  5. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  6. spring+mybaits xml配置解析----转

    一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...

  7. spring的xml配置声明以及相应的问题处理

    spring的xml配置声明:  xml配置声明 Code 问题处理 问题1 xml报错: cvc-elt.1: Cannot find the declaration of element 'bea ...

  8. spring中用xml配置构造注入的心得

    spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...

  9. Spring框架(2)---IOC装配Bean(xml配置方式)

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

随机推荐

  1. MongoDB 学习(一)安装配置和简单应用

    一.安装和部署 1.服务端安装 1.官网下载(官方网站 https://www.mongodb.org/downloads/#production),傻瓜式安装,注意修改安装路径. 安装完成后的目录结 ...

  2. 来看看javaweb的自定义标签

    1.为什么需要自定义标签? jsp的简单标签其实就是jsp的自定义标签,主要作用就是移除jsp页面中的java代码,使得jsp页面只有标签和EL表达式,而没有java代码.利用自定义标签,可以使软件开 ...

  3. Effective C++ .06 阻止编译器自动生成函数以及被他人调用

    这节讲了下如何防止对象拷贝(隐藏并不能被其他人调用) 两种方法: 1. 将拷贝构造函数声明为private 并且声明函数但不进行定义 #include <iostream> #includ ...

  4. 从Pc转向H5开发遇到的适配问题思考

    1.首先说滚动条 移动端开发在不设置任何适配和viewport宽度的情况下,以iphone5为例:屏幕界面的逻辑分辨率是320x568,在谷歌浏览器的界面下屏幕的可视宽度是980px(谷歌设置的,每个 ...

  5. JS里的居民们7-对象和数组转换

    编码 学习通用的数据用不同的数据结构进行存储,以及相互的转换 对象转为数组: var scoreObject = { "Tony": { "Math": 95, ...

  6. sftp java 上传

    1. 注意问题 uri的格式: sftp://zhangsan:123456@10.10.10.10:22 dir问题 : 判断有没有 没有创建 然后进入 类推 config问题: StrictHos ...

  7. 我的gulp第一个程序

    以前都是单枪匹马的干,从没用过模块化的打包工具,加入新的团队后,模块化开发学到不少,尤其是工具的使用.团队目前较多的使用gulp,也是最流行的一款前端打包工具.最近Team开始尝试用gulp,我也只是 ...

  8. Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: Unable to load the mojo 'resources' (or one of its required components)

    1.异常提示: Description Resource Path Location Type Execution default-resources of goal org.apache.maven ...

  9. LinqToSql EntityFramework(ef)查看生成的sql语句

    var dc=new DBDataContext(); TextWriter tw = new StringWriter(); dc.Log = tw; var list = dc.News.Skip ...

  10. Python爬虫编程常见问题解决方法

    Python爬虫编程常见问题解决方法: 1.通用的解决方案: [按住Ctrl键不送松],同时用鼠标点击[方法名],查看文档 2.TypeError: POST data should be bytes ...