ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springbot---配置绑定

2021-12-12 10:00:48  阅读:126  来源: 互联网

标签:return String Car brand 绑定 public --- springbot price


@ConfigurationProperties和component
功能:
  使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;
实体类
package com.atguigu.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 只有在容器中的组件,才会拥有springboot提供的强大功能
 */
@Component//表明将这个类放在容器中

@ConfigurationProperties(prefix = "mycar")//配置文件的前缀 public class Car { private String brand; private Integer price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", price=" + price + '}'; } }

配置配置文件:application.properties

mycar.brand=byd
mycar.price=100000

测试,编写一个controller,返回该对象看页面是否返回一个有值的对象

package com.atguigu.controller;

import com.atguigu.bean.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @Autowired
    Car car;
@RequestMapping("/car") public Car car(){ return car; } }

成功

 

 

@ConfigurationProperties和@EnableConfigurationProperties(Car.class)连用

实体类

package com.atguigu.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 只有在容器中的组件,才会拥有springboot提供的强大功能
 */
@ConfigurationProperties(prefix = "mycar")//配置文件的前缀
public class Car {
    private String brand;
    private Integer price;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

config也就是spring配置类myconfig

@Configuration(proxyBeanMethods = true)//告诉spring这是一个配置类,相当于xml配置文件
@EnableConfigurationProperties(Car.class)
public class MyConFig {
    /**
     * 配置类无论调用多想遍方法,对象都是同一个对象,单例模式
     */
    @Bean//给容器添加组件,默认方法名就是组件id,返回值类型就是组件类型,返回的值就是组件的实例,也就是赋值
    public User user(){
        User user =  new User("小王",14);
        user.setPet(pet());
        return user;
    }
    @Bean("tom")//可以给bean取别名,否则就是方法名
    public Pet pet(){
        return new Pet("tomcat");
    }
}

controller

@RestController
public class HelloController {
    @Autowired
    Car car;

    @RequestMapping("/car")
    public Car car(){
        return car;
    }


    @RequestMapping("/hello")
    public String get(){
        return "hello boot"+"你好";
    }

}

...

 

 

标签:return,String,Car,brand,绑定,public,---,springbot,price
来源: https://www.cnblogs.com/dbconfig1/p/15678298.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有