美文网首页
springboot 自定义 bean

springboot 自定义 bean

作者: lazyTai | 来源:发表于2018-10-23 16:39 被阅读50次

bean

package com.example.demo.beans;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Bean1 {
    @Value("1")
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

使用

package com.example.demo.web.json;

import com.example.demo.beans.Bean1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class JsonIndex {

    @Autowired
    Bean1 bean1;

    @RequestMapping("/index")
    public Map index() {
        Map a = new HashMap();
        a.put("name", bean1.getName());
        return a;
    }
}

相关文章

网友评论

      本文标题:springboot 自定义 bean

      本文链接:https://www.haomeiwen.com/subject/ojuazftx.html