1.链模型: 有顺序的执行方法;
2.上代码吧
TestService.class
import org.springframework.core.Ordered;
public interface TestService extends Ordered {
void execute();
}
Test1Service.class
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class Test1Service implements TestService {
@Override
public void execute() {
log.info("执行顺序:class:{},order:{}", this.getClass().getName(), getOrder());
}
@Override
public int getOrder() {
return 3;
}
}
Test2Service.class
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class Test2Service implements TestService {
@Override
public void execute() {
log.info("执行顺序:class:{},order:{}", this.getClass().getName(), getOrder());
}
@Override
public int getOrder() {
return 2;
}
}
Test3Service.class
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class Test3Service implements TestService {
@Override
public void execute() {
log.info("执行顺序:class:{},order:{}", this.getClass().getName(), getOrder());
}
@Override
public int getOrder() {
return 1;
}
}
TestController.class
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private List<TestService> testServiceList;
@GetMapping("/test")
public void test() {
testServiceList.forEach(TestService::execute);
}
}