Spring框架中级联赋值(外部属性注入)以及内部属性注入

  • 前言
  • 级联赋值
    • 1、对上述外部`Bean`配置文件进行修改:
    • 2、级联赋值第二种写法
  • 内部`bean`属性注入

前言

Spring框架中存在外部属性注入的方式完成实例创建,修改一下配置文件的创建方式,可以形成另外两种属性注入形式,级联赋值和内部属性注入。

《Spring框架XML配置文件使用外部Bean属性注入》

级联赋值

所谓级联赋值,实际是XML配置文件使用外部Bean属性注入的时候,在外部Bean中注入属性。

1、对上述外部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"><!-- bean的级联赋值 -->
<bean id="userService" class="com.action.spring.service.UserService"><property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.action.spring.dao.UserDaoImpl"><property name="uname" value="超级用户"></property>
</bean></beans>

详细的外部属性注入见《Spring框架XML配置文件使用外部Bean属性注入》

在外部被注入的Bean中,可注入自己的属性。此种做法的前提都是在目标类中,都已经创建了相应属性的set方法。

2、级联赋值第二种写法

级联赋值出了第一种在外部引用bean中直接注入属性之外,还可以直接使用.来直接过去属性进行注入。

User类

package com.action.spring;public class User {private Book book;public void setBook(Book book) {this.book = book;}public void add() {System.out.println("Spring...");book.testBook();}}

Book类

package com.action.spring;public class Book {private String bname;private String bauthor;private String address;public void setBname(String bname){this.bname = bname;}public void setBauthor(String bauthor){this.bauthor = bauthor;}public void setAddress(String address){this.address = address;}public void testBook(){System.out.println(bname+"::"+bauthor+"::"+address);}
}

xml文件的配置

<bean id="user" class="com.action.spring.User"><property name="book" ref="book"></property><property name="book.bname" value="级联名称"></property>
</bean>
<bean id="book" class="com.action.spring.Book"></bean>

此时运行的时候会报错:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in file [E:\workspace\Springwork\src\config\bean1.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'book.bname' of bean class [com.action.spring.User]: Nested property in path 'book.bname' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'book' of bean class [com.action.spring.User]: Bean property 'book' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

原因是在User类中没有可用获取Book属性的get方法。需添加:

public Book getBook() {return book;
}

测试运行结果

public class testSpring5 {@Testpublic void testAdd() throws Exception {ApplicationContext context =new FileSystemXmlApplicationContext("src/config/bean1.xml");User user = context.getBean("user",User.class);System.out.println(user);user.add();}}

结果:

内部bean属性注入

顾名思义,就是在注入属性的时候,直接在属性标签中再创建一个标签并注入属性。

<bean id="userService" class="com.action.spring.service.UserService"><property name="userDao"><bean id="userDaoImpl" class="com.action.spring.dao.UserDaoImpl"><property name="uname" value="超级用户"></property></bean></property>
</bean>

Spring框架中级联赋值(外部属性注入)以及内部属性注入相关推荐

  1. Spring框架中集合属性为对象的注入方法

    Spring框架中集合属性为对象的注入方法 前言 创建基础类 创建`Course`类 编写XML配置文件 创建测试类 执行结果 前言 在集合的属性注入中,如果注入属性为普通类型(String.int) ...

  2. Spring框架XML配置文件使用外部Bean属性注入

    Spring框架XML配置文件使用外部Bean属性注入 (1)创建两个类service类和dao类 (2)在service中调用dao里面的方法 (3)使用Spring框架进行调用 (4)创建测试类 ...

  3. Spring框架中提取list集合类型属性注入

    提取list集合类型属性注入 前言 引入名称空间 编写`xml`配置文件 运行结果 前言 对于某一个类型属性通用性较高的情况下,可以单独的提取出来,给需要的bean进行引用. 有关类的创建见<S ...

  4. Spring框架中XML配置文件注入集合(数组、LIST、MAP、SET)属性

    Spring框架中XML配置文件注入集合属性 前言 创建测试类与属性 配置XML配置文件 建立调用类 调用结果 前言 某些类的属性是可能是集合,包括:数组.LIST.MAP.SET等集合,在Sprin ...

  5. Spring框架中XML配置特殊属性注入

    Spring框架中XML配置特殊属性注入 前言 创建测试类 其他类型属性 前言 Spring框架中,在通过set方式进行属性注入时,可能会遇到某些特殊字符的注入,例如:null和某些标签字符" ...

  6. Spring框架中的控制反转和依赖注入

    控制反转: 控制反转是用来降低代码之间的耦合度的,基本思想就是借助"第三方"实现具有依赖对象的解耦. 为什么需要控制反转,因为项目中对象或多或少存在耦合.控制反转的关键在于Ioc容 ...

  7. Spring框架中的Bean

    1.什么是Bean? 在Spring框架中,Bean是指一个由Spring容器管理的对象.这个对象可以是任何一个Java类的实例,例如数据库连接.业务逻辑类.控制器等等.Bean实例的创建和管理是由S ...

  8. 在Spring框架中使用SQL存储过程

    Spring框架也支持对SQL存储过程的调用,SQL存储过程是一组预先定义好的SQL语句,并存储到数据库管理系统中,外部程序可以直接调用执行.本课主要讨论在Spring框架中应用程序如何调用MySQL ...

  9. Spring配置中的bean直接引用其它bean的属性值

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! prin ...

最新文章

  1. 车联网APP,安全设施薄弱的山寨品
  2. 特殊用户邮箱附件大小设置
  3. 数据库中INFORMATION_SCHEMA的说明及使用
  4. zhcon解决Ubuntu命令行模式乱码问题
  5. Memcached深度分析【zz】
  6. 内部排序选择、冒泡、插入、希尔、快速、归并、堆排序原理概要和实现
  7. Git使用技巧(1)-- 配置【持续更新】
  8. 大话异步与并行(一)
  9. SAP APF框架错误消息Filter is too complex的处理
  10. 安卓APP_ 其他(1) —— 程序的签名打包并在手机上运行
  11. linux运维和3dmax哪个简单,牛逼运维常用的工具系列-2
  12. Java内存区域(运行时数据区域)和内存模型(JMM)
  13. Mybatis多对多,复杂增删改查(特殊需求循环插入,分组查询)
  14. uniapp打包成html5包个ios壳,HBuilder之uni-app打包App方法
  15. 如何删除网关的session_微服务安全认证架构是如何演进而来的?坐好小板凳一起来听一听...
  16. 网站发布助手V1.1 (去年写的简单小工具)
  17. Vue.js——登录界面实现插入背景
  18. 【莫烦Python】Matplotlib Python 画图教程 plot in plot图中图
  19. java 高效列转行,java 列转行
  20. 杰理之GSENSOR【篇】

热门文章

  1. PAT 1086 就不告诉你
  2. python3 rrdtool 使用
  3. OpenShift:外国的免费云平台
  4. 【索引】联合索引的基本知识
  5. Perceptual:英特尔感知计算挑战赛 正式启动
  6. 【Spring 持久层】Spring 与 Mybatis 整合
  7. 做csdn图片水印中最亮的崽
  8. 跟我一起学jQuery——第一集
  9. 走进我的交易室08_有条理的交易者
  10. 手把手教你做酷炫的数据可视化大屏,零基础的你仅需6步