SlideShare a Scribd company logo
1 of 26
Download to read offline
victorrentea@gmail.com ♦ ♦ @victorrentea ♦ VictorRentea.ro
All the code is available on the branch JAX_Mainz on https://github.com/victorrentea/spring
Victor Rentea
VictorRentea.ro
Blog, Best Talks, Video Courses, ...
Independent Trainer
for companies and individuals
Founder of
Bucharest Software Craftsmanship Community
Java Champion
❤️ Simple Design, Refactoring, Unit Testing ❤️
Technical Training
400 days
(100+ online)
2000 devs
8 years
More: VictorRentea.ro
Workshops: victorrentea.teachable.com
50 companies
Reach out
to me:
Hibernate
Spring Functional Prog
Java Performance
Reactive Prog
Design Patterns
Pragmatic DDD
Clean Code
Refactoring
Unit Testing
TDD
any
lang
@victorrentea
Intense
34 © VictorRentea.ro
a training by
34
3
5 © VictorRentea.ro
a training by
class A {
private B b;
...
}
class BImpl
implements B {
m() {} ...
}
interface B {
m(); ...
}
Interface Proxy
has a
:A
B
:BImpl
implem
Static
Runtime
36 © VictorRentea.ro
a training by
class A {
private B b;
...
}
class BImpl
implements B {
m() {} ...
}
interface B {
m(); ...
}
Interface Proxy
has a
:A
B
:BImpl
implem
call
Static
Runtime
implem
proxy
m() {}
Created at run-time with
java.lang.reflect.Proxy
37 © VictorRentea.ro
a training by
class A {
private B b;
...
}
interface B {
m(); ...
}
Class Proxy
has a
Static
Runtime
class B {
{ }
:A
B
:B
call
proxy
m() {}
The bytecode of a subclass
is generated in memory by (eg CGLIB)
extends
Different InstanceS
38 © VictorRentea.ro
a training by
38
39 © VictorRentea.ro
a training by
Every time you don’t understand how Spring does something…
it's a Proxy
@Cacheable
@Transactional
@Aspect
@Async
@Secured / @PreAuthorize
@Retryable
Request/Session/Thread scope
…
The Magic of Spring
40 © VictorRentea.ro
a training by
Dependency Injection is great because
1 Classes are easier to break
3 Proxies can be given to you transparently
2 Cleaner to test
41 © VictorRentea.ro
a training by
:B
proxy
m() {}
Different InstanceS
Local Method Calls
don't get Proxied!
@Override
m() {}
Class Proxy Pitfalls
final
ignored:
final class
crash:
The default way
in Spring
You have to take
a reference to B
from Spring
@ Victo
r
Rentea
42
•A method of a non-Spring bean?
• Think new
•A final method/class?
• For a class-proxy:
•A static method?
•Field access?
•A local method call?
Can Spring AOP Intercept…
NO!
NO!
Spring AOP can only intercept method calls
NO!
Well…
Actually…
You know…
You can do any of that via:
Bytecode Enhancement
(compile-time hacking)
Or Instrumentation
(classload-time hacking)
NO!!!
NO!
How could it be overridden?
https://github.com/dsyer/spring-boot-aspectj#
@VictorRentea
43
proxy
methodWithTx();
myselfProxied.methodWithTx();
War Stories
@Transactional(propagation = REQUIRES_NEW)
public void methodWithTx() {
@Autowired
private CurrentClass myselfProxied;
--or–
move methodWithTx() to another @Autowired bean
--or--
CurrentClass myselfProxied =
(CurrentClass)AopContext.currentProxy();
--or--
transactionTemplate.execute(s->methodWithTx());
https://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo
44 © VictorRentea.ro
a training by
No More Magic
45 © VictorRentea.ro
a training by
Spring + nativeimage?
45
•Serverless with Spring Cloud Function
•Cheaper hosting of your Spring microservices
•Good fit with Kubernetes platforms like VMware Tanzu
https://spring.io/blog/2021/03/11/announcing-spring-native-beta
Niche?
https://github.com/spring-projects-experimental/spring-native
46 © VictorRentea.ro
a training by
nativeimage does NOT support
class-proxies (CGLIB)
Only JDK (interface-) proxies work,
via a Maven Plugin doing Ahead-Of-Time analysis
47 © VictorRentea.ro
a training by
@Cacheable -> CacheManager
@Transactional -> TransactionTemplate
@Async -> executor.submit()
@PreAuthorize/@RolesAllowed
-> SecurityContextHolder.getContext()...
@Retryable -> RetryTemplate
Programmatic Alternatives to Annotations
@VictorRentea
48
Functional Programming vs AOP
"Execute Around" Pattern
transactionTemplate.execute(this::methodInNewTx);
(Use JFR instead)
@VictorRentea
49
The Hamburger Problem
@VictorRentea
50
Average Error (99.9%) Unit
Cache Method 357.7 ± 11.7
us/op
Decorator 355.1 ± 6.0
Interface Proxy 398.7 ± 28.0
Class Proxy 386.9 ± 17.5
Class Proxy / BPP 373.9 ± 36.0
@Cacheable 8109.8 ± 1097.5
Performance
Details? Run it yourself: https://github.com/victorrentea/proxy-fairy-performance
@VictorRentea
51
Writing Custom Aspects
@VictorRentea
52
@annotation(my.proj.Logged)
annotation-based weaving
Custom Aspects - Best Practices
@Around("@within(my.proj.Facade)")
public Object logAround(ProceedingJoinPoint point) { … }
make them visible
@Facade
public class ExpensiveOps {
@Logged
public Boolean isPrime(int n) {…}
}
@VictorRentea
53
Custom Aspects - Best Practices
"execution(* com.mycomp.proj.facade.*.*(..))"
is less magic than
package names
or
class name patterns
"execution(* com.mycomp..*DAO.*(..))"
annotation-based weaving
54 © VictorRentea.ro
a training by
keep them light
Custom Aspects - Best Practices
synchronized
files
database
http
don't intercept insane-rate calls
55 © VictorRentea.ro
a training by
Annotation-based AOP can feel like changing the language
Make everyone in the team comfortable with it
(think twice before changing the proxy default behavior)
Don't fear performance
(as long as you don't apply it to 100% of methods, and you keep them light)
Consider Alternatives
When interfering with the design
The Moral?
find me on Twitter: @victorrentea
Company Training:
victorrentea@gmail.com
Training for You:
victorrentea.teachable.com
Blog, Talks, Curricula:
victorrentea.ro

More Related Content

What's hot

Integration testing with spring @snow one
Integration testing with spring @snow oneIntegration testing with spring @snow one
Integration testing with spring @snow oneVictor Rentea
 
Integration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzIntegration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzVictor Rentea
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixVictor Rentea
 
Definitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaDefinitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaVictor Rentea
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignVictor Rentea
 
Refactoring Games - 15 things to do after Extract Method
Refactoring Games - 15 things to do after Extract MethodRefactoring Games - 15 things to do after Extract Method
Refactoring Games - 15 things to do after Extract MethodVictor Rentea
 
Refactoring blockers and code smells @jNation 2021
Refactoring   blockers and code smells @jNation 2021Refactoring   blockers and code smells @jNation 2021
Refactoring blockers and code smells @jNation 2021Victor Rentea
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Victor Rentea
 
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideEvolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideVictor Rentea
 
Clean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best PracticesClean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best PracticesVictor Rentea
 
Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Victor Rentea
 
Functional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupFunctional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupVictor Rentea
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean codeVictor Rentea
 
A Tale About the Evil Partial Mock and Separation by Layers of Abstractions
A Tale About the Evil Partial Mock and Separation by Layers of AbstractionsA Tale About the Evil Partial Mock and Separation by Layers of Abstractions
A Tale About the Evil Partial Mock and Separation by Layers of AbstractionsVictor Rentea
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
 
JavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionJavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionYoav Aharoni
 
Pharo Optimising JIT Internals
Pharo Optimising JIT InternalsPharo Optimising JIT Internals
Pharo Optimising JIT InternalsESUG
 
Java fx smart code econ
Java fx smart code econJava fx smart code econ
Java fx smart code econTom Schindl
 

What's hot (20)

Integration testing with spring @snow one
Integration testing with spring @snow oneIntegration testing with spring @snow one
Integration testing with spring @snow one
 
Integration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzIntegration testing with spring @JAX Mainz
Integration testing with spring @JAX Mainz
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflix
 
Definitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaDefinitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in Java
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
Refactoring Games - 15 things to do after Extract Method
Refactoring Games - 15 things to do after Extract MethodRefactoring Games - 15 things to do after Extract Method
Refactoring Games - 15 things to do after Extract Method
 
Refactoring blockers and code smells @jNation 2021
Refactoring   blockers and code smells @jNation 2021Refactoring   blockers and code smells @jNation 2021
Refactoring blockers and code smells @jNation 2021
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8
 
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideEvolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best PracticesClean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best Practices
 
Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021
 
Functional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupFunctional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User Group
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean code
 
A Tale About the Evil Partial Mock and Separation by Layers of Abstractions
A Tale About the Evil Partial Mock and Separation by Layers of AbstractionsA Tale About the Evil Partial Mock and Separation by Layers of Abstractions
A Tale About the Evil Partial Mock and Separation by Layers of Abstractions
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
JavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionJavaFX - Sketch Board to Production
JavaFX - Sketch Board to Production
 
Pharo Optimising JIT Internals
Pharo Optimising JIT InternalsPharo Optimising JIT Internals
Pharo Optimising JIT Internals
 
Java fx smart code econ
Java fx smart code econJava fx smart code econ
Java fx smart code econ
 

Similar to The Proxy Fairy and the Magic of Spring @JAX Mainz 2021

Unit testing - 9 design hints
Unit testing - 9 design hintsUnit testing - 9 design hints
Unit testing - 9 design hintsVictor Rentea
 
Testing Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdfTesting Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdfVictor Rentea
 
Profiling your Java Application
Profiling your Java ApplicationProfiling your Java Application
Profiling your Java ApplicationVictor Rentea
 
Software Craftsmanship @Code Camp Festival 2022.pdf
Software Craftsmanship @Code Camp Festival 2022.pdfSoftware Craftsmanship @Code Camp Festival 2022.pdf
Software Craftsmanship @Code Camp Festival 2022.pdfVictor Rentea
 
The tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptxThe tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptxVictor Rentea
 
Invoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicInvoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicAntoine Sabot-Durand
 
From Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptxFrom Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptxVictor Rentea
 
Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev
 Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev
Making Your Own Static Analyzer Using Freud DSL. Marat VyshegorodtsevYandex
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019Paulo Clavijo
 
70-483 Programming in C# Complete Study
70-483 Programming in C# Complete Study70-483 Programming in C# Complete Study
70-483 Programming in C# Complete Studydrovioph
 
Power Shell As A Tools Platform
Power Shell As A Tools PlatformPower Shell As A Tools Platform
Power Shell As A Tools Platformbeefarino
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and backDavid Rodenas
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyHaim Yadid
 
Testing in FrontEnd World by Nikita Galkin
Testing in FrontEnd World by Nikita GalkinTesting in FrontEnd World by Nikita Galkin
Testing in FrontEnd World by Nikita GalkinSigma Software
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvCodelyTV
 
Никита Галкин "Testing in Frontend World"
Никита Галкин "Testing in Frontend World"Никита Галкин "Testing in Frontend World"
Никита Галкин "Testing in Frontend World"Fwdays
 
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit SoftwaretestsEffizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit SoftwaretestsDECK36
 
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey BuzdinMarvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey BuzdinJava User Group Latvia
 
multi-mechanize testing certification
multi-mechanize testing certificationmulti-mechanize testing certification
multi-mechanize testing certificationVskills
 

Similar to The Proxy Fairy and the Magic of Spring @JAX Mainz 2021 (20)

Unit testing - 9 design hints
Unit testing - 9 design hintsUnit testing - 9 design hints
Unit testing - 9 design hints
 
Testing Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdfTesting Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdf
 
Profiling your Java Application
Profiling your Java ApplicationProfiling your Java Application
Profiling your Java Application
 
Software Craftsmanship @Code Camp Festival 2022.pdf
Software Craftsmanship @Code Camp Festival 2022.pdfSoftware Craftsmanship @Code Camp Festival 2022.pdf
Software Craftsmanship @Code Camp Festival 2022.pdf
 
The tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptxThe tests are trying to tell you something@VoxxedBucharest.pptx
The tests are trying to tell you something@VoxxedBucharest.pptx
 
Invoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicInvoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamic
 
From Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptxFrom Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptx
 
Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev
 Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev
Making Your Own Static Analyzer Using Freud DSL. Marat Vyshegorodtsev
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
70-483 Programming in C# Complete Study
70-483 Programming in C# Complete Study70-483 Programming in C# Complete Study
70-483 Programming in C# Complete Study
 
Power Shell As A Tools Platform
Power Shell As A Tools PlatformPower Shell As A Tools Platform
Power Shell As A Tools Platform
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
 
Testing in FrontEnd World by Nikita Galkin
Testing in FrontEnd World by Nikita GalkinTesting in FrontEnd World by Nikita Galkin
Testing in FrontEnd World by Nikita Galkin
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
 
Никита Галкин "Testing in Frontend World"
Никита Галкин "Testing in Frontend World"Никита Галкин "Testing in Frontend World"
Никита Галкин "Testing in Frontend World"
 
Need 4 Speed FI
Need 4 Speed FINeed 4 Speed FI
Need 4 Speed FI
 
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit SoftwaretestsEffizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
 
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey BuzdinMarvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
 
multi-mechanize testing certification
multi-mechanize testing certificationmulti-mechanize testing certification
multi-mechanize testing certification
 

More from Victor Rentea

Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24Victor Rentea
 
Distributed Consistency.pdf
Distributed Consistency.pdfDistributed Consistency.pdf
Distributed Consistency.pdfVictor Rentea
 
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening KeynoteClean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening KeynoteVictor Rentea
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxVictor Rentea
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing ArchitecturesVictor Rentea
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipVictor Rentea
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the DomainVictor Rentea
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipVictor Rentea
 
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...Victor Rentea
 
Clean Code - The Next Chapter
Clean Code - The Next ChapterClean Code - The Next Chapter
Clean Code - The Next ChapterVictor Rentea
 

More from Victor Rentea (12)

Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24
 
Distributed Consistency.pdf
Distributed Consistency.pdfDistributed Consistency.pdf
Distributed Consistency.pdf
 
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening KeynoteClean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
 
OAuth in the Wild
OAuth in the WildOAuth in the Wild
OAuth in the Wild
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing Architectures
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software Craftsmanship
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the Domain
 
TDD Mantra
TDD MantraTDD Mantra
TDD Mantra
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software Craftsmanship
 
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
 
Clean Code - The Next Chapter
Clean Code - The Next ChapterClean Code - The Next Chapter
Clean Code - The Next Chapter
 

Recently uploaded

Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 

Recently uploaded (20)

Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 

The Proxy Fairy and the Magic of Spring @JAX Mainz 2021

  • 1. victorrentea@gmail.com ♦ ♦ @victorrentea ♦ VictorRentea.ro All the code is available on the branch JAX_Mainz on https://github.com/victorrentea/spring
  • 2. Victor Rentea VictorRentea.ro Blog, Best Talks, Video Courses, ... Independent Trainer for companies and individuals Founder of Bucharest Software Craftsmanship Community Java Champion ❤️ Simple Design, Refactoring, Unit Testing ❤️
  • 3. Technical Training 400 days (100+ online) 2000 devs 8 years More: VictorRentea.ro Workshops: victorrentea.teachable.com 50 companies Reach out to me: Hibernate Spring Functional Prog Java Performance Reactive Prog Design Patterns Pragmatic DDD Clean Code Refactoring Unit Testing TDD any lang @victorrentea Intense
  • 4. 34 © VictorRentea.ro a training by 34
  • 5. 3 5 © VictorRentea.ro a training by class A { private B b; ... } class BImpl implements B { m() {} ... } interface B { m(); ... } Interface Proxy has a :A B :BImpl implem Static Runtime
  • 6. 36 © VictorRentea.ro a training by class A { private B b; ... } class BImpl implements B { m() {} ... } interface B { m(); ... } Interface Proxy has a :A B :BImpl implem call Static Runtime implem proxy m() {} Created at run-time with java.lang.reflect.Proxy
  • 7. 37 © VictorRentea.ro a training by class A { private B b; ... } interface B { m(); ... } Class Proxy has a Static Runtime class B { { } :A B :B call proxy m() {} The bytecode of a subclass is generated in memory by (eg CGLIB) extends Different InstanceS
  • 8. 38 © VictorRentea.ro a training by 38
  • 9. 39 © VictorRentea.ro a training by Every time you don’t understand how Spring does something… it's a Proxy @Cacheable @Transactional @Aspect @Async @Secured / @PreAuthorize @Retryable Request/Session/Thread scope … The Magic of Spring
  • 10. 40 © VictorRentea.ro a training by Dependency Injection is great because 1 Classes are easier to break 3 Proxies can be given to you transparently 2 Cleaner to test
  • 11. 41 © VictorRentea.ro a training by :B proxy m() {} Different InstanceS Local Method Calls don't get Proxied! @Override m() {} Class Proxy Pitfalls final ignored: final class crash: The default way in Spring You have to take a reference to B from Spring
  • 12. @ Victo r Rentea 42 •A method of a non-Spring bean? • Think new •A final method/class? • For a class-proxy: •A static method? •Field access? •A local method call? Can Spring AOP Intercept… NO! NO! Spring AOP can only intercept method calls NO! Well… Actually… You know… You can do any of that via: Bytecode Enhancement (compile-time hacking) Or Instrumentation (classload-time hacking) NO!!! NO! How could it be overridden? https://github.com/dsyer/spring-boot-aspectj#
  • 13. @VictorRentea 43 proxy methodWithTx(); myselfProxied.methodWithTx(); War Stories @Transactional(propagation = REQUIRES_NEW) public void methodWithTx() { @Autowired private CurrentClass myselfProxied; --or– move methodWithTx() to another @Autowired bean --or-- CurrentClass myselfProxied = (CurrentClass)AopContext.currentProxy(); --or-- transactionTemplate.execute(s->methodWithTx()); https://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo
  • 14. 44 © VictorRentea.ro a training by No More Magic
  • 15. 45 © VictorRentea.ro a training by Spring + nativeimage? 45 •Serverless with Spring Cloud Function •Cheaper hosting of your Spring microservices •Good fit with Kubernetes platforms like VMware Tanzu https://spring.io/blog/2021/03/11/announcing-spring-native-beta Niche? https://github.com/spring-projects-experimental/spring-native
  • 16. 46 © VictorRentea.ro a training by nativeimage does NOT support class-proxies (CGLIB) Only JDK (interface-) proxies work, via a Maven Plugin doing Ahead-Of-Time analysis
  • 17. 47 © VictorRentea.ro a training by @Cacheable -> CacheManager @Transactional -> TransactionTemplate @Async -> executor.submit() @PreAuthorize/@RolesAllowed -> SecurityContextHolder.getContext()... @Retryable -> RetryTemplate Programmatic Alternatives to Annotations
  • 18. @VictorRentea 48 Functional Programming vs AOP "Execute Around" Pattern transactionTemplate.execute(this::methodInNewTx); (Use JFR instead)
  • 20. @VictorRentea 50 Average Error (99.9%) Unit Cache Method 357.7 ± 11.7 us/op Decorator 355.1 ± 6.0 Interface Proxy 398.7 ± 28.0 Class Proxy 386.9 ± 17.5 Class Proxy / BPP 373.9 ± 36.0 @Cacheable 8109.8 ± 1097.5 Performance Details? Run it yourself: https://github.com/victorrentea/proxy-fairy-performance
  • 22. @VictorRentea 52 @annotation(my.proj.Logged) annotation-based weaving Custom Aspects - Best Practices @Around("@within(my.proj.Facade)") public Object logAround(ProceedingJoinPoint point) { … } make them visible @Facade public class ExpensiveOps { @Logged public Boolean isPrime(int n) {…} }
  • 23. @VictorRentea 53 Custom Aspects - Best Practices "execution(* com.mycomp.proj.facade.*.*(..))" is less magic than package names or class name patterns "execution(* com.mycomp..*DAO.*(..))" annotation-based weaving
  • 24. 54 © VictorRentea.ro a training by keep them light Custom Aspects - Best Practices synchronized files database http don't intercept insane-rate calls
  • 25. 55 © VictorRentea.ro a training by Annotation-based AOP can feel like changing the language Make everyone in the team comfortable with it (think twice before changing the proxy default behavior) Don't fear performance (as long as you don't apply it to 100% of methods, and you keep them light) Consider Alternatives When interfering with the design The Moral?
  • 26. find me on Twitter: @victorrentea Company Training: victorrentea@gmail.com Training for You: victorrentea.teachable.com Blog, Talks, Curricula: victorrentea.ro