Skip to content

Instantly share code, notes, and snippets.

@rainoko
Created September 4, 2018 16:29
Show Gist options
  • Save rainoko/fa52421405b574a0a1763551d2dd90aa to your computer and use it in GitHub Desktop.
Save rainoko/fa52421405b574a0a1763551d2dd90aa to your computer and use it in GitHub Desktop.
@Service
public class Example1Service {
@Transactional
public void doSomethingTransactional() {
...
}
@Transactional
protected void doSomethingTransactionalProtected() {
...
}
public void doSomethingWithTransactional() {
doSomethingTransactional() // WRONG!!! not transactional
}
}
@Service
public class Example2Service {
@Inject
Example1Service service;
public void doSomethingWithTransactional() {
service.doSomethingTransactionalProtected() // WRONG!!! not transactional
service.doSomethingTransactional() // OK transactional
}
}
@Component
public class MyListener {
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
...
}
}
@Component
public class MyComponent {
@EventListener(condition = "#creationEvent.awesome")
public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) {
...
}
}
@Component
public class MyComponent {
private final ApplicationEventPublisher publisher;
@Autowired
public MyComponent(ApplicationEventPublisher publisher) { ... }
public void createOrder(Order order) {
// ....
this.publisher.publishEvent(new OrderCreatedEvent(order));
}
}
@Component
public class MyComponent {
@TransactionalEventListener(condition = "#creationEvent.awesome")
public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) {
...
}
}
@TransactionalEventListener is a regular @EventListener and also exposes a TransactionPhase, the default being AFTER_COMMIT. You can also hook other phases of the transaction (BEFORE_COMMIT, AFTER_ROLLBACK and AFTER_COMPLETION that is just an alias for AFTER_COMMIT and AFTER_ROLLBACK).
By default, if no transaction is running the event isn’t sent at all as we can’t obviously honor the requested phase, but there is a fallbackExecution attribute in @TransactionalEventListener that tells Spring to invoke the listener immediately if there is no transaction.
ThreadLocal
InheritableThreadLocal
CredentialsFilter
HandlerInterceptorAdapter
RestTemplate ClientHttpRequestInterceptor
Rabbitmq MessageListener ja MessagePostProcessor
@Scheduler ScheduledThreadPoolExecutor
@Async CredentialsAwareTaskDecorator
parallel DelegatingCredentialsCallable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment