Injecting Spring beans into non-managed objects
Advantages coming from dependency injection can be addicting. It's a lot easier to configure application structure using injections than doing all resolutions manually. It's hard to resign from it when we have some non-managed classes that are instantiated outside of the container - for example being part of other frameworks like Vaadin UI components or JPA entities. The latter are especially important when we're using Domain Driven Design. During DDD training ran by Slawek Sobotka we were talking about options to remove "bad coupling" from aggregate factories. I'm sure you'll admit, that it's better to have generic mechanism able to "energize" objects by dependencies defined by e.g. @Inject annotation than inject all necessary dependencies into particular factory and then pass them into object by constructor, builder or simple setters.
Spring framework brings us two different solutions to achieve such requirement. I'll now describe them both of them. Let's start with with simpler one.
It's especially powerful when we've case such as generic repository, mentioned earlier aggregate factory, or even any other factory just ensuring that we have only few places in our code where will instantiate objects outside of the container. In this case we can make use of AutowireCapableBeanFactory class. In particular we will be interested in two methods:
But what can we do when we have plenty places in our code where objects get born? That's for example case of building Vaadin layouts in Spring web application. Injecting custom bean configurer objects invoking autowireBean method won't be the peak of productivity. Happily Spring developers brought us @Configurable annotation. This annotation connected with aspects will configure each annotated object even if we will create it outside of the container using new operator. Like with any other aspects we can choose between
The second option is a little bit more complex to setup but after this it will be a lot lighter in runtime. Because now we want to load aspects till compilation we have to integrate aspectj compiler into our build. In Maven you need to add few dependencies:
I hope you are curious why above you can see persistence-api. That was also strange for me, when I saw "can't determine annotations of missing type javax.persistence.Entity" error during aspectj compilation. The answer can be found in SpringFramework JIRA in issue SPR-6819. This happens when you configure spring-aspects as a aspectLibrary in aspectj-maven-plugin. Issue is unresolved for over three year so better get used to it :) The last thing we need to do is to include above-mentioned plugin into our plugins section.
Spring framework brings us two different solutions to achieve such requirement. I'll now describe them both of them. Let's start with with simpler one.
It's especially powerful when we've case such as generic repository, mentioned earlier aggregate factory, or even any other factory just ensuring that we have only few places in our code where will instantiate objects outside of the container. In this case we can make use of AutowireCapableBeanFactory class. In particular we will be interested in two methods:
- void autowireBean(Object existingBean)
- Object initializeBean(Object existingBean, String beanName)
The first one just populates our bean without applying specific post processors (e.g. @PostConstruct, etc). The second one additionally applies factory callbacks like setBeanName and setBeanFactory, as well as any other post processors with @PostConstruct of course. In our code it'll look like this:
public abstract class GenericFactory<T> { @Autowired private AutowireCapableBeanFactory autowireBeanFactory; public T createBean() { // creation logic autowireBeanFactory.autowireBean(createdBean); return createdBean; } }Simple and powerful - my favorite composition :)
But what can we do when we have plenty places in our code where objects get born? That's for example case of building Vaadin layouts in Spring web application. Injecting custom bean configurer objects invoking autowireBean method won't be the peak of productivity. Happily Spring developers brought us @Configurable annotation. This annotation connected with aspects will configure each annotated object even if we will create it outside of the container using new operator. Like with any other aspects we can choose between
- load-time-waving (LTW)
- compile-time-waving (CTW).
@Configurable public class MyCustomButton extends Button { @Autowired private MyAwesomeService myAwesomeService; // handlers making use of injected service }
The second option is a little bit more complex to setup but after this it will be a lot lighter in runtime. Because now we want to load aspects till compilation we have to integrate aspectj compiler into our build. In Maven you need to add few dependencies:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency>
I hope you are curious why above you can see persistence-api. That was also strange for me, when I saw "can't determine annotations of missing type javax.persistence.Entity" error during aspectj compilation. The answer can be found in SpringFramework JIRA in issue SPR-6819. This happens when you configure spring-aspects as a aspectLibrary in aspectj-maven-plugin. Issue is unresolved for over three year so better get used to it :) The last thing we need to do is to include above-mentioned plugin into our plugins section.
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.5</version> <configuration> <source>1.7</source> <target>1.7</target> <complianceLevel>1.7</complianceLevel> <showWeaveInfo>true</showWeaveInfo> <aspectLibraries> <aspectLibrary> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </aspectLibrary> </aspectLibraries> </configuration> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin>And that's all folks :)
Comments
public class MyClass{
@Autowired
private SpringManagedType member;
}
https://danjee.github.io/hedgehog/
Hi to every one, it's really a pleasant for me to go to see this site, it contains precious Information. yahoo login mail
JAVA Training in Chennai
JAVA Training in Velachery
Software testing training in chennai
Android Training in Chennai
Selenium Training in Chennai
Hadoop Training in Chennai
JAVA Training in Annanagar
java training in chennai anna nagar
Best online software testing training course institute in chennai with placement
Best selenium testing online course training in chennai
Learn best software testing online certification course class in chennai with placement
Thanks for sharing.
mobile application development training online
mobile app development course
mobile application development course
learn mobile application development
mobile app development training
app development training
mobile application development training
mobile app development course online
online mobile application development
Pega Training in Chennai
Pega Training Institutes in Chennai
Tableau Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
Job Openings in Chennai
Social Media Marketing Courses in Chennai
Primavera Training in Chennai
Pega Training in Vadapalani
Pega Training in Thiruvanmiyur
RPA Training in anna nagar
RPA Training in Chennai
RPA Training in OMRR
DevOps Training in OMR
Web Designing Course in anna nagar
RPA Training in T Nagar
SEO Training in Anna Nagar
ccna Training in OMR
LoadRunner Training in Chennai
hp loadrunner training
best loadrunner training in chennai
Loadrunner Training in T Nagar
Loadrunner Training in Anna Nagar
QTP Training in Chennai
core java training in chennai
C C++ Training in Chennai
Mobile Testing Training in Chennai
Manual Testing Training in Chennai
selenium training in chennai
selenium online courses best selenium online training
selenium testing training
selenium classes
Thanks for sharing.
web designer courses in chennai | best institute for web designing Classes in Chennai
mobile application development course | mobile app development training | mobile application development training online
web designing classes in chennai | web designing training institute in chennai
Web Designing Institute in Chennai | Web Designing Training in Chennai
website design course | Web designing course in Chennai
Corporate TRaining Spring Framework
Project Centers in Chennai For CSE
Spring Training in Chennai
Learn Best Digital Marketing Course in Chennai
Digital Marketing Course Training with Placement in Chennai
Learn Digital Marketing Course Training in Chennai
Digital Marketing Training with Placement Institute in Chennai
Tally Course in Chennai
Tally Training in Chennai
Tally training in coimbatore
Tally course in madurai
Tally Course in Hyderabad
Tally Training in Bangalore
Tally classes in coimbatore
Tally coaching centre in coimbatore
Tally course in marathahalli
German Classes in Bangalore
Handling Solutions
Plastic Pallets
Here is the details of best BSc Medical Imaging Technology Colleges in Bangalore. You can get the college details from the below link. BSc Medical Imaging Technology Course is one of the best demanding course in recent times in India
BSc Medical Imaging Technology Colleges In Bangalore
Christian College Bangalore providing BSc Medical Imaging Technology Course. Here is the link about the details of BSc Medical Imaging Technology. You can click the below link for more information about BSc Medical Imaging Technology.
BSc Cardiac Care Technology Colleges In Bangalore
Christian College Bangalore providing BSc Optometry Course. Here is the link about the details of BSc Optometry. You can click the below link for more information about BSc Optometry. BSc Optometry is one of the most demanding course in recent times.
Optometry Colleges In Bangalore
BBA Aviation course is the best (Most Demanded) management course in India. Here, Christian College Bangalore providing BBA Aviation course. You can get the details of Christian College BBA Aviation from the below mentioned link. If you are interested in BBA Aviation, just visit the below link to know about BBA Aviation.
BBA Aviation Colleges In Bangalore
GrueBleen is one of the Branding and Marketing agency Based in Riyadh- Saudi Arabia. The main functions of GrueBleen is Advertising, Branding, Marketing, Office Branding, Exhibition Management and Digital Marketing. Visit the below link to know more about GrueBleen Creative Club.
Branding Agency Riyadh
Marketing Agency Riyadh
Agriculture Solutions – Taldeen is a plastic manufacturing company in Saudi Arabia. They are manufacturing agricultural plastic products like greenhouse cover and hay cover. Visit the below link to know more details
Agriculture Solutions
Greenhouse Cover
Medical Imaging Technology – One of the most demanding allied health science course in recent times in India. Check out the details of Best BSc Medical Imaging Technology Colleges Details with the following link.
BSc Medical Imaging Technology Colleges In Bangalore
BSc Perfusion Technology – If you are looking to study BSc Perfusion Technology in Bangalore, just check out the following link. In that link you can get the details of Best BSc Medical Imaging Technology colleges in Bangalore
BSc Perfusion Technology Colleges in Bangalore
GrueBleen – One of the best social media marketing agency in Riyadh- Saudi Arabia. Visit here for the all service details of GrueBleen.
Social Media Marketing Agency
Amazing Post. keep update more information.
Selenium Training in Chennai
Selenium Training in Bangalore
Selenium Training in Coimbatore
Best Selenium Training in Bangalore
Selenium Training Institute in Bangalore
Selenium Classes in Bangalore
selenium training in marathahalli
Selenium training in Btm
Ielts coaching in bangalore
German classes in bangalore
Data Science Course in Chennai
Data Science Classes in Chennai
R Training in Chennai
AWS Training in Chennai
Data Science Training in Guindy
Data Science Training in Thiruvanmiyur
Data Science Training in Anna Nagar
IELTS Coaching in Chennai
IELTS Classes in Chennai
german language course
Japanese Language Course in Chennai
Spoken English in Chennai
TOEFL Training in Chennai
spanish institute in chennai
content writing training in chennai
IELTS Coaching in Velachery
IELTS Coaching in Tambaram
DevOps Training in Chennai
DevOps Training in Bangalore
DevOps Training in Coimbatore
Best DevOps Training in Marathahalli
DevOps Training Institutes in Marathahalli
DevOps Institute in Marathahalli
DevOps Course in Marathahalli
DevOps Training in btm
DOT NET Training in Bangalore
PHP Training in Bangalore
Software Testing Training in Chennai
Software Testing Course in Bangalore
Software Testing Training in Coimbatore
Software Testing Course in Madurai
Best Software Testing Institute in Bangalore
Software Testing Training in Bangalore
Software Testing Training Institute in Bangalore
Tally Course in Bangalore
Ielts coaching in bangalore
Aviation Courses in Bangalore
Air Hostess Training in Bangalore
Airport Management Courses in Bangalore
Ground Staff Training in Bangalore
Aviation Institute in Bangalore
Air Hostess Academy Bangalore
Airport Management in Bangalore
Spoken English & Communication Coaching Classes Training in Chennai | Certification | Online Courses
German Classes in Chennai | Certification | Language Learning Online Courses | GRE Coaching Classes in Chennai | Certification | Language Learning Online Courses | TOEFL Coaching in Chennai | Certification | Language Learning Online Courses | Spoken English Classes in Chennai | Certification | Communication Skills Training
Visit us for Customised Credit Card Shaped Pen Drive.
Cyber Security Projects
projects for cse
Networking Projects
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Best Digital Marketing Agency in Chennai
website design in chennai
cloud computing skills
scope of automation
google digital marketing certification
technical skills required for cloud computing
node js questions and answers
giá vé máy bay đi Mỹ khứ hồi
bay từ mỹ về việt nam
vé máy bay giá rẻ đi Los Angeles
vé máy bay từ canada về việt nam
skills for data scientist
latest artificial intelligence technology
ccna certification course
short term courses
cyber security interview questions
java interview questions and answers
gay engagement rings in brighton
brighton based jewellery designer
Best handmade jewellery brighton
bespoke jewellery brighton
best jewellers in brighton
independent jewellers brighton
engagement rings in brighton
pendants for women in brighton
earrings for women in brighton
Illustration jewellery designer in brighton
Diamond Earrings jewelry shop in brighton lanes
antique jewellery shops in brighton
fine jewellery in brighton
cocktail rings in brighton
fashion rings in brighton
brighton contemporary jewellery
the illustrated jeweller
antique jewellery shops in brighton
ring jewellers brighton
jewellery Shop in brighton
wedding rings in brighton
personalised necklace silver
brighton lanes jewellers
handmade jewellery near me
jewellery makers near me
bespoke jewellery near me
gay mens jewellery near me
Illustration engagement rings in brighton lanes
Trending engagement rings brighton
Screening call
No screening means
RPA Training in Bangalore
RPA Training in Pune