使用@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无法启动管理服务器解决方法
  • 热门搜索
    化粪池做法 鸡爪子的做法 油炸糕的做法 苹果手机怎么投屏到电视 年糕的做法家常做法 黄焖鸡的做法视频 米酒煮鸡蛋的做法 干炸里脊的家常做法 开塞露怎么用 琥珀核桃仁的做法