Spring框架中级联赋值(外部属性注入)以及内部属性注入
前言
Spring
框架中存在外部属性注入的方式完成实例创建,修改一下配置文件的创建方式,可以形成另外两种属性注入形式,级联赋值和内部属性注入。
级联赋值
所谓级联赋值,实际是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>