spring-第九篇之高级依赖关系配置
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-第九篇之高级依赖关系配置的更多相关文章
- Spring框架学习之高级依赖关系配置(一)
上篇文章我们对Spring做了初步的学习,了解了基本的依赖注入思想.学会简单的配置bean.能够使用Spring容器管理我们的bean实例等.但这还只是相对较浅显的内容,本篇将介绍bean的相关更高级 ...
- Spring框架学习之高级依赖关系配置(二)
紧接着上篇内容,本篇文章将主要介绍XML Schema的简化配置和使用SpEL表达式语言来优化我们的配置文件. 一.基于XML Schema的简化配置方式 从Spring2.0以来,Spring支持使 ...
- Spring第九篇【Spring与Hibernate整合】
前言 前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibernate进行整合 Spring和Hibernate整合的关键点: SessionFact ...
- Spring boot starter pom的依赖关系说明
Spring Boot 通过starter依赖为项目的依赖管理提供帮助.starter依赖起始就是特殊的maven依赖,利用了传递依赖解析,把常用库聚合在一起,组成了几个为特定功能而定制的依赖. sp ...
- Spring应用教程-3 依赖关系配置
注:组件与组件之间的耦合,采用依赖注入管理,但普通的JavaBean属性值,应直接在代码中设置. 1. 注入其他Bean的属性值 我们分析一下,Bean_A的一个属性要依赖Bean_B的一个属性值.这 ...
- spring各个包之间的依赖关系
从图中可以看到: 1.spring core,spring beans被其他较多包依赖,spring aop,spring context,spring expression分别被两个包依赖,而spr ...
- Jenkins job之间依赖关系配置(联动构建)
使用场景: 想要在某APP打新包之后,立即执行自动化测试的job来验证该新包.比如Job A 执行完执行Job B ,如下图所示,如何建立依赖呢? 主要有两种方法: 1.配置上游依赖: 2.配置下游依 ...
- Jenkins-job之间依赖关系配置
使用场景: 想要在某APP打新包之后,立即执行自动化测试的job来验证该新包. 比如Job A 执行完执行Job B ,如下图所示,如何建立依赖呢? 1.配置上游依赖 构建触发器-配置如下信息: 选择 ...
- SpringMvc+Spring+Mybatis的jar包依赖关系图
随机推荐
- k3 cloud中获取年月日
日期类型字段元素.Date.Year(获取年) 日期类型字段元素.Date.Month(获取月)日期类型字段元素.Date.Day(获取天)
- ES6——面向对象应用
面向对象应用——React 特点: 1.组件化(模块化) --- class(一个组件就是一个class) 2.强依赖与JSX (JSX==babel==browser.js 是JS ...
- kali优化配置(3)--工具箱
1.netcat 收集信息.Telnet/banner.传输文本信息.连接服务器端口. *通过IP,连接服务器端口: *信息通信: *重定向符号:> (e.g:>>ps.txt:重定 ...
- ffmpeg知多少~~~
一.ffmpeg安装: https://jingyan.baidu.com/article/f7ff0bfcd64cea2e26bb1334.html 二.ffmpeg视频处理(包括各种视频流处理 ...
- javaweb各种框架组合案例(八):springboot+mybatis-plus+restful
一.介绍 1. springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之 ...
- 获取用户真实IP:(模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP)
模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP 192.168.109.137 :nginx01(充当第一层代理==F5)192.168.109.138 :nginx02(二 ...
- 11JSP基础
1.Jsp基础 1.1 简介 Jsp,全称 Java Server Page java服务页面,能提供java服务的页面 jsp vs html html: 由html标签组成的,输出静态内容. js ...
- (4)Linux(ubuntu)下配置Opencv3.1.0开发环境的详细步骤
Ubuntu下配置opencv3.1.0开发环境 1.最近工作上用到在Ubuntu下基于QT和opencv库开发应用软件(计算机视觉处理方面),特把opencv的配置过程详细记录,以供分享 2.步骤说 ...
- [CSP-S模拟测试]:building(模拟)
题目传送门(内部题64) 输入格式 第一行有一个整数$id$,表示测试点编号.第二行有四个整数$n,m,k,q$.然后有$k$行,每一行有四个整数$x_{i_1},y_{i_1},x_{i_2},y_ ...
- lnmp环境下 tp3.2 not found
最近将一个lamp环境下使用tp3.2 开发的项目迁移到本地了, 但是在打开项目的时候,提示 not found,经过多方面查找发现是伪静态问题,解决方法如下: 在nginx 域名配置文件我这里是[v ...