0

Assume I have the classes A,B,C,D,E:

@Component
public class A {
}

@Component
public class B {
}

@Component
public class D {
}

@Component
public class E {
}

public class C {
private List myList;

public List getMyList() {
    return myList;
}

public void setMyList(List myList) {
    this.myList = myList;
}

}

This is my application context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">


    <util:list id="myList1">
        <ref bean="a"/>
        <ref bean="b"/>
    </util:list>

    <util:list id="myList2">
        <ref bean="d"/>
        <ref bean="e"/>
    </util:list>

    <bean id="myFirstC" class="com.app.C">
        <property name="myList" ref="myList1"></property>
    </bean>

    <bean id="mySecondC" class="com.app.C">
        <property name="myList" ref="myList2"></property>
    </bean>

    <context:component-scan base-package="com.app"></context:component-scan>
    </beans>

Is it possible to create the beans "myFirstC" and "mySecondC" using annotations inside C class? I have tried to autowire the lists inside the C class and then use @Bean annotation to create beans and set the lists, but get errors.

So, I tried to do it in this way:

@Component
@Configuration
public class C {

private List myList;

@Autowired
@Qualifier("myList1")
private List myList1;

@Autowired
@Qualifier("myList2")
private List myList2;

@Bean
public C getNewCBean1() {
    C c = new C();
    c.setMyList(myList1);
    return c;
}

@Bean
public C getNewCBean2() {
    C c = new C();
    c.setMyList(myList2);
    return c;
}

But get error like this:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'c': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.app.C.myList1; nested exception is org.springframework.beans.FatalBeanException: No element type declared for collection [java.util.List]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.app.Start.main(Start.java:9)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.app.C.myList1; nested exception is org.springframework.beans.FatalBeanException: No element type declared for collection [java.util.List]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 13 more
Caused by: org.springframework.beans.FatalBeanException: No element type declared for collection [java.util.List]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:807)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 15 more

I could not find a solution to this in documentations and forums. Any help will be appreciated. Thank you

FalseScience
  • 197
  • 2
  • 17

1 Answers1

0

Your Java code doesn't do the same thing as your xml code at all:

<util:list id="myList1">
    <ref bean="a"/>
    <ref bean="b"/>
</util:list>

<util:list id="myList2">
    <ref bean="d"/>
    <ref bean="e"/>
</util:list>

<bean id="myFirstC" class="com.app.C">
    <property name="myList" ref="myList1"></property>
</bean>

<bean id="mySecondC" class="com.app.C">
    <property name="myList" ref="myList2"></property>
</bean>

This defines two beans myFirstC and mySecondC, containing the beans a, b and d, e respectively. So, in Java, you need two @Bean methods creating the same C beans:

 @Configuration
 public class Config {
     @Bean
     public C myFirstC(A a, B b) {
         return new C(Arrays.asList(a, b));
     }


     @Bean
     public C mySecondC(D d, E e) {
         return new C(Arrays.asList(d, e));
     }
 }
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This solves the problem, but is it possible to use "myList1" and "myList2" instead of (A a, B b) and (D d, E e)? – FalseScience Nov 17 '18 at 16:38
  • You could define two beans of type List, and inject them into your C beans, but why would you want that? – JB Nizet Nov 17 '18 at 16:39
  • Could you also give an example in that way? Thank you. This is actually a part of some youtube tutorial exercise for Spring(without solution), which consists of converting the context application to annotation based one. I am trying to learn the annotation based approach. – FalseScience Nov 17 '18 at 16:55
  • I tried to define the list like this: `@Bean public List myList2() { D d = new D(); E e = new E(); return Arrays.asList(d, e); }` and inject as argument like this: `public C mySecondC(@Qualifier("myList2") List list)` and get error : No element type declared for collection – FalseScience Nov 17 '18 at 17:10
  • 1
    Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – JB Nizet Nov 17 '18 at 17:11
  • Strange, never thought that those warnings could lead to errors – FalseScience Nov 17 '18 at 17:26
  • Still, after reading about raw types and changing List to List because of different types, I got strange results using @Qualifier annotation. So, for example, @Qualifier("myList2") List list was resulting in the list of ArrayLists. To solve the problem, I had to use @Qualifier("myList2") Object list and cast object to List, which is also violating generics rules. – FalseScience Nov 17 '18 at 21:46
  • Solved by using @Value("#{myList2}" instead of @Qualifier("myList2") – FalseScience Nov 17 '18 at 23:18