Today, server app developers declare their components using a mixture of technologies that includes atlassian-plugin.xml, Spring XML files, and Spring Scanner. This fragmented approach comes with its own learning curve and an array of pitfalls.
In this talk, Andrew Swan from Atlassian's Server Java Platform team will describe how server app developers can declare their Spring components in pure Java code. This approach is cleaner, more powerful, more flexible, easier to reason about, and more industry-standard. Attendees will also learn about a new Atlassian library that facilitates this approach by providing easy importing and exporting of OSGi services.
Attendees will come away being immediately able to start using Java-based configuration in their server apps. Links to documentation and working sample code will be provided.
11. What are components?
All Server products support
apps
P2 plugins
Apps are made up of
components
Spring beans
12. What are components?
Some components are
internal to the app
Optionally exported to OSGi
All Server products support
apps
P2 plugins
Apps are made up of
components
Spring beans
13. What are components?
Some components are
internal to the app
Optionally exported to OSGi
Some components are
external to the app
Imported via OSGi from product or
other apps
All Server products support
apps
P2 plugins
Apps are made up of
components
Spring beans
33. Plugin XML
How
Cons
Runtime cost
Plugin system transforms your JAR to a Spring-powered
OSGi bundle
Inflexible
Limited to a few XML elements & attributes; no support
for conditions; no control over autowiring
34. Plugin XML
How
Cons
Runtime cost
Plugin system transforms your JAR to a Spring-powered
OSGi bundle
Inflexible
Limited to a few XML elements & attributes; no support
for conditions; no control over autowiring
Unreliable
e.g. problems with Java 9 module-info.class files
35. Plugin XML
How
Cons
Runtime cost
Plugin system transforms your JAR to a Spring-powered
OSGi bundle
Inflexible
Limited to a few XML elements & attributes; no support
for conditions; no control over autowiring
Unreliable
e.g. problems with Java 9 module-info.class files
Poor tooling
No XML schema
45. Spring
Scanner
How (1)
How (2)
Pros
Cons
1. Scan for annotations
@Component, @ComponentImport, etc.
2. Generate “index” files
In META-INF/plugin-components
3. Bundle index files into JAR
Using maven-resources-plugin
46. Spring
Scanner
How (1)
How (2)
Pros
Cons
1. Scan for annotations
@Component, @ComponentImport, etc.
2. Generate “index” files
In META-INF/plugin-components
3. Bundle index files into JAR
Using maven-resources-plugin
4. (runtime) Generate bean definitions
The scan-indexes element in Spring XML activates a
BeanFactoryPostProcessor
53. Spring
Scanner
How (1)
How (2)
Pros
Cons
Long toolchain
Several places where things can go wrong
No good place for @ComponentImport
Separate wiring class, or per parameter?
Clashes between imported services
e.g. Jira’s vs SAL’s ApplicationProperties
54. Spring
Scanner
How (1)
How (2)
Pros
Cons
Long toolchain
Several places where things can go wrong
No good place for @ComponentImport
Separate wiring class, or per parameter?
Clashes between imported services
e.g. Jira’s vs SAL’s ApplicationProperties
DI concerns leak into production code
All components require annotations
55. Spring
Scanner
How (1)
How (2)
Pros
Cons
Long toolchain
Several places where things can go wrong
No good place for @ComponentImport
Separate wiring class, or per parameter?
Clashes between imported services
e.g. Jira’s vs SAL’s ApplicationProperties
DI concerns leak into production code
All components require annotations
Atlassian-specific
New hires require special training
72. Spring
Java config
How
Pros
Define @Configuration classes
Anywhere in your project.
Cons
Define @Bean methods
In your @Configuration classes.
Compose @Configuration classes
Use normal patterns or @Import.
73. Spring
Java config
How
Pros
Define @Configuration classes
Anywhere in your project.
Cons
Define @Bean methods
In your @Configuration classes.
Compose @Configuration classes
Use normal patterns or @Import.
Apply @Conditional annotations
To turn beans on/off.
84. Spring
Java config
How
Pros
It’s Java
Powerful, familiar, great tooling
Cons
Conditions
Great for cross-product or cross-version apps
Simple
Short toolchain
Clean
No DI concerns in production code
96. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Importing an OSGi service
Exporting a service to OSGi
Registering a config class
98. @Bean
public FactoryBean<ServiceRegistration> exportFooService() {
Map<String, Object> serviceProps = …; // you decide
return exportOsgiService(
fooService(), serviceProps, FooService.class);
}
@Bean
public FooService fooService() {
return new FooServiceImpl();
}
Exporting a service to OSGi
99. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Importing an OSGi service
Exporting a service to OSGi
Defining a module type
Registering a config class
107. New Java
config library!
Helper methods
Conditions
Config classes
More
Import an OSGi service (or factory)
Export an OSGi service
108. New Java
config library!
Helper methods
Conditions
Config classes
More
Import an OSGi service (or factory)
Export an OSGi service
Define a module type
110. New Java
config library!
Helper methods
Conditions
Config classes
More
Host product
All the Server products, e.g. JiraOnly.class
111. New Java
config library!
Helper methods
Conditions
Config classes
More
Host product
All the Server products, e.g. JiraOnly.class
Any system property
Checks the system property of your choice
112. New Java
config library!
Helper methods
Conditions
Config classes
More
Host product
All the Server products, e.g. JiraOnly.class
Dev mode
Checks the atlassian.dev.mode system property
Any system property
Checks the system property of your choice
113. New Java
config library!
Helper methods
Conditions
Config classes
More
Prebuilt config classes with beans for:
◦ ModuleFactory
◦ PluginAccessor
115. New Java
config library!
Helper methods
Conditions
Config classes
More
Easier plugin points
Superclass for your ListableModuleDescriptorFactory
116. New Java
config library!
Helper methods
Conditions
Config classes
More
Easier plugin points
Superclass for your ListableModuleDescriptorFactory
Automatic package imports
… of packages required for Java Config
117. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Spring:
◦ org.springframework:
◦ spring-beans
◦ spring-context
OSGi:
◦ org.apache.felix:
◦ org.apache.felix.framework
◦ com.atlassian.plugins:
◦ atlassian-plugins-osgi-javaconfig
NEW LIBRARY!
(OPTIONAL)
118. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
◦ org.springframework.cglib.core
◦ org.springframework.cglib.proxy
◦ org.springframework.cglib.reflect
◦
Import these packages from OSGi:
Tip: if you use our new library, BND will generate these for you.
120. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Keep it simple
No long classes/methods
121. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Keep it simple
No long classes/methods
Be consistent
Choose params or method calls, and stick to it
122. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Keep it simple
No long classes/methods
Be consistent
Choose params or method calls, and stick to it
Be aware
Spring turns your classes into bean definitions
123. Java config in
Server apps
Products
Dependencies
Packages
Tips
Examples
Keep it simple
No long classes/methods
Be consistent
Choose params or method calls, and stick to it
Be aware
Spring turns your classes into bean definitions
Be testable
Create seams in your config classes