使用@PropertySource加载外部配置文件属性赋值

 时间:2024-10-15 13:18:48

1、新建一个spring组件配置类:package com.gwolf.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.gwolf.vo.Connection;@Configurationpublic class MainConfigOfPropertyValues { @Bean public Connection connection() { return new Connection(); }}

使用@PropertySource加载外部配置文件属性赋值

3、在组件配置类中使用@PropertySource注解导入*.properties配置文件:package com.gwolf.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import com.gwolf.vo.Connection;@Configuration@PropertySource("classpath:dbconfig.properties")public class MainConfigOfPropertyValues { @Bean public Connection connection() { return new Connection(); }}

使用@PropertySource加载外部配置文件属性赋值

5、编写一个junit测试类,得到容器中的bean对象的值,打印bean的值。pa罕铞泱殳ckage com.gwo造婷用痃lf.test;import org.junit.Test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.gwolf.config.MainConfigOfPropertyValues;public class ComponentTest { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class); @Test public void testImport() { String[] beanNames = applicationContext.getBeanDefinitionNames(); System.out.println(applicationContext.getBean("connection")); applicationContext.close(); }}

使用@PropertySource加载外部配置文件属性赋值
  • Oracle 怎么删除和新建用户?
  • securecrt如何导入会话
  • Mybatis中foreach遍历集合
  • java实现SFTP上传、下载文件功能
  • weblogic无法启动管理服务器解决方法
  • 热门搜索
    光明磊落的意思 positive是什么意思 奇异的意思 财产保全是什么意思 一尘不染的意思 心领神会的意思 阿奇霉素分散片的作用 一本正经的意思 标段是什么意思 白术的作用与功效