搜索
您的当前位置:首页正文

springboot 自定义 bean

来源:二三娱乐

bean

package com.example.demo.beans;

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

@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;
    }
}

Top