Spring 学习笔记 (三)
19. 深入理解依赖关系配置
(1) 注入其他bean的属性值
配置片段:
<!-- 将其他bean的属性注入给当前正在定义bean的属性 -->
<bean id="son1" class="Bean.superIOCparam.Son">
<property name="age">
<!--以下是访问bean属性的简单方式,这样可以将person这个 bean的son属性的age属性 赋值 给son1这个bean的age属性-->
<!-- org.springframework.beans.factory.config.PropertyPathFactoryBean这个类是固定的 -->
<bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean>
<!-- 将其他bean的属性定义成一个bean -->
<!-- 以下将会获得结果son2,它将是person bean的son的数值-->
<!-- 定义son2这个bean,这个bean是来自名字为person这个bean的son属性 -->
<bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- targetBeanName表示需要从id为person的bean获取属性 -->
<property name="targetBeanName" varlue="person" />
<!-- propertyPath表示需要从id为person的bean获取属性,需要获取的属性为son -->
<property name="propertyPath" varlue="son" />
</bean>
<!-- 使用嵌套bean,定义一个bean,并将该bean的某个属性 定义成一个新bean(theAge2) -->
<bean id="theAge2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- targetObject 中设置一个bean,表示需要从这个bean获取属性 -->
<property name="targetObject" >
<bean class="xxx.xxx.Person">
<property name="age" value="30" />
</bean>
</property>
<!-- 定义需要获取的属性名称 -->
<property name="propertyPath" value="age" />
</bean>
(2) 注入其他bean的Field值,使用FieldRetrievingFactoryBean
配置片段:
<!-- 将指定的类的静态Field设置成bean的属性值 --> <bean id="son" class="..."> <property name="age"> <!-- id指定了哪个Field的值 将会被设置给id="son"的bean --> <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> </property> </bean> <!-- 将其他bean的Field定义成一个bean --> <bean id="theAge1" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <!-- targetClass 设置Field所在的类,targetObject,当目标对象时使用(代替targetClass) --> <property name="targetClass" value="java.sql.Connection" /> <!-- targetField指定目标类的目标Field --> <property name="targetField" value="TRANSACTION_SERIALIZABLE" /> </bean> <!-- 将静态Field定义成一个bean的简单写法 --> <bean id="theAge2" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE" /> </bean>
(3) 注入其他Bean的方法返回值 MethodInvokingFactoryBean. 获得的方法的返回值既可以注入到指定的Bean实例,
也可以直接定义成Bean实例
配置片段:
<!-- 提供方法的bean --> <bean id="valueGenerator" class="xxx.xxx.valueGenerator" /> <!-- 将一个bean的方法 返回值 注入 新bean的 age属性 --> <bean id="son1" class="xxx.xxx.son"> <property name="age"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="valueGenerator" /> <property name="targetMethod" value="getValue" /> </bean> </property> </bean> <!-- 上面 是调用非静态类对象的getValue()这种无参方法 --> <!-- 调用静态类的静态方法,静态方法的返回值直接 定义成bean --> <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="java.lang.System" /> <property name="targetMethod" value="getProperties" /> </bean> <!-- 调用无参 静态类的静态方法 创建bean name="staticMethod",value=静态类.静态方法--> <bean id="myBean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="xxx.MyStaticClass.myStaticMethod" /> </bean> <!-- 有参数的方法调用,返回值 配置成一个bean --> <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <!-- 目标bean,确定调用哪个bean的方法 --> <property name="targetObject" ref="sysProps" /> <!-- 确定目标方法,确定调用bean的哪个方法 --> <property name="targetMethod" value="getProperty" /> <!-- 确定调用目标方法的参数 相当于调用getProperty方法,传递参数"java.version" --> <property name="arguments"> <list> <value>java.version</value> </list> </property> </bean>
相关推荐
struts+spring+hibernate3+webligic812环境配置备忘录
NULL 博文链接:https://jackyin5918.iteye.com/blog/1912089
NULL 博文链接:https://java-doom.iteye.com/blog/2126725
Spring2.5.3+Struts2.0.11.1+Hibernate3.2.6整合备忘 (转载)
日常SSHF开发中,经常因为Jar包问题头痛。本文档收集了struts2+spring2.5+hibernate+freemarker 所用到的最基本依赖包,对解决Jar包问题非常有帮助。
myfaces+ajax4jsf+spring+jpa+chartCreate 配置备忘 完整eclipse项目源代码
包括mybatis、spring、spring-boot、hbase、hive、impala、guava、jdk等 包含的模块 spring-demo mybatis-demo spring-mybatis-jetty springboot-demo opensource-utils spring-demo spring依赖注入相关demo,包括...
这是个人练习的spring配置定时任务方面的例子,备忘用,不建议下载
2021年Spring该存储库将(希望)跟踪我在2021年Spring学期的笔记。 我还将尝试记录所做的任何修订以供将来参考。最初设定每个文件夹都包含一个班级的相关注释。 注释使用Markdown编写,并在必要时添加LaTeX。 编辑器...
基于SpringBoot+Vue的备忘录系统源码+sql数据库(课程大作业).zip 基于SpringBoot+Vue的备忘录系统源码+sql数据库(课程大作业).zip 基于SpringBoot+Vue的备忘录系统源码+sql数据库(课程大作业).zip 【资源说明】 项目...
spring mvc freemarker 自定义标签demo spring mvc 通过自定义的标签在jsp页面调用,初学,记录下做个备忘
这是spring 测试的示例,个人备忘用!
基于springboot+vue的备忘录管理系统源码+数据库文件(课程设计).zip已获导师指导并通过的97分的高分大作业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 基于springboot+vue...
在应用程序网络中添加有用的ReactJ(前端)和Spring Boot(后端)的修饰符和高级注释。 目录 Propos du Projet 事前和事后证明书均以书面形式发表,有能力的人可享有恢复原状的权利。 注释中的“有附加条件的注释...
Android大作业,基于bomb的云备忘录项目源码.zip
开机 我介绍 Spring Boot 的演讲的存储库。 大多数谈话都是实时代码,所以这些实际上只是保存点和备忘单。
2018Spring网络应用程序 我们将使用react来构建一个习惯跟踪应用程序(例如,您可以使用此Web应用程序跟踪您的新年愿望,其中一个是学习React吗?) 技术: 资源: 备忘单: es6 React 轴距 Lodash 日程 1级 ...
前端大作业基于SpringBoot + Vue的备忘录系统源码+数据库 项目结构 sql => 数据库 memo-java => 后端 memo-vue => 前端 开发环境 MySQL 5.7 JDK 8 技术栈 前端 vue2 Element UI 后端 SpringBoot MybatisPlus
本文还是使用VUE+ Spring Boot的方式来进行管理的,但在系统建设过程当中也考虑了许许多多信息安全的保护。 管理员具有系统用户管理、客户服务管理、城市管理、操作员管理、联系人管理、备忘录管理、客户管理、业务...