详解SpringMVC加载配置Properties文件的几种方式

网友投稿 1237 2023-06-08


详解SpringMVC加载配置Properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,

网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式

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

1.1、在spring.xml中加入context相关引用

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

1.2、引入jdbc配置文件

1.3、jdbc.properties的配置如下

jdbc_driverClassName=com.mysql.jdbc.Driver

jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8

jdbc_username=root

jdbc_password=123456

1.4、在spring-mybatis.xml中引用jdbc中的配置

destroy-method="close" >

${jdbc_driverClassName}

${jdbc_url}

${jdbc_username}

${jdbc_password}

20

1

60000

20

3

true

180

clientEncoding=UTF-8

1.5、在java类中引用jdbc.properties中的配置

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Configuration;

@Configuration

public class JdbcConfig{

@Value("${jdbc_url}")

public String jdbcUrl; //这里变量不能定义成static

@Value("${jdbc_username}")

public String username;

@Value("${jdbc_password}")

public String password;

}

1.6、在controller中调用

@RequestMapping("/service/**")

@Controller

public class JdbcController{

@Autowired

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

@Value("${jdbc_url}")

private String jdbcUrl; //直接在Controller引用

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("jdbcUrl", Config.jdbcUrl);

return modelMap;

}

@RequestMapping(value={"/test2"})

public ModelMap test2(ModelMap modelMap) {

modelMap.put("jdbcUrl", this.jdbcUrl);

return modelMap;

}

}

1.7、测试

在ie中输入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2

返回如下结果:

{

jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"

}

注:通过context:property-placeholde加载多个配置文件

只需在第1.2步中将多个配置文件以逗号分隔即可

复制代码 代码如下:

2、通过util:properties实现配置文件加载

2.1、在spring.xml中加入util相关引用

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">

2.2、 引入config配置文件

2.3、config.properties的配置如下

gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest

2.4、在java类中引用config中的配置

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component

public class Config {

@Value("#{settings['gnss.server.url']}")

public String GNSS_SERVER_URL;

}

2.5、在controller中调用

@RequestMapping("/service2/**")

@Controller

public class ConfigController{

@Autowired

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

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);

return modelMap;

}

}

2.6、测试

在ie中输入http://localhost:8080/testWeb/service2/test

返回如下结果:

{

"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"

}

3.直接在Java类中通过注解实现配置文件加载

3.1、在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("${gnss.server.url}")

public String GNSS_SERVER_URL;

@Value("${gnss.server.url}")

public String jdbcUrl;

}

3.2、在controller中调用

@RequestMapping("/service2/**")

@Controller

pubhttp://lic class ConfigController{

@Autowired

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

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);

return modelMap;

}

}

3.3、测试

在ie中输入http://localhost:8080/testWeb/service2/test

返回如下结果:

{

"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"

}

最后附上spring.xml的完整配置:

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

1.2、引入jdbc配置文件

1.3、jdbc.properties的配置如下

jdbc_driverClassName=com.mysql.jdbc.Driver

jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8

jdbc_username=root

jdbc_password=123456

1.4、在spring-mybatis.xml中引用jdbc中的配置

destroy-method="close" >

${jdbc_driverClassName}

${jdbc_url}

${jdbc_username}

${jdbc_password}

20

1

60000

20

3

true

180

clientEncoding=UTF-8

destroy-method="close" >

${jdbc_driverClassName}

${jdbc_url}

${jdbc_username}

${jdbc_password}

20

1

60000

20

3

true

180

clientEncoding=UTF-8

1.5、在java类中引用jdbc.properties中的配置

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Configuration;

@Configuration

public class JdbcConfig{

@Value("${jdbc_url}")

public String jdbcUrl; //这里变量不能定义成static

@Value("${jdbc_username}")

public String username;

@Value("${jdbc_password}")

public String password;

}

1.6、在controller中调用

@RequestMapping("/service/**")

@Controller

public class JdbcController{

@Autowired

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

@Value("${jdbc_url}")

private String jdbcUrl; //直接在Controller引用

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("jdbcUrl", Config.jdbcUrl);

return modelMap;

}

@RequestMapping(value={"/test2"})

public ModelMap test2(ModelMap modelMap) {

modelMap.put("jdbcUrl", this.jdbcUrl);

return modelMap;

}

}

1.7、测试

在ie中输入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2

返回如下结果:

{

jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"

}

注:通过context:property-placeholde加载多个配置文件

只需在第1.2步中将多个配置文件以逗号分隔即可

复制代码 代码如下:

2、通过util:properties实现配置文件加载

2.1、在spring.xml中加入util相关引用

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">

2.2、 引入config配置文件

2.3、config.properties的配置如下

gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest

2.4、在java类中引用config中的配置

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component

public class Config {

@Value("#{settings['gnss.server.url']}")

public String GNSS_SERVER_URL;

}

2.5、在controller中调用

@RequestMapping("/service2/**")

@Controller

public class ConfigController{

@Autowired

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

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);

return modelMap;

}

}

2.6、测试

在ie中输入http://localhost:8080/testWeb/service2/test

返回如下结果:

{

"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"

}

3.直接在Java类中通过注解实现配置文件加载

3.1、在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("${gnss.server.url}")

public String GNSS_SERVER_URL;

@Value("${gnss.server.url}")

public String jdbcUrl;

}

3.2、在controller中调用

@RequestMapping("/service2/**")

@Controller

pubhttp://lic class ConfigController{

@Autowired

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

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);

return modelMap;

}

}

3.3、测试

在ie中输入http://localhost:8080/testWeb/service2/test

返回如下结果:

{

"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"

}

最后附上spring.xml的完整配置:

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">

2.2、 引入config配置文件

2.3、config.properties的配置如下

gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest

2.4、在java类中引用config中的配置

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component

public class Config {

@Value("#{settings['gnss.server.url']}")

public String GNSS_SERVER_URL;

}

2.5、在controller中调用

@RequestMapping("/service2/**")

@Controller

public class ConfigController{

@Autowired

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

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);

return modelMap;

}

}

2.6、测试

在ie中输入http://localhost:8080/testWeb/service2/test

返回如下结果:

{

"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"

}

3.直接在Java类中通过注解实现配置文件加载

3.1、在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("${gnss.server.url}")

public String GNSS_SERVER_URL;

@Value("${gnss.server.url}")

public String jdbcUrl;

}

3.2、在controller中调用

@RequestMapping("/service2/**")

@Controller

pubhttp://lic class ConfigController{

@Autowired

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

@RequestMapping(value={"/test"})

public ModelMap test(ModelMap modelMap) {

modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);

return modelMap;

}

}

3.3、测试

在ie中输入http://localhost:8080/testWeb/service2/test

返回如下结果:

{

"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"

}

最后附上spring.xml的完整配置:

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-4.0.xsd">


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解Spring 两种注入的方式(Set和构造)实例
下一篇:java 中同步方法和同步代码块的区别详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~