Question

I had previously used Spring MVC and hibernate annotations in my Google web application project. It is taking some time to start the application after deployment.

For that reason, I am switching to a Spring MVC XML-based approach for the controller only. However, for service and DAO classes, @Service and @Repository annotations remain as is.

In my Spring XML I am doing as like below (there is no bean tag defined for service and DAO classes):

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="com.my.controller.UserController">
    <property name="domainManager" ref="domainManager"/>
    <property name="userProfileDao" ref="userProfileDao"/>
</bean>

Inside UserController, I am not using any @autowired annotation. I am using combination of annotations with XML. Are there any drawbacks of this approach? Am I going about this the wrong way?

Was it helpful?

Solution

The difference is not between using Annotation or XML, it's between Autowiring and "manually injecting beans".

EDIT: @Autowired and XML component scan are doing the same thing.

You can "manually inject" beans with both XML and full Java @Configuration, the equivalent of your example would be :

@Configuration
public class WebAppConfig {

   @Bean
   public UserDao userDao() {
     return new UserDao();
   }

   @Bean
   public UserController userController() {
     UserController ctrl = new  UserController();
     ctrl.setUserDao(userDao());
     return ctrl;
   }

}

The question is quite accurate because the App Engine team itself has revealed that the App Engine runtime was bad at classpath scanning (which Autowiring does to find matches by Class).

The performance loss at instance startup time would occur if you were doing :

public class UserController {

   @Autowired
   private UserDao userDao;

   // ...
}

See this video, especially the question from the Pivotal (Spring framework) contributor : http://www.youtube.com/watch?v=lFarE1hH0ss

Few people know about this issue. Using Spring AOP can even totally crash on the production runtime. See : Using Spring AOP on App Engine causes StackOverflowError

So about your use of XML, there is no "right or wrong". Personally I don't like writing XML since I feel like it's more error prone, but some people like to clearly separate their configuration from their code. I still use autowiring in production, since the startup time is not an issue for me. Do what you and your team feel comfortable with, just keep in in mind the GAE limitations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top