读取设置文件方式

打印 上一主题 下一主题

主题 991|帖子 991|积分 2975

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
方式一:XML

设置方式一:
  1.     <!--引入外部属性文件1-->
  2.     <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  3.         <property name="locations" value="classpath:jdbc.properties"/>
  4.     </bean>
复制代码
设置方式一:
  1. <!--引入外部属性文件2-->
  2.     <context:property-placeholder location="classpath:jdbc.properties"/>
复制代码
 设置文件:
  1. prop.driverClass=com.mysql.jdbc.Driver
  2. prop.url=jdbc:mysql://localhost:3306/userDb
  3. prop.userName=root
  4. prop.password=abc123
复制代码
\
动态取值:
  1.     <!--配置连接池-->
  2.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  3.         <property name="url" value="${prop.url}"/>
  4.         <property name="username" value="${prop.userName}"/>
  5.         <property name="password" value="${prop.password}"/>
  6.         <property name="driverClassName" value="${prop.driverClass}"/>
  7.     </bean>
复制代码
方式一:@ConfigurationProperties(prefix = "xxx")+@Component

  1. @ConfigurationProperties(prefix = "person")
  2. @Component
  3. @Data
  4. public class Person {
  5.     private String userName;
  6.     private Boolean boss;
  7.     private Date birth;
  8.     private Integer age;
  9.     private Pet pet;
  10.     private String[] interests;
  11.     private List<String> animal;
  12.     private Map<String, Object> score;
  13.     private Set<Double> salarys;
  14.     private Map<String, List<Pet>> allPets;
  15. }
复制代码
设置文件:application.properties
  1. person.user-name=zhangsan
  2. person.boss=true
  3. person.birth=2019/12/9
  4. person.age=18
  5. person.pet.name=阿狗
  6. person.pet.weight=99.99
  7. person.interests=篮球,足球
  8. person.animal=阿猫,阿狗
  9. person.score.english=80
  10. person.score.math=80
  11. person.salarys=9999.98,9999.99
  12. person.all-pets.sick[0].name=阿狗;
  13. person.all-pets.sick[0].weight=99.99
  14. person.all-pets.health[0].name=阿花
  15. person.all-pets.health[1].name=阿明
  16. person.all-pets.health[0].weight=199.99
  17. person.all-pets.health[1].weight=200
  18. #https://blog.csdn.net/qq_35754073/article/details/136606186
复制代码
测试:
  1.     @Autowired
  2.     Person person;
  3.    
  4.     @Test
  5.     void test() {
  6.         System.out.println(person);
  7.     }
复制代码

方式三:@ConfigurationProperties+@EnableConfigurationProperties(xxx.class)

  1. @ConfigurationProperties(prefix = "person")
  2. @Data
  3. public class Person {
  4.     private String userName;
  5.     private Boolean boss;
  6.     private Date birth;
  7.     private Integer age;
  8.     private Pet pet;
  9.     private String[] interests;
  10.     private List<String> animal;
  11.     private Map<String, Object> score;
  12.     private Set<Double> salarys;
  13.     private Map<String, List<Pet>> allPets;
  14. }
复制代码
  1. @EnableConfigurationProperties(Person.class)
  2. @Configuration
  3. public class MyConfig {
  4.    
  5.     @Bean
  6.     public Person person(Person person) {
  7.         System.out.println(person);
  8.         return person;
  9.     }
  10. }
复制代码
测试:
  1. @Autowired
  2. MyConfig myConfig;
复制代码

方式四:@Value+application.properties

设置文件:
  1. myconfig.name=1111
  2. myconfig.pwd=2222
复制代码
测试:application.properties
  1.     @Value("${myconfig.name}")
  2.     private String name;
  3.     @Value("${myconfig.pwd}")
  4.     private String pwd;
  5.     @Test
  6.     void test2() {
  7.         System.out.println(name);
  8.         System.out.println(pwd);
  9.     }
复制代码

方式四:@Value+xxx.properties自界说设置文件

设置:other.properties
  1. other.name=3333
  2. other.pwd=4444
复制代码
  1. @PropertySources({
  2.         @PropertySource("classpath:other.properties")
  3. })
  4. @Configuration
  5. public class OtherConfig {
  6.    
复制代码
测试:
  1.     @Value("${other.name}")
  2.     private String otherName;
  3.     @Value("${other.pwd}")
  4.     private String otherPwd;
  5.     @Test
  6.     void test3() {
  7.         System.out.println(otherName);
  8.         System.out.println(otherPwd);
  9.     }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81428

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表