1.注解注入(不建议使用)
package com.ins.zone.controller;
import com.ins.zone.service.StudentInfoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Zone
* @version 1.0
* @date 2020/3/29 15:57
*/
@Slf4j
@Api(tags = "学生信息")
@RestController
@RequestMapping("query")
public class StudentInfoController {
@Autowired
private StudentInfoService service;
/**
* @Description
* @Date 16:51 2020/3/29
* @param age
* @param name
* @return com.ins.zone.entity.Student
**/
@PostMapping("studentInfo")
public String getStudentInfo(String age, String name){
return service.getStuInfo(age, name);
}
}
2.构造器注入
package com.ins.zone.controller;
import com.ins.zone.service.StudentInfoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Zone
* @version 1.0
* @date 2020/3/29 15:57
*/
@Slf4j
@Api(tags = "学生信息")
@RestController
@RequestMapping("query")
public class StudentInfoController {
private StudentInfoService service;
@Autowired
public StudentInfoController(StudentInfoService studentService) {
this.service = studentService;
}
/**
* @Description
* @Date 16:51 2020/3/29
* @param age
* @param name
* @return com.ins.zone.entity.Student
**/
@PostMapping("studentInfo")
public String getStudentInfo(String age, String name){
return service.getStuInfo(age, name);
}
}
3.setter注入
package com.ins.zone.controller;
import com.ins.zone.service.StudentInfoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Zone
* @version 1.0
* @date 2020/3/29 15:57
*/
@Slf4j
@Api(tags = "学生信息")
@RestController
@RequestMapping("query")
public class StudentInfoController {
private StudentInfoService service;
@Autowired
public void setService(StudentInfoService service) {
this.service = service;
}
/**
* @Description
* @Date 16:51 2020/3/29
* @param age
* @param name
* @return com.ins.zone.entity.Student
**/
@PostMapping("studentInfo")
public String getStudentInfo(String age, String name){
return service.getStuInfo(age, name);
}
}