spring外部化配置 /static
时间: 2023-08-09 08:06:48 浏览: 42
Spring外部化配置是指将应用程序的配置信息从代码中分离出来,存放在外部文件中,以便在不修改代码的情况下进行配置的更改。对于静态资源(如CSS、JavaScript、图片等),可以将这些资源放置在项目的/static目录下。
在Spring中,可以使用`@Value`注解和`PropertySourcesPlaceholderConfigurer`来实现外部化配置。具体步骤如下:
1. 在Spring配置文件中,添加`PropertySourcesPlaceholderConfigurer` bean,用于解析外部配置文件中的属性值。例如:
```xml
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
```
2. 创建一个外部配置文件,例如`config.properties`,将静态资源路径配置在该文件中:
```properties
static.resources.path=/static
```
3. 在需要使用静态资源路径的地方,使用`@Value`注解来获取配置值:
```java
@Value("${static.resources.path}")
private String staticResourcesPath;
```
通过以上步骤,就可以将静态资源路径从代码中分离出来,统一管理在外部配置文件中。