for object creation – Hollywood Principle: don't call us, we call you • No more hardcoded dependencies • Ease Object Oriented coding – Most times, the user only uses Interfaces, not real classes MyDbService dbS = new DbServiceForVerySpecialDatabase(specParam); MyDbService dbS = new DbServiceForVerySpecialDatabase(specParam);
Someone has to trigger the instance creation - even in a DI environment – But the trigger has no control over the Implementation class nor Instance • 'Singletons', Scopes, Contexts, – Each created Contextual Instance is a 'Singleton' in a well specified Context – Session Singleton, ApplicationSingleton, ConversationSingleton, RequestSingleton, ...
The spec formerly known as “WebBeans” • Started ~ 2007 • Originally designed for J2EE but also usable for standalone applications • Typesafe Dependency Injection
(nonfinal, nonabstract , default ct or annotated ct) – EJB classes – Objects returned by producer methods – JPA and JMS Resources – Web Service References • META-INF/beans.xml marker file
the Contextual Instance will get/use the Context of it's parent • Qualifier @New (no scope!) – Annotation only valid at injection points! – For each injection, a new instance will be created
(beside @Named) is specified • @New – For each managed bean (and session bean), a second Managed Bean with @New exists. • @Any – Always exists but not for @New beans
class Playlist { private @Inject Country ctry; @Inject public setUser(@LoggedIn User usr) {..} public void addTrack(Track track) {..} public List<Track> getTracks() {..} } @ConversationScoped public class Playlist { private @Inject Country ctry; @Inject public setUser(@LoggedIn User usr) {..} public void addTrack(Track track) {..} public List<Track> getTracks() {..} }
public class PlaylistController { @Inject private Playlist pl; public void resortPlaylist() {..} } @SessionScoped public class PlaylistController { @Inject private Playlist pl; public void resortPlaylist() {..} }
needed to create a Contextual Instance @ConversationScoped public class Playlist { public void addTrack(Track track) {..} public List<Track> getTracks() {..} @Produces @Favorit @RequestScoped public Track getFavTrack() { return new Track(favTitle);} public void dropFavT(@Disposes @Favorit Track t) { dosomecleanup();} } @ConversationScoped public class Playlist { public void addTrack(Track track) {..} public List<Track> getTracks() {..} @Produces @Favorit @RequestScoped public Track getFavTrack() { return new Track(favTitle);} public void dropFavT(@Disposes @Favorit Track t) { dosomecleanup();} }
1 Interceptor to the same method • Interceptors have to be enabled in beans.xml or ejb.jar <beans> <interceptors> <class>org.mypkg.TransactionalInterceptor</class> <class>org.mypkg.OtherInterceptor</class> </interceptors> </beans> <beans> <interceptors> <class>org.mypkg.TransactionalInterceptor</class> <class>org.mypkg.OtherInterceptor</class> </interceptors> </beans>
bitte keine private methods! private @Inject Event<UserLoggedIn> loggedInEvent; loggedInEvent.fire( new UserLoggedIn(user) ); private @Inject Event<UserLoggedIn> loggedInEvent; loggedInEvent.fire( new UserLoggedIn(user) ); public void onLogin(@Observes UserLoggedIn loggedIn) { // do something with loggedIn.getUser() } public void onLogin(@Observes UserLoggedIn loggedIn) { // do something with loggedIn.getUser() }
@Named(”myName”) • @Named default name – unqualified class name in camelCase – camelCase producerMethod name (without 'get' or 'is') – camelCase producerField name
meta information at startup – Classparth Scanning and creating Managed Beans metainformation • Contextual Instance creation at runtime – Based on the Managed Beans, the Context will maintain the instances – ServletContextListener, ServletRequestListener, HttpSessionListener, HttpSessionActivationListener
SPI APIs to easily extend it's core in a portable fashion • Enables 3rd party libraries to run on any JSR-299 container • Integration via java.util.ServiceLoader and JSR-299 'Container Lifecycle Events'.