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

前言

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 {
	
	@Test
	public 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>
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值