【Spring】—-常用属性注入及属性编辑器(三)
对于对象的注入,我们使用ref方式,可以指定注入的对象,下面看下属性的注入,以及当spring无法转换基本类型进行注入时,如何编写一个类似转换器的东西来完成注入。
【属性注入】
常见属性的注入:int,String,list,set,map的注入
【属性编辑器和作用】
- 将spring配置文件中的字符串转换成相应的Java对象
- spring内置了一些属性编辑器,也可以自定义属性编辑器
Bean1.java
package com.spring.tl.injection;import java.util.List;
import java.util.Map;
import java.util.Set;/*** Created by 滕柳 on 2018/6/4.*/
public class Bean1 {private String strValue;private int intValue;private List listValue;private Set setValue;private String[] arrayValue;private Map mapValue;public String getStrValue() {return strValue;}public void setStrValue(String strValue) {this.strValue = strValue;}public int getIntValue() {return intValue;}public void setIntValue(int intValue) {this.intValue = intValue;}public List getListValue() {return listValue;}public void setListValue(List listValue) {this.listValue = listValue;}public Set getSetValue() {return setValue;}public void setSetValue(Set setValue) {this.setValue = setValue;}public String[] getArrayValue() {return arrayValue;}public void setArrayValue(String[] arrayValue) {this.arrayValue = arrayValue;}public Map getMapValue() {return mapValue;}public void setMapValue(Map mapValue) {this.mapValue = mapValue;}
}
applicationContext-injection.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="bean1" class="com.spring.tl.injection.Bean1"><property name="strValue" value="Hello_tl"/><property name="intValue" value="123"/><property name="listValue" ><list><value>list1</value><value>list2</value></list></property><property name="arrayValue" ><list><value>arrayValue1</value><value>arrayValue2</value></list></property><property name="mapValue"><map><entry key="k1" value="v1"/><entry key="k2" value="k2"/></map></property><property name="setValue" ><set><value>set1</value><value>set2</value></set></property></bean></beans>
InjectionTest
package com.spring.tl.client;import com.spring.tl.injection.Bean1;import junit.framework.TestCase;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by 滕柳 on 2018/6/4.*/public class InjectionTest extends TestCase {private BeanFactory factory;@Overrideprotected void setUp() throws Exception{//factory=new ClassPathXmlApplicationContext("injection.xml");//一个配置文件//多个配置文件数组形式展示// String[] configLocations=new String[]{"injection.xml","applicationContext-editer.xml"}; // factory=new ClassPathXmlApplicationContext(configLocations);//多个配置文件统一展示factory=new ClassPathXmlApplicationContext("classpath*:/applicationContext-*.xml");}@Overrideprotected void tearDown() throws Exception{}@Testpublic void testInjection(){Bean1 bean=(Bean1) factory.getBean("bean1");System.out.println(bean.getStrValue());System.out.println(bean.getIntValue());System.out.println(bean.getListValue());System.out.println(bean.getSetValue());System.out.println(bean.getMapValue());System.out.println(bean.getArrayValue());}}
【自定义属性编辑器】
- 集成PropertyEditorSupport
- 覆盖setAsTest()方法
- 将自定义的属性编辑器注入到spring中
看此博客:spring2和spring4自定义属性编辑器的差别----处理日期格式
我们添加了日期的属性,不能自动注入到spring中,所以我们要转一下,来实践一下
Bean1.java
applicationContext-injection.xml
UtilDatePropertyEditor.java
package com.spring.tl.injection;import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*** Created by 滕柳 on 2018/6/4.*/
public class UtilDatePropertyEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text) throws IllegalArgumentException{System.out.println("-----setAsTest---"+text);try {Date date=new SimpleDateFormat("yyyy-MM-dd").parse(text);this.setValue(date);}catch (ParseException e){e.printStackTrace();throw new IllegalArgumentException(text);}}
}
applicationContext-editer.xml
【多配置文件的读取方式】
- 可以采用数组
- 可以采用*配置模式
看此博客:Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法
【减少spring的配置文件】
- 通过<bean>标签将公共的配置文件提取出来,然后指定<bean>标签中的abstract属性为true
- 在其他<bean>标签中指定其parent即可
代码实现:
Bean2中包含Bean3,Bean4,Bean5其中Bean3和Bean4中有相同的属性
Bean2.java
Bean3.java
Bean4.java
Bean5.java
1、正常配置:
applicationContext-injection.xml
<bean id="bean2" class="com.spring.tl.injection.Bean2"><property name="bean3" ref="bean3"></property><property name="bean4"><ref bean="bean4"></ref></property><property name="bean5" ref="bean5"></property></bean><bean id="bean3" class="com.spring.tl.injection.Bean3"><property name="id" value="100"></property><property name="age" value="22"></property><property name="name" value="张三"></property></bean><bean id="bean4" class="com.spring.tl.injection.Bean4"><property name="id" value="100"></property><property name="name" value="李四"></property><property name="age" value="23"></property><property name="sex" value="女"></property></bean><bean id="bean5" class="com.spring.tl.injection.Bean5"><property name="password"><value>123</value></property></bean>
2、减少spring的配置文件:
applicationContext-injection.xml
<bean id="bean2" class="com.spring.tl.injection.Bean2"><property name="bean3" ref="bean3"></property><property name="bean4"><ref bean="bean4"></ref></property><property name="bean5" ref="bean5"></property></bean><bean id="bean5" class="com.spring.tl.injection.Bean5"><property name="password"><value>123</value></property></bean>
applicationContext-common.xml
测试时要扫描全部的配置文件
【配置延迟加载】
1、spring配置文件中的beans默认情况下是及时加载的,有时候一些类加载耗时很厉害,我们可以通过配置将其设置为延迟加载。即延迟配置文件(bean)的初始化
2、spring配置文件中的beans节点有一个属性default-lazy-init和bean节点有一个属性是lazy-init,将这个属性的值设置为true时就可以实现延迟加载,放到beans节点上时会影响其下的所有子bean,放在bean上时只影响一个bean。
【Spring】—-常用属性注入及属性编辑器(三)相关推荐
- Autowired,Qualifier,Spring 按名称注入bean属性
Autowired,Qualifier,Spring 按名称注入bean属性 @Autowired@Qualifier("addItemDestination")private T ...
- spring 常用的注入方式有哪些?
spring 常用的注入方式有哪些? 1.xml中配置 bean 的申明.注册 节点注册 bean 节点的 factory-bean 参数指工厂 bean,factory-method 参数指定工厂方 ...
- 【Spring 基础篇三】属性注入与属性编辑器
上篇我们了解了一下applicationContext.xml的两种注入方式,本篇我们来了解一下关于属性的注入以及操作. 在敲代码的过程中,我们很容易遇到这样的问题,比如一个Lis ...
- Spring Bean的属性注入
在spring中bean的属性注入有两种 构造器注入 <bean id="car" class="nwtxxb.di.Car"><constr ...
- spring配置详解-属性注入(set方式)
Spring当中属性注入,关键的部分了,Spring中的属性注入,这个属性注入其实也算配置,Spring中的属性注入,那Spring的属性注入呢,一共有三种方式,我今天感觉方式有点多,Spring注入 ...
- 【Spring】- 属性注入方式
2019独角兽企业重金招聘Python工程师标准>>> House类:只有一个People属性,验证引用的ref引用bean的set方法注入方式 package com.zhiwei ...
- Java Spring-注解进行属性注入
2017-11-06 21:19:43 一.Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提 ...
- ASP.NET Core中使用IOC三部曲(二.采用Autofac来替换IOC容器,并实现属性注入)
上一篇ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自带的IOC容器) ,我们说过ASP.NET Core中自带的IOC容器是属于轻量级的,功能并不是很多,只是提供了基础 ...
- Spring的依赖注入方法
文章目录 Spring中依赖注入的方法 基于构造方法的注入 根据索引赋值 根据所属类型传值 根据所属类型传值(不推荐) 基于setter注入 基于接口的注入(不常用,这里不说明了) 拓展方式注入 P命 ...
最新文章
- 昨天你用的 YYYY-MM-dd 被捶了吗?
- Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介、使用方法之详细攻略
- 《Python3网络爬虫开发实战(第二版)》上市了!!!!
- css鼠标移入线条延中心伸长,css动画效果:鼠标移上去底部线条从中间往两边延伸 - 子成君-分享出去,快乐加倍!-旧版已停更...
- 【TypeScript系列教程06】基础类型
- 力扣28. 实现 strStr()(KMP算法,JavaScript)
- 别奢望大数据会为你做这10件事儿!
- python tkinter linux,用于Python和Tkinter的Linux上的字体管理
- C#、TypeScript之父Anders Hejlsberg:“会用Excel就是程序员 ”
- via浏览器原始css,简约却不简单—via浏览器
- 仿网易严选微信小程序
- windows 下 tomcat 开机自启动
- bugly android sdk,Bugly SDK 集成使用
- BLDC反电势过零检测计算
- 随笔:信息系统项目管理师(软考高级2023)考试指南
- 颜色在计算机中的存储和显示详解
- 不一样的 Python 课【王的机器出品】
- 佟年计算机天才不会打游戏,亲爱的热爱的:Gun神带佟年开黑,网友:甜蜜游戏时间...
- 3D目标检测(单目)D4LCN论文复现(paddlepaddle)
- 使用Unity3D编写ARPG游戏——角色属性的定义与实现(二)
热门文章
- STM32定时器实现5秒周期串口通信,2秒LED灯闪烁
- HTML:实现div中块元素在一行显示
- unity动态切换横竖屏采坑记录
- 面试官:你还有什么问题要问的吗?
- 【知识图谱】Py2neo操作Neo4j使用教程
- 从“3A ”发展看中国云计算产业竞争格局变化
- android 读写project.properties,Android project.properties与default.properties
- PMP学习笔记之第一章引论
- 国内外知识图谱相关公司简介
- 从17万暴涨到1144万,猛翻66倍...