aspose file tools*
The moose likes Spring and the fly likes Spring config file question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring config file question" Watch "Spring config file question" New topic
Author

Spring config file question

Ray Clark
Ranch Hand

Joined: Aug 16, 2012
Posts: 55
I have a Spring application that is fully annotated and is working fine. What I would like to do is to take out all of the annotations and put the bean references in the spring config files. I haven't been able to find any example on the web yet for this. I was hoping that if I post 1 example class then someone could reply back with the bean definition for it. That could get me started.

How would the request mapping and the bean injection be done in the dispatcher-servlet or applicationContext XML files for the following class?



I'm going through this exercise because my resource manager said that knowing Spring annotations was good but I would need to know the Spring config files in order to be able to land a position.

Thank you,
Ray
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Hi friend,

Definitely you need to know Spring XML configuration beside the annotation one. The following link provides a useful example:

Spring XML configuration and annotation configuration

Hope that helps.


PM Me If You Want to get Urgent Help on Java Programming
Ray Clark
Ranch Hand

Joined: Aug 16, 2012
Posts: 55
Thank you for the link. I'll go through it to see what happens.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

If you are talking about the @Controller, @RequestMapping and the @ModelAttribute annotations that is exactly what you should be using not XML configuration. This is the accepted way of wiring up controller since Spring 2.5. Unless you plan on extending old deprecated Spring Controller classes trying to do this in XML seems inappropriate to me. I would ask your Resource Manager for clarification on exactly what he is looking for.

Are you using Java Config rather than XML config? If so maybe that is what he wants to see you convert. Or perhaps he does not want you to @Autowire in dependencies or use component scans. In that case you can do this manually in XML




Even in the simple example above you can see how verbose this gets you will have to create beans for the controller and every dependency needed by the controller and then wire those dependencies into the controller. I much prefer the approach you have taken, but it is still important to know both as many projects you will enocunter will define all the beans in xml like this and you will have to know how to read and wire beans using XML. Apart from a potential performance gain on startup for large package scans I think it is still an overly verbose approach vs the annotation and component scan based variation though.


[How To Ask Questions][Read before you PM me]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

FYI if he is hell bent on doing the handler mappings without annotations you will not find any help in the newest Spring documentation and even in the version 3.0 documentation you will find only this http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor
if you want more detail you will have to go back to 2.5 Spring documentation. Basically you will have to settle for a less powerful and flexible controller handler mapping then what you have shown above. This is possible (it was how all controllers were wired pre Spring 2.5) but that does not mean it should be done.
Ray Clark
Ranch Hand

Joined: Aug 16, 2012
Posts: 55
Thank you for the reply. During the technical interview with this consulting company I said that I was learning Spring and felt pretty good about Spring annotations but hadn't messed with the older way of doing it with the config files. The guy doing the interview said that I should know both ways because I very well might have to work on legacy code that was done with the config files. So right now they want me to study up on how to do Spring via a config file.

With the link that was first posted on this thread I just about have the controllers all wired up. (Had to take a break to watch my favorite NFL team lose.)

Bill, I see that you are right that I'll have to rework my controllers because of the change to the controller. This is what I have the controller looking like now. There is just 1 method that the IDE said that I needed to implement so I'll have to figure out how to tell the difference between the initial display of the page (GET) and a submittal of the page (POST). I haven't thought about that yet, I'm still wiring up all of the classes into the config file.



Thank you all for your help.

One question, how often do you do work on apps with config files and how often do you do work on annotated apps?

Ray
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Ray Clark wrote:

One question, how often do you do work on apps with config files and how often do you do work on annotated apps?



Personally, looks like I always work with config file based projects, but sometimes we mix both XML and annotation together. XML is preferred over annotation, since it has many advantages over annotation, as pointed out by Bill.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Legacy apps are about the only excuse to be doing this stuff. I will say if it is using Spring 2.5 the application should be using the @Controller style, but you often will find Spring 2.5 applications that are not. Have a look (or google) for examples using SimpleFormController. This might aide you a bit in your endeavors.

I may integrate with legacy systems or architect solutions to replace them but I am no longer the guy maintaining them and rarely am I the guy enhancing them There is a lot of legacy code out there though and if it works fine business typically does not see any ROI on updating the code base.

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

Nam Ha Minh wrote:
Personally, looks like I always work with config file based projects, but sometimes we mix both XML and annotation together. XML is preferred over annotation, since it has many advantages over annotation, as pointed out by Bill.


Actually I never said XML has many advantages over annotations I did say that the component scan can come with a performance penalty but that penalty happens once at startup and in majority of cases I find it far preferable to declaring a million beans. Whether you declare all your beans in one @Configuration class (or several) or in one XML configuration class (or several) you have the advantage of keeping them defined in groupings that make sense. I actually think the Java Config wins out here because of its naturally better support in the IDE with ctrl click and content assist etc. I prefer to read java code to reading XML. The new @Enable annotations introduced in Spring 3.1 offer most of the things that the custom spring name spaces afforded you. In some cases annotations like @PropertySource do not really have an easy equivalent in XML. Going the other direction there are still some Spring projects like Spring Security where I find it easier still to use the XML due to all the extra bean wiring I must do since java config is not yet available in this project. Other projects like Spring Integration support annotation and XML and I usually use a mix of both. I prefer the XML for my channels and endpoints because I find its easier to read and if you are using a good IDE you can get a visual graph of the integration.

That said the Stereotype annotations such as @Service and @Repository etc are very powerful and can save a lot of time especially when used in conjunction with component scanning. This also makes things a lot less verbose and in my mind more maintainable. I prefer using the annotation based field injection (@Resource, @Inject or @Autowired) as well.
Ray Clark
Ranch Hand

Joined: Aug 16, 2012
Posts: 55
Bill Gorder wrote:Have a look (or google) for examples using SimpleFormController. This might aide you a bit in your endeavors.


Per your suggestion I did a quick search for this controller. It comes with a method isFormSubmission that is what I was looking for. Should be easy now for me to convert my annotated controllers over to xml config controllers.

Thanks again Bill.

Ray
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring config file question