Is it possible in Spring to create beans using annotations if they have properties set to different bean...
up vote
0
down vote
favorite
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
java xml spring annotations
add a comment |
up vote
0
down vote
favorite
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
java xml spring annotations
Post what you tried, and post the errors you got. And please, generics exist since Java 5, 14 years ago. Stop using raw types.
– JB Nizet
Nov 17 at 15:07
All right, I have edited the question and added what I tried
– FalseScience
Nov 17 at 15:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
java xml spring annotations
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
java xml spring annotations
java xml spring annotations
edited Nov 17 at 15:29
asked Nov 17 at 15:05
FalseScience
519
519
Post what you tried, and post the errors you got. And please, generics exist since Java 5, 14 years ago. Stop using raw types.
– JB Nizet
Nov 17 at 15:07
All right, I have edited the question and added what I tried
– FalseScience
Nov 17 at 15:30
add a comment |
Post what you tried, and post the errors you got. And please, generics exist since Java 5, 14 years ago. Stop using raw types.
– JB Nizet
Nov 17 at 15:07
All right, I have edited the question and added what I tried
– FalseScience
Nov 17 at 15:30
Post what you tried, and post the errors you got. And please, generics exist since Java 5, 14 years ago. Stop using raw types.
– JB Nizet
Nov 17 at 15:07
Post what you tried, and post the errors you got. And please, generics exist since Java 5, 14 years ago. Stop using raw types.
– JB Nizet
Nov 17 at 15:07
All right, I have edited the question and added what I tried
– FalseScience
Nov 17 at 15:30
All right, I have edited the question and added what I tried
– FalseScience
Nov 17 at 15:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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));
}
}
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 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 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 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 at 17:10
1
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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));
}
}
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 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 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 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 at 17:10
1
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
|
show 3 more comments
up vote
0
down vote
accepted
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));
}
}
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 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 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 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 at 17:10
1
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
|
show 3 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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));
}
}
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));
}
}
answered Nov 17 at 15:39
JB Nizet
529k51854987
529k51854987
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 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 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 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 at 17:10
1
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
|
show 3 more comments
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 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 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 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 at 17:10
1
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
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 at 16:38
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 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 at 16:39
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 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 at 16:55
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 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 at 17:10
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 at 17:10
1
1
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
Again, generics were introduced in Java 5, 14 years ago. And you're still using raw types. Why? stackoverflow.com/questions/2770321/…
– JB Nizet
Nov 17 at 17:11
|
show 3 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352449%2fis-it-possible-in-spring-to-create-beans-using-annotations-if-they-have-properti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Post what you tried, and post the errors you got. And please, generics exist since Java 5, 14 years ago. Stop using raw types.
– JB Nizet
Nov 17 at 15:07
All right, I have edited the question and added what I tried
– FalseScience
Nov 17 at 15:30