sharad-garg
sharad-garg

Reputation: 469

How to stick with both XML and Annotation based configuration Spring

Currently i am using spring 2.5 XML based configuration for bean. Now i want to upgrade it to Spring 3.x. I want to know after upgrading to 3.x my old XML configuration will work or not. If works then can i write annotation based configuration for new work in my current project.

Example of XML configuration:

<bean id="addTestimonialController" class="com.eam.web.testimonial.AddTestimonialController" singleton="true">
        <property name="branchManager" ref="branchMan"/>
        <property name="userManager" ref="userMan"/>
        <property name="itemManager" ref="itemMan"/>        
        <property name="vendorManager" ref="vendorMan"/>
        <property name="categoryManager" ref="categoryMan"/>        
        <property name="lineupManager" ref="lineupMan"/>        
        <property name="testimonialManager" ref="testimonialMan"/>
        <property name="categoryMenuManager" ref="categoryMenuMan"/>
        <property name="setManager" ref="setMan"/>
        <property name="configurationManager" ref="configMan"/>
        <property name="cartManager" ref="cartMan"/>
    <property name="employeeManager" ref="employeeMan"/>
    <property name="employeeBranchManager" ref="employeeBranchMan"/>
    <property name="orderItemManager" ref="orderItemMan"/>
    <property name="orderFaxManager" ref="orderFaxMan"/>
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="addTestimonialBean"/>
        <property name="branchesVendorManager" ref="branchesVendorMan" />       
        <property name="commandClass" value="com.eam.bus.testimonial.TestimonialBean"/>
        <property name="validator" ref="addTestimonialValidator"/>      
        <property name="formView" value="addtestimonial"/>
        <property name="successView" value="listtestimonials.html"/>        
</bean> 

Please help me. Also let me know if you similar link where somebody has explained both the configuration in a single configuration file.

Appreciate your help.

Upvotes: 1

Views: 4498

Answers (2)

kapil das
kapil das

Reputation: 2101

you can use both annotation based configuration and xml based (ControllerClassNameHandlerMapping ) by specifying the order of these handlers.

for annotation based Configuration use below

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
    <property name="order" value="0"/>
</bean>

for annotation based configuration we have to provide location ie: where to locate annotated controllers.

<context:component-scan base-package="ur packageName" />

here package name will be the package where @Controller classes are located.

for Controller Class Name based url Mapping

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >  
    <property name= "order" value="2"/>
</bean>

Upvotes: 1

Keerthivasan
Keerthivasan

Reputation: 12880

You can very well use both XML based metadata and Annotation based configuration metadata in your application. The configuration metadata is the information how you tell the Spring container to instantiate, configure, and assemble the objects in your application. Configuration metadata is traditionally supplied in a simple and intuitive XML format. i.e XML based configuration metadata. Spring 2.5 introduced support for annotation-based configuration metadata.Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you have different ways of providing your configuration metadata of your application through XML, Annotation based and Java config from Spring 3.x versions. This link will take you in the right direction. You must have to learn IOC chapter in Spring documentation

Upvotes: 1

Related Questions