Spring基础类型注入演示案例
- 原生类型(Primitive ): boolean、byte、char、short、int 、float、long、double
- 标量类型(Scalar ): Number、Character、Boolean、Enum、Locale、Charset、Currency、Properties、UUID
- 常规类型(General ): Object、String、Timezone、Calendar、Optional Spring 类型:Resource、Inputsource、Formatter
部分注入案例
- String字符串注入
- Int类型注入
- 数组注入
- List注入
- Map注入
- 枚举注入
- Optional注入
- Resource注入
配置文件application.properties
#String字符串注入
rumen.z=11#int类型注入
rumenz.i=123
#数组类型注入
rumenz.arr=1,2,3,4
#List类型注入
rumenz.list=1,2,3,4,5
#Map类型注入
rumenz.map={"code":200,"msg":"ok","data":"123"}
#枚举类型注入
rumenz.enum=RED
#Resource类型注入
rumenz.resource=test.txt
需要注入属性的实体类Db.java
package com.rumenz;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;import java.util.List;
import java.util.Map;
import java.util.Optional;@Configuration
public class Db {//String字符串注入@Value("${rumen.z}")private String one;//int类型注入@Value("${rumenz.i}")private Integer intVal;//数组类型注入@Value("${rumenz.arr}")private int[] arr;//List类型注入@Value("#{'${rumenz.list}'.split(',')}")private List<Integer> list;//Map类型注入@Value("#{${rumenz.map}}")private Map<String, String> map;//枚举类型注入@Value("${rumenz.enum}")private RumenzEnum enum1;//Resource类型注入@Value("${rumenz.resource}")private Resource resource;//Optional类型注入@Autowiredprivate Optional<Rumenz> opt;public Integer getIntVal() {return intVal;}public void setIntVal(Integer intVal) {this.intVal = intVal;}public Resource getResource() {return resource;}public void setResource(Resource resource) {this.resource = resource;}public Optional<Rumenz> getOpt() {return opt;}public void setOpt(Optional<Rumenz> opt) {this.opt = opt;}public RumenzEnum getEnum1() {return enum1;}public void setEnum1(RumenzEnum enum1) {this.enum1 = enum1;}public Map<String, String> getMap() {return map;}public void setMap(Map<String, String> map) {this.map = map;}public List<Integer> getList() {return list;}public void setList(List<Integer> list) {this.list = list;}public int[] getArr() {return arr;}public void setArr(int[] arr) {this.arr = arr;}public String getOne() {return one;}public void setOne(String one) {this.one = one;}@Overridepublic String toString() {return "Db{" +"one='" + one + '\'' +'}';}
}
调用案例
package com.rumenz;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.*;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.context.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.w3c.dom.ls.LSOutput;import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.UUID;@PropertySource("classpath:application.properties") //1
public class DemoApplication {@Autowiredprivate static UUID uuid;@Autowiredprivate static Environment env;public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(ac);xr.loadBeanDefinitions("Beans.xml");ac.register(DemoApplication.class);ac.refresh();Db bean = ac.getBean(Db.class);System.out.println("字符串"+bean.getOne());System.out.println("int"+bean.getIntVal());System.out.println("数组"+Arrays.toString(bean.getArr()));System.out.println("List"+Arrays.toString(bean.getList().toArray()));System.out.println("Map"+bean.getMap().toString());System.out.println("枚举"+bean.getEnum1());System.out.println("Optional"+bean.getOpt().get().getName());System.out.println("resource"+bean.getResource());ac.close();}@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();//2//propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"));return propertySourcesPlaceholderConfigurer;}}
输出
字符串11
int123
数组[1, 2, 3, 4]
List[1, 2, 3, 4, 5]
Map{code=200, msg=ok, data=123}
枚举RED
Optional入门小站
resourceclass path resource [test.txt]
源码:https://github.com/mifunc/Spring-BaseType-Injection-demo
原文: https://rumenz.com/rumenbiji/Spring-BaseType-Injection-demo.html
Spring基础类型注入演示案例相关推荐
- spring基础——注解注入
Spring 针对 Bean 管理中创建对象提供注解 (1)@Component (2)@Service (3)@Controller (4)@Repository 配置文件 1.注解注入要先引入co ...
- WEB安全基础-SQL注入演示
这个是下面演示的源码,看链接地址找对应文件 http://download.csdn.net/download/qq78442761/10224681 本次演示SQL注入 首先进入login.html ...
- 万字博客带你全面剖析Spring的依赖注入
1.写在前面 前面的博客我们已经写了Spring的依赖查找,这篇博客我们来了解写Spring的依赖注入. 2.依赖注入的模式和类型 手动模式 - 配置或者编程的方式, 提前安排注入规则 XML 资源配 ...
- Spring IoC依赖注入-6
1. 依赖注入的模式和模型: Spring 提供了哪些依赖注入的模式和类型? 手动模式 - 配置或者编程的方式,提前安排注入规则 XML资源配置元信息 Java 注解配置元信息 API 配置元信息 自 ...
- Spring基础专题——第二章(注入Injection)
前言:去年到现在一直没有很好的时间完成这个spring基础+源码的博客目标,去年一年比较懒吧,所以今年我希望我的知识可以分享给正在奋斗中的互联网开发人员,以及未来想往架构师上走的道友们我们一起进步,从 ...
- Spring基础专题——第三章(反转控制与依赖注入)
前言:去年到现在一直没有很好的时间完成这个spring基础+源码的博客目标,去年一年比较懒吧,所以今年我希望我的知识可以分享给正在奋斗中的互联网开发人员,以及未来想往架构师上走的道友们我们一起进步,从 ...
- spring配置详解-复杂类型注入
复杂类型注入,刚才都是属性,这个叫注入方式,下面再来一个复杂类型注入,来看一下复杂类型注入行为,复杂类型注入呢,指的是,咱们刚才的注入类型,无非就是值或者对象,假设遇到数组,List,Map,Prop ...
- (十一)Spring 基础注解(对象创建相关注解、注入相关注解)
注解编程 目录 注解基础概念 注解的作用 Spring 注解的发展历程 Spring 基础注解(Spring 2.x) 对象创建相关注解 @Component @Repository.@Service ...
- 【Spring 基础注解】对象创建相关注解、注入相关注解、注解扫描详解
Spring 基础注解(2.x) 注解基础概念 注解的作用 Spring 注解的发展历程 对象创建相关注解 @Component @Repository.@Service.@Contoller @Sc ...
最新文章
- 哪些品牌开始连接鸿蒙系统,华为鸿蒙系统硬件生态品牌升级为 HarmonyOS Connect
- CVPR 2017 全部及部分论文解读集锦
- qml demo分析(threadedanimation-线程动画)
- 开单大师(开源可定制的房产管理系统) v3.6.9学习版
- 【计算机视觉】期刊整理
- Summed-Area Variance Soft Shadow Mapping(SAVSM):一
- 使用TensorFlow.js的AI聊天机器人二:训练Trivia Expert AI
- Ubuntu16.04安装最新版nodejs
- linux文件管理命令详解
- linux进程加载和运行,linux进程管理之可执行文件的加载和运行
- 动态规划实战7 leetcode-221. Maximal Square
- vss2005 配置与使用
- 算法导论答案(第一章)
- android zip winrar,WinRAR Zip Unzip Archive
- CentOS 官网下载各个版本CentOS系统
- 推荐一个开源 Java 小说网站项目
- 浏览器网络异常导致JS文件请求失败的问题及处理
- JAVAWEB第五天XML简介及语法
- 细胞衰老——酪氨酸激酶抑制剂
- python构建聊天机器人之录制声音保存为音频文件(利用pyaudio进行录音)
热门文章
- 优秀的.NET Winform UI控件(标价$399):DotNetMagic Version 3.0 Released
- Linux 共享库LD_PRELOAD环境变量
- 用vlc搭建简单流媒体服务器(UDP和TCP方式)-转 rtsp很慢才能显示
- 【计算机组成原理】定点乘法运算之原码两位乘法
- 计算机网络运用专业,计算机网络专业
- python爬虫requests库_python爬虫使用Requests库 - pytorch中文网
- oracle分页查询过程的简单实现
- react style: 二级菜单
- 如何设置访问共享弹出窗口
- 使用ResourceBundle读取配置文件