1、关于配置文件一些使用

组件与组件之间的耦合,采用依赖注入管理;基本类型的成员变量值,应该直接在代码中设置。

2、获取其他bean的属性值

PorpertyPathFactoryBean用来获取目标bean的属性值(实际上就是它的getter方法的返回值),获得的值可以注入给其他bean,也可以直接定义成新的bean。使用PorpertyPathFactoryBean来调用其他bean的getter方法需要指定如下信息:

调用哪个对象:由PorpertyPathFactoryBean的setTargetObject(Object targetObject)的方法指定。

调用哪个getter方法:由PorpertyPathFactoryBean的setPropertyPath(String propertyPath)方法指定。

举个例子:

Person.java

package com.lfy.bean;

public class Person {

    private int age;
private Son son; public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

Son.java

package com.lfy.bean;

public class Son {

    private int age;

    public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Son[age="+age+"]";
}
}

beans.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!--下面配置定义一个将要被引用的目标bean-->
<bean id="person" class="com.lfy.bean.Person">
<property name="age" value="30"/>
<property name="son">
<!-- 使用嵌套Bean定义setSon()方法的参数值 -->
<bean class="com.lfy.bean.Son">
<property name="age" value="11" />
</bean>
</property>
</bean> <!-- 将指定Bean实例的getter方法返回值定义成son1 Bean -->
<bean id="son1" class=
"org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- 确定目标Bean,指定son1 Bean来自哪个Bean的getter方法 -->
<property name="targetBeanName" value="person"/>
<!-- 指定son1 Bean来自目标bean的哪个getter方法,son代表getSon() -->
<property name="propertyPath" value="son"/>
</bean> <!-- 下面定义son2 Bean -->
<bean id="son2" class="com.lfy.bean.Son">
<property name="age">
<!-- 使用嵌套Bean为调用setAge()方法指定参数值 -->
<!-- 以下是访问指定Bean的getter方法的简单方式,
person.son.age代表获取person.getSon().getAge()-->
<bean id="person.son.age" class=
"org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean> <!-- 将基本数据类型的属性值定义成Bean实例 -->
<bean id="theAge" class=
"org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- 确定目标Bean,表明theAge Bean来自哪个Bean的getter方法的返回值 -->
<property name="targetBeanName" value="person"/>
<!-- 使用复合属性来指定getter方法。son.age代表getSon().getAge() -->
<property name="propertyPath" value="son.age"/>
</bean> <!-- 将基本数据类型的属性值定义成Bean实例 -->
<bean id="theAge2" class=
"org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- 确定目标Bean,表明theAge2 Bean来自哪个Bean的属性。
此处采用嵌套Bean定义目标Bean -->
<property name="targetObject">
<!-- 目标Bean不是容器中已经存在的Bean, 而是如下的嵌套Bean-->
<bean class="com.lfy.bean.Person">
<property name="age" value="30"/>
</bean>
</property>
<!-- 指定theAge2 Bean来自目标bean的哪个getter方法,age代表getAge() -->
<property name="propertyPath" value="age"/>
</bean> <!-- son1的简化配置 -->
<util:property-path id="son3" path="person.son"/> <!-- son2的简化配置 -->
<bean id="son4" class="com.lfy.bean.Son">
<property name="age">
<util:property-path path="person.son.age"/>
</property>
</bean> <!-- theAge的简化配置 -->
<util:property-path id="theAge3" path="person.son.age"/>
</beans>

SpringTest.java

package com.lfy.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lfy.bean.Person; /**
*
* @author lfy
*
*/
public class SpringTest { public static void main(String[] args) {
//创建spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
System.out.println("系统获取son1:"+ctx.getBean("son1"));
System.out.println("系统获取son2:"+ctx.getBean("son2"));
System.out.println("系统获取theAge:"+ctx.getBean("theAge"));
System.out.println("系统获取theAge:"+ctx.getBean("theAge2"));
//简化配置
System.out.println("系统获取son3:"+ctx.getBean("son3"));
System.out.println("系统获取son4:"+ctx.getBean("son4"));
System.out.println("系统获取theAge3:"+ctx.getBean("theAge3"));
} }

运行结果:

总结:<util:property-path.../>元素可以作为PropertyPathFactoryBean的简化配置,需要使用该元素,必须在配置文件中声明util:命名空间。其配置时指定的两个属性

id:该属性指定将getter方法的返回值定义成名为id的bean实例,如本例的son3。

path:该属性指定将哪个bean实例、哪个属性(可以是复合属性)暴露出来。

3、获取Field字段值

FieldRetrievingFactoryBean,可以访问类的静态Field或对象的实例Field值。使用FieldRetrievingFactoryBean访问Field分两种情形:

1》要访问的Field是静态Field,需要指定

调用哪个类:由FieldRetrievingFactoryBean的setTargetClass(String targetClass)方法指定。

访问哪个Field:由FieldRetrievingFactoryBean的setTargetField(String targetField)方法指定。

2》要访问的Filed是实例Field(要求实例的Field使用public控制访问权限,没太大用处),需要指定

调用哪个对象:由FieldRetrievingFactoryBean的setTargetObject(String targetObject)方法指定。

访问哪个Field:由FieldRetrievingFactoryBean的setTargetField(String targetField)方法指定。

4、获取方法返回值

MethodInvokingFactoryBean工厂bean,使用MethodInvokingFactoryBean两种情形:

1》要访问的是静态方法,需要指定

调用哪个类:由MethodInvokingFactoryBean的setTargetClass(String targetClass)方法指定。

调用哪个方法:由MethodInvokingFactoryBean的setTargetMethod(String targetMethod)方法指定。

调用方法的参数:由MethodInvokingFactoryBean的setTargetArguments(Object[] arguments)方法指定。方法无参数该配置可以省略。

2》要访问的是实例方法,需要指定

调用哪个对象:由MethodInvokingFactoryBean的setTargetObject(Object targetObject)方法指定。

调用哪个方法:由MethodInvokingFactoryBean的setTargetMethod(String targetMethod)方法指定。

调用方法的参数:由MethodInvokingFactoryBean的setTargetArguments(Object[] arguments)方法指定。方法无参数该配置可以省略。

spring-第九篇之高级依赖关系配置的更多相关文章

  1. Spring框架学习之高级依赖关系配置(一)

    上篇文章我们对Spring做了初步的学习,了解了基本的依赖注入思想.学会简单的配置bean.能够使用Spring容器管理我们的bean实例等.但这还只是相对较浅显的内容,本篇将介绍bean的相关更高级 ...

  2. Spring框架学习之高级依赖关系配置(二)

    紧接着上篇内容,本篇文章将主要介绍XML Schema的简化配置和使用SpEL表达式语言来优化我们的配置文件. 一.基于XML Schema的简化配置方式 从Spring2.0以来,Spring支持使 ...

  3. Spring第九篇【Spring与Hibernate整合】

    前言 前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibernate进行整合 Spring和Hibernate整合的关键点: SessionFact ...

  4. Spring boot starter pom的依赖关系说明

    Spring Boot 通过starter依赖为项目的依赖管理提供帮助.starter依赖起始就是特殊的maven依赖,利用了传递依赖解析,把常用库聚合在一起,组成了几个为特定功能而定制的依赖. sp ...

  5. Spring应用教程-3 依赖关系配置

    注:组件与组件之间的耦合,采用依赖注入管理,但普通的JavaBean属性值,应直接在代码中设置. 1. 注入其他Bean的属性值 我们分析一下,Bean_A的一个属性要依赖Bean_B的一个属性值.这 ...

  6. spring各个包之间的依赖关系

    从图中可以看到: 1.spring core,spring beans被其他较多包依赖,spring aop,spring context,spring expression分别被两个包依赖,而spr ...

  7. Jenkins job之间依赖关系配置(联动构建)

    使用场景: 想要在某APP打新包之后,立即执行自动化测试的job来验证该新包.比如Job A 执行完执行Job B ,如下图所示,如何建立依赖呢? 主要有两种方法: 1.配置上游依赖: 2.配置下游依 ...

  8. Jenkins-job之间依赖关系配置

    使用场景: 想要在某APP打新包之后,立即执行自动化测试的job来验证该新包. 比如Job A 执行完执行Job B ,如下图所示,如何建立依赖呢? 1.配置上游依赖 构建触发器-配置如下信息: 选择 ...

  9. SpringMvc+Spring+Mybatis的jar包依赖关系图

随机推荐

  1. 数据写入到Excel,模板样式复杂

    先整理好Excel模板,如: 接下来在程序获取上面整理好的Excel模板并替换关键字就可以了public ActionResult SummaryStatistics() public ActionR ...

  2. Maya2014下载安装与激活

    目录 1. 更多推荐 2. 下载地址 2.1. OneDrive 2.2. 其他下载地址 3. 激活步骤 1. 更多推荐 其他Maya版本的下载与激活:https://www.cnblogs.com/ ...

  3. VS 2019编辑含有资源文件.resx的项目时提示MSB3086 任务未能使用 SdkToolsPath 或注册表项找到“al.exe”

    环境: Win10 X64, VS2019 错误提示: 错误 MSB3086 任务未能使用 SdkToolsPath“”或注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  4. 【转】WebRTC之RTCP

    转自:https://blog.csdn.net/momo0853/article/details/88051312#RTPFBTransport_layer_FB_messagesNACKTrans ...

  5. Oracle 附加日志(supplemental log)

    参考资料: 1.https://blog.csdn.net/li19236/article/details/41621179

  6. Thinkphp 请求和响应

    一. Request对象获取方法 1. request() 助手函数获取 2. think\Request 类获取 3.利用框架注入Request对象  Request方法时单利方法 在think框架 ...

  7. python对象之__call__方法

    先看示例,然后啥都明白了 class Student(): def __call__(self, *args, **kwargs): print('__call__方法被调用', *args) cla ...

  8. BZOJ 1492: [NOI2007]货币兑换Cash 斜率优化 + splay动态维护凸包

    Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个 ...

  9. [CSP-S模拟测试]:Market(背包DP)

    题目描述 在比特镇一共有$n$家商店,编号依次为$1$到$n$.每家商店只会卖一种物品,其中第$i$家商店的物品单价为$c_i$,价值为$v_i$,且该商店开张的时间为$t_i$. $Byteasar ...

  10. [CSP-S模拟测试]:折纸(模拟)

    题目描述 小$s$很喜欢折纸.有一天,他得到了一条很长的纸带,他把它从左向右均匀划分为$N$个单位长度,并且在每份的边界处分别标上数字$0\sim n$.然后小$s$开始无聊的折纸,每次他都会选择一个 ...