Spring启动时可以加载外部的配置文件,可以通过以下几种方式来实现:
使用@PropertySource注解:在Spring配置类上使用@PropertySource注解来指定外部配置文件的位置。例如,如果有一个名为config.properties的配置文件,可以在配置类上使用@PropertySource注解来加载它:@Configuration@PropertySource("classpath:config.properties")public class AppConfig {// ...}使用@Value注解:可以在Spring组件(例如@Service、@Controller等)中使用@Value注解来注入外部配置文件中的属性值。例如,假设在配置文件中有一个属性名为app.name,可以使用@Value注解将其注入到某个类中:@Servicepublic class SomeService {@Value("${app.name}")private String appName;// ...}使用Environment对象:可以通过注入Environment对象来访问外部配置文件中的属性值。例如,可以在配置类中注入Environment对象,并使用getProperty()方法获取属性值:@Configuration@PropertySource("classpath:config.properties")public class AppConfig {@Autowiredprivate Environment environment;// ...@Beanpublic SomeBean someBean() {String propertyValue = environment.getProperty("app.name");// ...}}除了上述方式,还可以使用其他方式来加载外部配置文件,比如使用Spring的PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer来加载属性文件,或者使用@ImportResource注解来导入XML配置文件等。

