Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Precedence of XML vs. Annotation configuration

  1. #11
    Join Date
    Jan 2005
    Location
    Phoenix, AZ
    Posts
    139

    Default

    OK, here it is:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<context:annotation-config/>
    	
    	<bean id="dao" class="example.Dao"/>
    	
    	<!-- dao is autowired into service, but maxClients isn't -->
    	<bean id="service" class="example.Service" p:maxClients="2000"/>
    	
    </beans>
    and a couple of Java classes:

    Code:
    package example;
    
    public class Dao { }
    Code:
    package example;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class Service {
    	private Dao dao;
    	private int maxClients;
    	
    	@Autowired
    	public void setDao(Dao dao) {
    		System.out.println("Setting dao=" + dao);
    		this.dao = dao;
    	}
    	
    	public void setMaxClients(int maxClients) {
    		System.out.println("Setting maxClients=" + maxClients);
    		this.maxClients = maxClients;
    	}
    }
    When I load up the context I get the following on the console:

    Code:
    INFO  DispatcherServlet - FrameworkServlet 'contact': initialization started
    INFO  XmlWebApplicationContext - Refreshing org.springframework.web.context.support.XmlWebApplicationContext@3ef810: display name [WebApplicationContext for namespace 'contact-servlet']; startup date [Wed Jul 09 02:42:32 MST 2008]; root of context hierarchy
    INFO  XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/contact/beans-servlet.xml]
    INFO  XmlWebApplicationContext - Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@3ef810]: org.springframework.beans.factory.support.DefaultListableBeanFactory@11c2b67
    INFO  DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@11c2b67: defining beans [org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,dao,service]; root of factory hierarchy
    Setting dao=example.Dao@c5e9c
    Setting maxClients=2000
    INFO  DispatcherServlet - FrameworkServlet 'contact': initialization completed in 891 ms
    Hope that addresses what you're trying to do.
    Willie Wheeler
    Author, Spring in Practice (Manning Publications)
    Spring stuff: Tutorials | Blog

  2. #12
    Join Date
    Jul 2008
    Posts
    10

    Default

    Dear Willie,

    thank you very much for your work. I can't believe it works for you. This is exactly, what I want to do, but for me it don't work.

    Beside this bean I have several other beans, which are autodetected via <context:component-scan>. Perhaps the problem is, that I have this feature set. Does the code still work for you, if you add <context:component-scan>?

    Thank you
    Timo

  3. #13
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    component-scan works ONLY with autowiring so if you have a @Repository bean it will only work with autowiring, configuration in XML for the same bean is ignored (well not exactly it will result in another bean instance).

    Also when you have component-scan you don't need the annotation-config, that is automatically implied.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #14
    Join Date
    Jul 2008
    Posts
    10

    Default

    Hi Marten,

    thanks for clarifying the component-scan issue. But how do you configure the bean properties, which cannot be autowired? Or does component-scan with autowiring just work for beans, which has only autowire-able references?

  5. #15
    Join Date
    Jan 2005
    Location
    Phoenix, AZ
    Posts
    139

    Default

    Timo, you lucky guy. I think I have something for you. :-)

    I didn't realize that you were stuck with the component-scan for other beans. Now I see why that kept popping up in your examples.

    So here's what you can do. Let's say your current application context file is called app-context.xml, and you are forced to keep component-scan in there. Simply add an import there, like this:

    Code:
    	<import resource="example-context.xml"/>
    (OK, I don't know if one line of code justified using code tags, but Marten used the Force on me.)

    OK, now that you have that, create the example-context.xml file, which is just the app context I gave above. Here it is again just to avoid confusion:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    	
    	<context:annotation-config/>
    	
    	<bean id="dao" class="example.Dao"/>
    	
    	<!-- dao is autowired into service, but maxClients isn't -->
    	<bean id="service" class="example.Service" p:maxClients="2000"/>
    </beans>
    Basically this will make the example beans visible to your current application context, and yet it will allow you to isolate those so that you can define them without component scanning.

    Let me know.
    Willie Wheeler
    Author, Spring in Practice (Manning Publications)
    Spring stuff: Tutorials | Blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •