Spring 学习笔记《依赖注入》—— 扫描配置文件注入
发布日期:2021-06-30 14:57:26 浏览次数:2 分类:技术文章

本文共 1241 字,大约阅读时间需要 4 分钟。

Spring 学习笔记

扫描配置文件注入

.properties 配置文件

/SpringIoC/src/textbook.properties

user=11111level=22222222222

警告 : 此处有个坑【.properties】文件里写中文要转码,如果你在Eclipse里,它会帮你自动转。但如果你用其他编辑器,就要小心吧,比如Editplus就提示我转文件编码格式,我就转了,然后。。。当然就是出翔啦。下面是写中文的样子的

user=\u94A2\u7B14\u5B57\u5E16level=\u521D\u4E2D

Spring 配置文件

/SpringIoC/src/applicationContext.xml

测试代码

/SpringIoC/src/com/jerry/test/TestSpringIoC.java

package com.jerry.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jerry.entity.MyCollection;import com.jerry.entity.Student;import com.jerry.entity.Teacher;import com.jerry.entity.Textbook;public class TestSpringIoC {
public static void main(String[] args) {
// 获取Spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // ********************************** 依赖注入 ************************************* // 扫描配置文件注入 Textbook textbookByProperties= (Textbook)context.getBean("textbookByProperties");// 从容器中获取对象 System.out.println("扫描配置文件注入,创建 bean : " + textbookByProperties.toString());// 查看结果 }}

此功能最常见的就是用来配置 dataSource 扫描 jdbc.properties 获取属性。还没试手,试玩再做笔记。

转载地址:https://jerryjin.blog.csdn.net/article/details/86578594 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring 学习笔记《依赖注入》—— 自动注入
下一篇:Spring 学习笔记《准备工作》

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月26日 07时22分13秒