Results 1 to 3 of 3

Thread: mixing constructor autowiring with setters.

  1. #1
    Join Date
    Feb 2007
    Posts
    291

    Default mixing constructor autowiring with setters.

    HI i want to achieve this, what is the best way to do it.


    i have
    Code:
    class Library {
        private BookService bookService; 
    
        private Integer maxCheckout;
    
        @Autowired
        Library(@Qualifier("cablepuff.bookService") BookService bookService) {
             super():
             this.bookService = bookService;
        }
    
        public void setMaxCheckout(Integer maxCheckout) {
            this.maxCheckout = maxCheckout;
        }
    }
    What is the best way to acheive this. I want BookService to be autowired via constructor and maxCheckout to be set by xml definition.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    What is the best way to acheive this. I want BookService to be autowired via constructor and maxCheckout to be set by xml definition.
    By simply doing as you state... Define the bean in xml, dependency, tell the context to use annotations and presto...
    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

  3. #3
    Join Date
    Feb 2007
    Posts
    291

    Default

    Thanks it works.

    *** edit **** never mind i get this error.

    Code:
            <bean class="org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver"/>
    	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    	<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
    i have this defined in my xml, when i start up app i get this error.

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'library' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.los_angeles.Library]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:519)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class
    Last edited by cablepuff; Nov 5th, 2010 at 09:00 AM.

Posting Permissions

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