使用@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无法启动管理服务器解决方法
  • 热门搜索
    五彩斑斓的近义词 塌鼻梁怎么办 郑重其事的近义词 股票印花税怎么算 贷款怎么算 优点用英语怎么说 路由器重置后怎么设置 感冒可以喝咖啡吗 即食燕窝怎么吃 怎么消灭蟑螂