beans
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package click.kamil.examples.statemachine.extended.config;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
public abstract class AbstractBaseConfig {
|
||||
|
||||
@Bean
|
||||
public String inheritedBeanTester(@Qualifier("customName") QuirkService someService) {
|
||||
someService.doQuirk();
|
||||
return "inherited";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package click.kamil.examples.statemachine.extended.config;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class BeanParameterTestConfig extends AbstractBaseConfig {
|
||||
|
||||
@Bean
|
||||
public String myBeanParamTester(@Qualifier("customName") QuirkService someService) {
|
||||
// We will use this method as an entry point in the test
|
||||
someService.doQuirk();
|
||||
return "tested";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package click.kamil.examples.statemachine.extended.config;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import click.kamil.examples.statemachine.extended.service.QualifierQuirkService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ConcreteReturnTypeConfig {
|
||||
|
||||
@Bean
|
||||
public QuirkService hiddenConcreteService() {
|
||||
return new QualifierQuirkService(); // It returns an interface, but the concrete type is QualifierQuirkService
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package click.kamil.examples.statemachine.extended.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MockBeanConfig {
|
||||
|
||||
@Bean(name = {"customMockBean", "aliasMockBean"})
|
||||
public String myStringBean() {
|
||||
return "Hello World";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package click.kamil.examples.statemachine.extended.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine(name = "paymentStateMachine")
|
||||
public class PaymentStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial("NEW")
|
||||
.state("AUTHORIZED")
|
||||
.state("CAPTURED")
|
||||
.state("DECLINED")
|
||||
.state("QUIRK1")
|
||||
.state("QUIRK2")
|
||||
.state("QUIRK3")
|
||||
.state("QUIRK4");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source("NEW").target("AUTHORIZED")
|
||||
.event("AUTHORIZE")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("AUTHORIZED").target("CAPTURED")
|
||||
.event("CAPTURE")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("NEW").target("DECLINED")
|
||||
.event("DECLINE")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("NEW").target("QUIRK1")
|
||||
.event("PRIMARY_EVENT")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("NEW").target("QUIRK2")
|
||||
.event("NAMED_EVENT")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("NEW").target("QUIRK3")
|
||||
.event("QUALIFIER_EVENT")
|
||||
.and()
|
||||
.withExternal()
|
||||
.source("NEW").target("QUIRK4")
|
||||
.event("FALLBACK_EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AmbiguousA implements AmbiguousService {
|
||||
@Override
|
||||
public void doAmbig() {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AmbiguousB implements AmbiguousService {
|
||||
@Override
|
||||
public void doAmbig() {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
public interface AmbiguousService {
|
||||
void doAmbig();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FallbackQuirkService implements QuirkService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("paymentStateMachine")
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Override
|
||||
public void doQuirk() {
|
||||
stateMachine.sendEvent("FALLBACK_EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class HighPriorityOrderedService implements OrderedService {
|
||||
@Override
|
||||
public void doAction() {}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Order(10)
|
||||
public class LowPriorityOrderedService implements OrderedService {
|
||||
@Override
|
||||
public void doAction() {}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("customName")
|
||||
public class NamedQuirkService implements QuirkService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("paymentStateMachine")
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Override
|
||||
public void doQuirk() {
|
||||
stateMachine.sendEvent("NAMED_EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
public interface OrderedService {
|
||||
void doAction();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
public interface PaymentService {
|
||||
void processPayment(String paymentId);
|
||||
void capturePayment(String paymentId);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.statemachine.persist.StateMachinePersister;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PaymentServiceImpl implements PaymentService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("paymentStateMachine")
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Autowired
|
||||
private StateMachinePersister<String, String, String> persister;
|
||||
|
||||
@Override
|
||||
public void processPayment(String paymentId) {
|
||||
// Send a trigger mapped strictly to PaymentStateMachineConfig
|
||||
stateMachine.sendEvent("AUTHORIZE");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void capturePayment(String paymentId) {
|
||||
try {
|
||||
persister.restore(stateMachine, paymentId);
|
||||
} catch (Exception e) {}
|
||||
|
||||
org.springframework.messaging.Message<String> msg = org.springframework.messaging.support.MessageBuilder
|
||||
.withPayload("CAPTURE")
|
||||
.setHeader("paymentId", paymentId)
|
||||
.build();
|
||||
stateMachine.sendEvent(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Primary
|
||||
public class PrimaryQuirkService implements QuirkService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("paymentStateMachine")
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Override
|
||||
public void doQuirk() {
|
||||
stateMachine.sendEvent("PRIMARY_EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Profile("nonexistent")
|
||||
public class ProfiledQuirkService implements QuirkService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("paymentStateMachine")
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Override
|
||||
public void doQuirk() {
|
||||
stateMachine.sendEvent("PROFILED_EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class QualifierQuirkService implements QuirkService {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("paymentStateMachine")
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Override
|
||||
public void doQuirk() {
|
||||
stateMachine.sendEvent("QUALIFIER_EVENT");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.examples.statemachine.extended.service;
|
||||
|
||||
public interface QuirkService {
|
||||
void doQuirk();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
public interface BaseController {
|
||||
|
||||
@GetMapping("/api/base/{id}")
|
||||
String processBaseEndpoint(@PathVariable String id);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ConcreteReturnTypeController {
|
||||
|
||||
private final QuirkService hiddenConcreteService;
|
||||
|
||||
public ConcreteReturnTypeController(@Qualifier("hiddenConcreteService") QuirkService hiddenConcreteService) {
|
||||
this.hiddenConcreteService = hiddenConcreteService;
|
||||
}
|
||||
|
||||
@GetMapping("/test-concrete")
|
||||
public void testConcrete() {
|
||||
hiddenConcreteService.doQuirk();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class FieldInjectionController {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("customName")
|
||||
private QuirkService myFieldService;
|
||||
|
||||
@GetMapping("/test-field")
|
||||
public void testField() {
|
||||
myFieldService.doQuirk();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.AmbiguousService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class ListInjectionController {
|
||||
|
||||
private final List<AmbiguousService> services;
|
||||
|
||||
public ListInjectionController(List<AmbiguousService> services) {
|
||||
this.services = services;
|
||||
}
|
||||
|
||||
@GetMapping("/test-list")
|
||||
public void testList() {
|
||||
for (AmbiguousService s : services) {
|
||||
s.doAmbig();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.OrderedService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OrderedTestController {
|
||||
|
||||
private final OrderedService service;
|
||||
|
||||
// The name "service" does not match HighPriorityOrderedService or LowPriorityOrderedService
|
||||
// So the Fallback by name will fail, and it will fall back to @Order
|
||||
public OrderedTestController(OrderedService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/test-order")
|
||||
public void testOrder() {
|
||||
service.doAction();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.PaymentService;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@RestController
|
||||
public class PaymentController implements BaseController {
|
||||
|
||||
private final PaymentService paymentService;
|
||||
|
||||
public PaymentController(PaymentService paymentService) {
|
||||
this.paymentService = paymentService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String processBaseEndpoint(@PathVariable String id) {
|
||||
paymentService.processPayment(id);
|
||||
return "Started Base Payment: " + id;
|
||||
}
|
||||
|
||||
@PostMapping("/api/payment/{id}/capture")
|
||||
public String capturePaymentEndpoint(@PathVariable String id) {
|
||||
paymentService.capturePayment(id);
|
||||
return "Captured: " + id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class QuirkController {
|
||||
|
||||
@Autowired
|
||||
private QuirkService quirkService; // Should resolve to PrimaryQuirkService
|
||||
|
||||
@Autowired
|
||||
@Qualifier("customName")
|
||||
private QuirkService someService; // Should resolve to NamedQuirkService
|
||||
|
||||
@Autowired
|
||||
@Qualifier("qualifierQuirkService")
|
||||
private QuirkService anotherService; // Should resolve to QualifierQuirkService
|
||||
|
||||
@Autowired
|
||||
private QuirkService fallbackQuirkService; // Should resolve to FallbackQuirkService
|
||||
|
||||
@PostMapping("/api/quirk/primary")
|
||||
public void testPrimary() {
|
||||
quirkService.doQuirk();
|
||||
}
|
||||
|
||||
@PostMapping("/api/quirk/named")
|
||||
public void testNamed() {
|
||||
someService.doQuirk();
|
||||
}
|
||||
|
||||
@PostMapping("/api/quirk/qualifier")
|
||||
public void testQualifier() {
|
||||
anotherService.doQuirk();
|
||||
}
|
||||
|
||||
@PostMapping("/api/quirk/fallback")
|
||||
public void testFallback() {
|
||||
fallbackQuirkService.doQuirk();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package click.kamil.examples.statemachine.extended.web;
|
||||
|
||||
import click.kamil.examples.statemachine.extended.service.QuirkService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class SetterInjectionController {
|
||||
|
||||
private QuirkService setterService;
|
||||
|
||||
@Autowired
|
||||
public void setSetterService(@Qualifier("customName") QuirkService setterService) {
|
||||
this.setterService = setterService;
|
||||
}
|
||||
|
||||
@GetMapping("/test-setter")
|
||||
public void testSetter() {
|
||||
setterService.doQuirk();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user