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;
}
}
网友评论