Women in Technology

Hear us Roar



Article:
  Using Dependency Injection in Java EE 5.0
Subject:   So what's new ?
Date:   2006-01-05 00:01:12
From:   GerritCap

Well this article is my first encounter with the new EJB specs and it is rather early in the morning so I might miss some important issues here but basically I don't see any difference between

Context ic = new InitialContext();
HelloWorld helloWorld = ( HelloWorld)ic.lookup("java:comp/env/ejb/HelloWorld");

and

@Resource(name="ejb/HelloWorld")
private HelloWorld helloWorld;


Okay, it looks different and you don't seen any InitialContext magic but to me it is still the same, this is not inversion of control as I appreciate it from Spring.


For me the prime reason to use IOC is that in a certain location I want to use an object implementing a certain interface but without having to determine at that location what object (of what exact concrete type) I am going to use. This functionality is basically already provided in the old J2EE specs as you lookup an object by name in the J2EE context. It is in the J2EE config where you define 'the" object suited to become the object implementing the desired interface. The same type of object lookup is also available in Spring by using its ApplicationContext and looking up a POJO bean by name.


Still the problem here is that this is not really IOC, its is an enhanced version of an implementation of the Interface Design Pattern. The problem as I see it is that sometimes you suddenly need 2 different objects of different types implementing a certain interface instead of just one. And if you developed your application by looking up objects by their configuration name. Example: an application uses a datasource to retrieve and store data. The data source is retrieved from the runtime environment using a single name AdventureDB. But then for some architectural reason 2 different databases are put into use and thus 2 differently configured datasources with 2 different names. Ok, it is simple to alter the code to use a different name in the lookup process. But if you used real IOC this wouldn't be necessary.


This is my second reason to use IOC in its full glory: in a spring based application where I need a datasource I provide a setter method to inject a datasource and in my spring config I define for a lot of different objects which datasource they need to use. Okay okay, if one object suddenly needs to use 2 different datasources this scheme also needs modification then.


Maybe the @Resource javax.ejb.TimerService ts; is a J2EE example of a more complete IOC as I just described it. But then the article is missing one element: where do you define the resource ? E.g. where in the new J2EE do you define the real class of which the resource is going to be a runtime instance of?


And finally one last remark which is not the issue of the article "Using dependency injection", where in J2EE 5.0 can I find Aspect Oriented Programming, which is the third reason I like Spring way over J2EE 1.4.


On the whole: a nice article on J2EE 5.0 but regarding J2EE 5.0 itself: too little too late.


Just my $.02

Full Threads Newest First

Showing messages 1 through 7 of 7.

  • So what's new ?
    2006-01-05 13:12:24  DebaPanda [View]

    But then the article is missing one element: where do you define the resource ? E.g. where in the new J2EE do you define the real class of which the resource is going to be a runtime instance of? Here is what I've in the article:

    To use a field injection, simply define a field and annotate it to be a resource reference. If you don't define the resource's name and type, the container will derive this information from the field's name and type.

    You must have a resource e.g. DataSource configured in the data-source. Here is what I've in the article "In this example, a DataSource called AdventureDB should be available; if not, the container will throw an exception"

    I do not have any thing against Spring. I agree it has some demerits against containers like Spring but makes your code and configuration portable across any Java EE container. Spring configuration being proprietary is not portable and you have to stick with Spring.

    Setter injection is also supported with Java EE. If you are an XML fan you use XML instead of annotations as I described in the article. Also note that XML overrides annotation so you change your resource being injected.

    Anyway I don't think Spring is being touted as an alternative to Java EE, Spring is touted as a framework to be used with an Java EE container

    -Debu

    • So what's new ?
      2006-01-06 03:38:56  DavideBaroncelli [View]

      I do not have any thing against Spring. I agree it has some demerits against containers like Spring but makes your code and configuration portable across any Java EE container. Spring configuration being proprietary is not portable and you have to stick with Spring.

      Uhm. My application runs well on Tomcat, WebSphere and WebLogic, and the only configuration files I have to change when I put it on one server or the other are specific to the implementation of the particular "standard" server: my Spring configuration remains untouched, and Spring actively shields me from part of those platform-specific configuration changes. So, what is the point of talking about the "platform dependence" of a Spring configuration? I use oscache as my caching solution, and so I do rely on a oscache.properties file: this does not make my app less portable. Portability is about different things: portability is about being able to run an APP on different platforms, it is not about being able to switch libraries at any time. Now that you've "standardized hibernate" I will be able to run EJB 3.0 persistence apps by switching the persistence provider when I do change my application server: is this "more portable" than what I can have now by moving my hibernate-based application on different application servers? I strongly doubt so: especially in the first months after the implementations will have come out there will surely be a number of subtle incompatibilities, not to mention the implementation-specific features that will probably be needed to manage more complex cases. I do absolutely agree that something standard like EJB3 persistence was needed, but "portability" is not a good argument for those kinds of standardizations, and the fact that the Spring team, their achievements and their lesson have been ignored in this iteration of JEE is frankly disappointing. I will probably stick with Spring, while doing JEE 5: as you say it's a framework to be used with a JEE container.

      • So what's new ?
        2006-05-28 03:23:01  Gargamelle [View]

        Well, Ive just started with all this Java server side frameworks, but I disagree about J2EE having ignored competitors lessons. In fact, dependency injection - POJO testable objects seems spring based, annotations configuration use seems xdoclet based and persistence API seems a lightweight solution Hibernate-JDO based. Still, J2EE has dependency on EJB Container if you use EJBs. But you can use the Persistence API absolutelly stand alone from EJB Container, albeit you could need remote beans, then you must use the EJB Container.
        I think J2EE team has been aware of their own mistakes and advantages and have tried to merge all the successful ideas into one strong especification. Any way I already have to investigate more!
    • So what's new ?
      2006-01-11 00:28:28  GerritCap [View]

      A few remarks:


      1. Often the mnemonics J2EE and EJB are used interchangably. But I think that J2EE stand for enterprise computing as a step in the evolution of stand alone apps -> client/server -> 3-tier apps. A more flexible way of organising your code. EJB is an implementation of that flexible organisation and delivered by tools like WebSphere, JBoss, WebLogic, Geronimo, ... So where you talk about a J2EE container you should really mention an EJB container.

      2. If I understand your article and rely correctly using field injection you either specify the name of the resource or the EJB container derives the name from its declaration. And binding the resource is done by the EJB container based upon that name.


        Well to me this is conceptually exactly the same as requesting the EJB container for a bean with a certain name although you don't program it that way.


        Again if I understand it correctly, setter injection isn't basically that different. The EJB container again sets the resource based upon the name of the setter.


        So where is the conceptual difference of looking up a bean by its name or the EJB container binding a resource to your objects based upon your name? This is a very limiting IoC strategy, like having inheritance in a OO language without polymorphism. The basics are there but you don't go all the way. Example what if you have 2 DAO's that programmatically use the same name for a data source resource but in a certain setup they would have to use 2 different datasources.

        In a conceptually complete IoC implementation (like Spring) you define a DAO object, you define a datasource and you link (or wire) them together. If tomorrow you need a different datasource for that bean then you wire another datasource to it indepedant of the fact of your original datasource still being used (or wired to) another dao.

      3. Talking about proprietary or portability:
        • Using Spring all of your java code never has to reference any Spring related class or whatsoever. Every object can be a POJO. You can use Spring to set a specific LayoutManager in a JPanel. Using EJB if you want to make an object persistent then it has to be a subclass of javax.ejb.EntityBean.

        • Spring configuration is done using an XML file, if for some reason you don't like spring anymore then you can do the wiring hard coded for example or you can interpret the same XML structure and do the wiring based upon your own interpretation. Or if there is a particular part you don't like about Spring then use it's source code and change it the way you like it. In a project I used to work on there was one particular thing we didn't like about EJB but a) we could not change the EJB container and b) all of our classes where already dependant to the javax.ejb hierarchy so we had 2 choices: delete all code and start all over without EJB or shut up and put up with the EJB situation.

        • Ever tried to migrate an EJB project from WebSphere to WebLogic or vice versa ? In every implementation of an EJB container there are some "additions" the are completely propriatary. Once I tried using a java webstart app and using the "original" java runtime environment to connect to a WebSphere EJB Session bean. You needed a IBM JRE including some IBM dll's on windows or a "patched" Sun JRE for the reaqson of having some portability between ibm's corba and ibm's rmi implementation usin in ejb's. Go figure. What are all these "Controls" you use in a WebLogic environment, you think they are portable to WebSphere ??? Why is there an org.jboss.ejb.EnterpriseContext ???

          Get real: every commercial EJB implementation makes money on you using their implementation so they will add some "features" only available on their platform (which usually makes your life easier in certain situations I agree) but once you use these features typically the cost of migrating to another EJB implementation is enourmous compared to the promise of "portability of EJB's". I know of a highly payed (and thus costly) consultant of company XYZ make a lot of money doing nothing but migrating customers to the XYZ EJB platform.


      4. I agree: Spring is not touted as an alternative to J2EE but as an alternative to EJB and definitly not supposed to be used within a EJB container. Read the book of Rod Johnson & Juergen Hoeller (the 2 most important persons behind Spring in my humble opinion) called "J2EE development without EJB". Can it be more clear?
      5. And basically Spring is not a framework, it are a number of different frameworks

        1. Spring core: used to wire plain POJO's implementing the full conceptuial meaning of IoC and allowing you to set properties of objects

        2. AOP implementation: add aspects of your logic to your code wihtout implementing them. Typical example is transaction management.

        3. JDBC support: using Spring's JdbcTemplates or use O/R tools like hibernate but not restricted to hibernate

        4. MVC web framework: going that one important step futher than struts. But allowing you to use struts, struts tiles and/or java server faces.

        5. Integration with EJB: yes you can integrate it with EJB for those who can't avoid it I guess

        6. JMS, Mail, JMX, job scheduling,...





      Gerrit
  • So what's new ?
    2006-05-28 03:10:58  Gargamelle [View]

    Using Spring all of your java code never has to reference any Spring related class or whatsoever. Every object can be a POJO. You can use Spring to set a specific LayoutManager in a JPanel. Using EJB if you want to make an object persistent then it has to be a subclass of javax.ejb.EntityBean.

    Umm... have do you read we are talking about J2EE 5.0?
  • So what's new ?
    2006-03-28 23:23:44  NidhiTuli [View]

    The article didn't explain in details with example How to use dependency injection using XML in EJB3. Would it be possible to get more info or some links for the same. In our application we want to use dependency injection but at compile time we don't know which class it would be, so we can't use annotation. Doing dependency injection usin XML fits to our needs. Please suggest some example for the same.

    Thanks
    Nidhi
  • So what's new ?
    2006-05-05 00:22:13  pbpanther [View]

    The difference between these 2 is unit testing the bean/servlet.

    To test with an InitialContext you have to first set up a test context somehow then insert your mock/stub object into the the context.

    With dependency injection there is no dependency on an InitialContext in your code, so all you have to do is inject your mock/stub implementation of HelloWorld and you are testing.

    The reason for POJOs generally is so that you can unit test your code thoroughly and even program using TDD.