首页 > 其他分享 >IDEA社区版搭建Spring工程(04-加载配置文件及加解密)

IDEA社区版搭建Spring工程(04-加载配置文件及加解密)

时间:2024-07-10 11:42:50浏览次数:10  
标签:配置文件 04 Spring content context Config hello 加载

Spring MVC 加载配置文件的几种方式

  1. 通过 context:property-placeholde 实现加载配置文件

在 springmvc.xml 配置文件里加入 context 相关引用

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">

引入config配置文件 <context:property-placeholder location="classpath:config.properties"/>

  • 通过context:property-placeholde加载多个配置文件,只需将多个配置文件以逗号分隔即可。

config配置文件内容如下:

hello.content=hello,everyone.

在Java类中调用

@Value("${hello.content}")
private String content;
  1. 通过util:properties实现配置文件加载

在springmvc.xml中加入util相关引用

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 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/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/util  
      http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 

引入config配置文件 <util:properties id="settings" location="classpath:config.properties"/>

在Java类中调用

@Value("#{settings['hello.content']}")
private String content;
  1. 直接在Java类中通过注解实现配置文件加载

在java类中引入配置文件

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration  
@PropertySource(value="classpath:config.properties")    
public class Config {

@Value("${hello.content}")
public  String content;

}

在springmvc配置文件里添加

<context:component-scan base-package="springmvc"/>

<!-- 配置springMVC开启注解支持 -->
<mvc:annotation-driven/>

在controller中调用

@Autowired  
private Config Config; //引用统一的参数配置类  

@RequestMapping(value={"/hello"})  
public ModelMap test(ModelMap modelMap) {   
  modelMap.put("message", Config.content);  
      return modelMap;  

标签:配置文件,04,Spring,content,context,Config,hello,加载
From: https://www.cnblogs.com/andy020/p/18293733

相关文章

  • C语言学习笔记(04)——内存空间的使用
    指针指针概述:内存类型资源的地址、门牌号的代名词指针只是个概念,要用还得用指针变量:存放指针这个概念的盒子C语言编译器对指针这个特殊的概念,有2个疑问?1、分配一个盒子,盒子要多大?​ 在32bit系统中,指针就是4个字节,指针大小都固定了,就是4字节,跟你指向什么类型没有关系2、盒子......
  • Spring AOP的几种实现方式
    1.通过注解实现1.1导入依赖<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.1.6.RELEASE</version></dependency>1.2定义注解imp......
  • spring boot microservices
    微服务包括这些组件,服务注册,服务发现,负载均衡,配置管理,路由,断流器-过流熔断,分布式系统的日记追踪,服务安全等。thecomponentofspringbootmicroservices.springboot,itprovidesthefoudationforbuildingmicroservices.Itsimplifiestheconfigurationanssetup......
  • 毕业设计-基于Springboot+Vue的家政服务管理平台的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456882基于SpringBoot+Vue的家政服务管理平台开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/1gssA8jncDvvFfo8NSHDh8g?pw......
  • 毕业设计-基于Springboot+Vue的社区医院管理服务系统的设计与实现(源码+LW+包运行)
    源码获取:https://download.csdn.net/download/u011832806/89456872基于SpringBoot+Vue的社区医院管理服务系统开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue.js工具:IDEA/Ecilpse、Navicat、Maven系统演示视频:链接:https://pan.baidu.com/s/14Zrh0wu8QdSeEJof1uyc0......
  • SpringBoot 整合 MyBatisPlus框架入门
    步骤1:创建maven工程创建一个空Maven工程,如下:步骤2:pom.xml文件中添加MyBatisPlus相关依赖<dependencies><!--mybatispulus依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter&l......
  • Jenkins集成部署SpringBoot
    Jenkins集成部署SpringBoot1.前言随着业务的增长,需求也开始增多,每个需求的大小,开发周期,发布时间都不一致。基于微服务的系统架构,功能的叠加,对应的服务的数量也在增加,大小功能的快速迭代,更加要求部署的快速化,智能化。因此,传统的人工部署已经心有余而力不足。持续集成,持续部署,持......
  • IDEA社区版搭建Spring工程(03-Spring MVC搭建)
    新建一个基于Maven的"webapp"模板的基础工程在main文件夹下新建java源码文件夹将自动生成的index.jsp移入webapp的view文件夹下,在java下新建一个controller文件夹添加SpringMVC框架所需的POM配置<properties><project.build.sourceEncoding>UTF-8</pro......
  • 利用SpringBoot+rabbitmq 实现邮件异步发送,保证100%投递成功
    在之前的文章中,我们详细介绍了SpringBoot整合mail实现各类邮件的自动推送服务。但是这类服务通常不稳定,当出现网络异常的时候,会导致邮件推送失败。本篇文章将介绍另一种高可靠的服务架构,实现邮件100%被投递成功。类似的短信自动发送等服务也大体相同。一、先来一张流程图......
  • 基于java+springboot+vue实现的音乐网站(文末源码+Lw)102
     功能介绍:本音乐网站管理员功能有个人中心,用户管理,歌曲分类管理,歌曲信息管理,管理员管理,系统管理等。用户可以注册登录,试听歌曲,可以下载歌曲。因而具有一定的实用性。本站是一个B/S模式系统,采用SpringBoot框架,MYSQL数据库设计开发,充分保证系统的稳定性。系统具有界面清晰、......