查看spring boot官網文檔:
以下是一個 @Service
Bean,其使用構造注入方式獲取一個必需的 RiskAssessor
bean。
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
如果 bean 中只有一個構造方法,您可以忽略掉 @Autowired
註解。
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
提示
請注意,構造注入允許
riskAssessor
字段被修飾爲final
,這表示以後它不能被更改。也可以不使用final修飾
如下例子:
controller
package com.wm.test.controller;
import com.wm.test.service.HelloServiceImpl;
import com.wm.test.service.TestServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/***
* @ClassName: TestController
* @Description: 測試Controller
* @Author: wm_yu
* @Create_time: 15:25 2019-10-17
*/
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {
private final HelloServiceImpl helloService;
private TestServiceImpl testService;
/**
*
* @return
*/
@GetMapping("/hello")
public String test(String name){
String s = helloService.test(name);
return s;
}
/**
*
* @return
*/
@GetMapping("/test")
public String hello(String name){
String s = testService.test(name);
return s;
}
}
service:
package com.wm.test.service;
import org.springframework.stereotype.Service;
/***
* @ClassName: HelloServiceImpl
* @Description:
* @Author: wm_yu
* @Create_time: 10:03 2019-11-12
*/
@Service
public class HelloServiceImpl {
public String test(String name){
return String.format("hell,%s",name);
}
}
package com.wm.test.service;
import org.springframework.stereotype.Service;
/***
* @ClassName: TestServiceImpl
* @Description:
* @Author: wm_yu
* @Create_time: 10:09 2019-11-12
*/
@Service
public class TestServiceImpl {
public String test(String name){
return String.format("hell,%s",name);
}
}
上面的例子可以使用final修飾,也可以不修飾
下面是設置遠程debug啓動:
編寫一個簡單的項目:
controller:
package com.wm.mi.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/***
* @ClassName: HelloController
* @Description:
* @Author: wm_yu
* @Create_time: 16:35 2019-11-11
*/
@RestController
@RequestMapping("/yu")
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestBody String name){
return String.format("hell0,%s",name);
}
}
將項目打包成jar:
mvn package
如下:
普通啓動爲:
java -jar mi-0.0.1-SNAPSHOT.jar
設置debug端口啓動:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar mi-0.0.1-SNAPSHOT.jar
在idea中設置debug遠程配置調試:
啓動測試就可以了,官方文檔如下: