SpringBoot——yml注入和创建bean(二)

 

 

yml文件:

yml属性注入:
student:
  stu_name: 沈刚伟
  stu_age: 123
  stu_email: 123456@qq.com

  user_id: ${random.int[1000,2000]} //随机数

  user_name: ${student.name2:吴没}//调用properties里面配置 如果为空默认为后面的

birthday: 2018/2/1
local: #{name: 沈刚伟,aa: 杭州}
  name: 沈刚伟
  aa: 杭州

habbies:
  - 足球
  - 篮球
skills: [编程,金融]
  #- 编程
  #- 金融
pet: {pet_name: 张三1,pet_sex: 4}
  #pet_name: 张三
  #pet_sex: 4

Java

package com.xiaonuo.pojo;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.*;

@Component
@ConfigurationProperties(prefix = "student")
@Validated
public class Student {
    @Value("${student.stu_name}")
    private String stu_name;
    @Value("${student.stu_age}")
    private Integer stu_age;
    @Email
    private String stu_email;

    @Value("[谁给我,aaa]")
    private List<String> hobbies;

    private Map<String,Object> map;

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public String getStu_email() {
        return stu_email;
    }

    public List<String> getHobbies() {
        return hobbies;
    }


    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public void setStu_email(String stu_email) {
        this.stu_email = stu_email;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public Integer getStu_age() {
        return stu_age;
    }

    public void setStu_age(Integer stu_age) {
        this.stu_age = stu_age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stu_name='" + stu_name + '\'' +
                ", stu_age=" + stu_age +
                ", stu_email='" + stu_email + '\'' +
                ", hobbies=" + hobbies +
                '}';
    }
}

2、创建Bean

1.可以用@ImportResource("")的方式来创建xml搭配,不过不推荐

2.推荐用Java配置

Java配置类

@Configuration
public class Appconfig {
    @Bean
    public ServiceImpl service(){
        ServiceImpl service = new ServiceImpl();
        service.setSout("xxxxq");

        return service;
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值