Spring 通过XML配置装配Bean

使用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;@Overridepublic 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;}
}

View Code

User:

package com.wbg.springxmlbean.entity;public class User {private int id;private Role role;@Overridepublic 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;
}

View Code

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;@Overridepublic 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;}
}

View Code

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;@Overridepublic String toString() {return "MapUserRole{" +"map=" + map +'}';}public Map<User, Role> getMap() {return map;}public void setMap(Map<User, Role> map) {this.map = map;}
}

View Code

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>

View Code

测试:

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

posted @ 2018-12-13 19:14 韦邦杠 阅读(...) 评论(...) 编辑 收藏

Spring 通过XML配置装配Bean相关推荐

  1. SSM框架笔记06:初探Spring——采用XML配置方式

    初探Spring--采用XML配置方式   Spring框架是一个轻量级的企业级开发的一站式解决方案.所谓解决方案就是可以基于Spring解决Java EE开发的所有问题.Spring框架主要提供了I ...

  2. Spring AOP XML配置及注解配置

    一.XML配置切面类 1.日志切面类 //切面类注解配置加两个注解 @Aspect @Component public class LoggerAspect {public Object log(Pr ...

  3. spring 使用XML配置开发Spring AOP

    XML方式开发AOP与注解开发原理是相同的,所以这里主要介绍一些用法即可.这里需要在XML中引入AOP的命名空间,所以先来了解一下AOP可配置的元素 代码清单:切面类 package com.ssm. ...

  4. Shiro Spring 集成xml配置

    JavaSE 应用: spring-shiro.xml 提供了普通 JavaSE 独立应用的 Spring 配置: <!-- 缓存管理器 使用Ehcache实现 --> <bean ...

  5. Spring基于xml自动装配

    基于xml自动装配 手动装配 自动装配 首先写两个类Dept类和Emp类. Dept类: package com.Keafmd.spring5.autowire;/*** Keafmd** @Clas ...

  6. spring security xml配置官方详解

    6. Security Namespace Configuration 6.1 Introduction 自2.0版本的spring框架以来,命名空间配置已可用. 它允许您使用来自附加XML模式的元素 ...

  7. Spring实战01——自动化装配bean

    自动装配bean 的四种方式: 首先定义一个类,使用@Configuration 标注类成为配置类,然后有下面四种方式: 1.配置类使用@ComponentScan 注解扫描指定包下的类,默认是扫描同 ...

  8. spring mvc xml配置

    首先来看web的xml配置 <!-- 改变接收编码,表单提交用post方法 -->   <filter>         <filter-name>encoding ...

  9. Spring 基于xml配置方式的AOP

    我们具体用代码来说明: 1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculato ...

  10. Java Spring Beans.xml里的Bean定义是如何被解析出来的

    ClassPathXmlApplicationContext的构造函数里,一部分逻辑是给各个成员变量赋上初始值: 然后是执行refresh操作. 获取ConfigurableListableBeanF ...

最新文章

  1. pgjdbc源码分析
  2. 玩转可视化--来聊聊地图投影的学问
  3. 解决scrollViewDidScroll do not work的方法
  4. EF Core查询标签TagWith
  5. html5增强的页面元素
  6. c++局部对象是什么_什么是Java内部类?
  7. 解读 Kotlin/Native 技术预览版
  8. 5.4Irvine32库
  9. 几乎没人教你的用poi导出如此复杂的考勤表
  10. java pdf加水印 性能_java 实现 PDF 加水印功能
  11. 51单片机入门——蜂鸣器
  12. jzxx2600野猫过生日
  13. Android The emulator process for AVD XXX has terminated.
  14. MIB Browser使用方法
  15. WPS中将公式居中,序号在最右边
  16. 学习 Redis Connection(连接)
  17. 关于Java RDP协议实现远程桌面连接的开源项目properjavardp .
  18. Qt开发过程中技巧知识点汇总
  19. DIY打造多合1集成SATA驱动Windows XP系统安装光盘
  20. ARM335X参考手册nbsp;中文

热门文章

  1. 控制器布局 php,PhalconPHP视图/布局/控制器
  2. 计算机系统基础栈,计算机系统基础 (一): 程序的表示, 转换与链接 (第七周小测验)...
  3. 计算机组成原理2套题,计算机组成原理试卷2套含答案(大学期末复习资料).doc...
  4. 和 对比_Yeezy350V2新灰橙真假对比
  5. git add commit checkout 工作区 暂存区 远程仓库 区别
  6. thymeleaf使用
  7. lsblk命令 – 查看系统的磁盘
  8. consul服务发现与注册于配置 (mac版为例)
  9. 小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
  10. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_4_File类的构造方法...