Spring injection not working for constructor-arg - java

I have following java class:
package configuration;
import common.config.ConfigurationService;
public class AppConfig {
private ConfigurationService configurationService;
public AppConfig(ConfigurationService configurationService){
this.configurationService = configurationService;
}
also
public class ConfigurationServiceImpl
implements ConfigurationService, Runnable
{...
and the application context file is as follows:
<bean id="appConfig" class="configuration.AppConfig" scope="prototype">
<constructor-arg ref="configurationService"></constructor-arg>
</bean>
<bean id="configurationService" class="common.config.ConfigurationServiceImpl" scope="singleton" />
<bean id="propertyPlaceholderConfigurer" class="common.config.PropertyPlaceholderConfigurer">
<constructor-arg ref="configurationService" />
<constructor-arg ref="serviceName" />
</bean>
<bean id="serviceName" class="java.lang.String"><constructor-arg value="filter"/></bean>
during initialization I am getting following error and my beans are not initialized:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig' defined in class path resource [conf/applicationContext.xml]: 1 constructor arguments specified but no matching constructor found in bean 'appConfig' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)
While Spring injection works if I modify the java class code as follows:
package configuration;
import common.config.ConfigurationServiceImpl;
public class AppConfig {
private ConfigurationServiceImpl configurationService;
public AppConfig(ConfigurationServiceImpl configurationService){
this.configurationService = configurationService;
}

First of all , you have to know that Spring do not support interface injection, and thats why the code in your first case do not work,because you are passing ConfigurationService which is an interface as the constructor args.
In the second case , you are doing it right by passing the implementation class of ConfigurationService and taking it as the constructor argument.

Just Looking at it, the package name for AppConfig in the Spring configuration does not match the package declared in the Java source. You have "common.config" versus "configuration". It may be that the error text is misleading, that the reason the constructor is not found is that the class itself is not found.

Related

Spring Configuration xml file with two beans of the same type NoUniqueBeanDefinitionException

I am trying to set up a test applicationContext file for running integration tests on a project that I am working on.
I have two classes that have fields that are marked as #Resource. One class I can change and one I cannot as it is imported from a different project that I don't have any permissions to change. I cannot get my configuration file to set these #Resouces fields without giving me an org.springframework.beans.factory.NoUniqueBeanDefinitionException.
Simple example:
appconfig.xml file
...Typical spring setup...
<bean id="baseUrl" class="java.lang.String">
<constructor-arg value="myURL"/>
</bean>
<bean id="supportedLang" class = "java.lang.String">
<constructor-arg value="en"/>
</bean>
Class that uses baseURL, (I have control to change, simplified Version)
#Service("myService")
public class MyService implements AnotherService{
#Resource
private String baseUrl;
public String getBaseUrl(){return baseUrl;}
public void setBaseURL(String baseURL){this.baseUrl = baseUrl;}
}
Class that uses supportedLang (I don't have access to change this class simplified version)
#Service
public class LangSupportImpl implements InitializaingBean, LangSupport{
#Resource(name= "supportedLang")
private String twoLetterSupportedLang;
public getTwoLetterSupportedLang(){return this.twoLetterSupportedLang;}
}
If I don't set up the beans in the application config file I get a no bean defined error instead.
Any help would be greatly appreciated.
Try to use #Resource(name = "baseUrl") in your MyService class. This will tell Spring which exact bean to take and will resolve ambiguity.
Another option is to change XML configuration and add primary="true" to declaration of baseUrl bean

How does Spring inject list as constructor argument

I've got this:
#Named
#Singleton
public class MyDefaultDef {
#Inject
public MyDefaultDef(SomeRef someRef, List<AnotherRef> anotherRefs) {
//...
}
//...
}
Question1: [main] How does work autowiring of List<AnotherRef> anotherRefs as a constructor arg?
I mean if I'd like to replace that bean definition with xml, I had to specify each element of the list. I.e.
<constructor-arg>
<list>
<ref bean="..."/>
<ref bean="..."/>
</list>
</constructor-arg>
But from where Spring takes those elements in case of annotations?
Question2: How to replace MyDefaultDef bean definition to xml?
When Spring needs to autowire a List<SomeBean>, it looks up all beans in its BeanFactory and retrieves all those that are of type SomeBean. It thens creates a List and adds them to it. It then autowires it.
There is no way in Spring XML to do this listing by type. You'll need to compromise. You leave your class as such
public class MyDefaultDef {
#Inject
public MyDefaultDef(SomeRef someRef, List<AnotherRef> anotherRefs) {
//...
}
//...
}
and simply declare
<bean class="com.example.MyDefaultDef" autowire="constructor"/>
Spring will end up using your constructor above.

Autowire a single argument in a constructor

In a constructor with 2 arguments, I want to explicitly wire one and autowire the other.
Something like:
public Obj(Interface arg, Interface arg2) {
// stuff
}
And in the bean definition:
<bean id="objImpl" class="com.work.Obj">
<constructor-arg index="0" ref="interfaceImpl"/>
<constructor-arg index="1" autowire-this-somehow/>
</bean>
Does anyone know of a way ?
Try this:
<bean id="objImpl" class="com.work.Obj" autowire="constructor">
<constructor-arg index="0" ref="interfaceImpl"/>
</bean>
Here, you have specified that bean of type com.work.Obj should created using constructor autowiring. If you specify any constructor arg, then it overrides autowired arg. So index 0 is explicitly provided. Other args which are not explicitly provided will be autowired by type.
Caution: constructor autowiring has the same limitation as byType - Spring won't attempt to guess which bean to autowire when it finds multiple beans that match a constructor arg. Further, if the class has multiple constructors, any of which can be satisfied by autowiring, then Spring won't attempt to guess which constructor to use. You will get an exception in this case.
EDIT: For this to work, other beans of type Interface (constructor arg type) except the one that needs to be autowired, should be marked with autowire-candidate=false
Example:
<bean id="impl1" class="stackoverflow.SomeImpl" autowire-candidate="false"/>
<bean id="impl2" class="stackoverflow.SomeImpl"/>
<bean id="obj" class="stackoverflow.Obj" autowire="constructor">
<constructor-arg index="0" ref="impl1"/>
</bean>
And Obj class:
package stackoverflow;
public class Obj {
public Obj(SomeInterface i1, SomeInterface i2){
System.out.println("i1" + i1);
System.out.println("i2" + i2);
}
}
Here SomeImpl implements SomeInterface. On running impl2 bean is autowired in the second constructor arg i2. The first arg is manually provided in Spring config.

Cannot integrate JMX with Spring application

I have got a SPRING application. When I run
mvn jetty:run
everything is ok.
I would like to use JMX in my project.
I created another project, I tried tutorial for beginners and I was able to see some changes with jconsole.
Now, I want to use JMX in my real project and I would like to use SPRING libraries which manages JMX - following this post
How to integrate JMX with Spring?
I have got a class:
public class MyMainClass {
private int var1;
private int var2;
private TimeUnit var3;
// public getters and setters
public static MyXXXClass<String, Object> getInstance();
}
and in config.xml
<!-- other beans -->
<bean id="myid" class="com.my.package.MyMainClass">
<property name="var1" value.../>
<property name="var2" value... />
<property name="var3" value.../>
</bean>
<!-- other beans -->
I changed few things to make it works with JMX.
I added an interface:
import java.util.concurrent.TimeUnit;
public interface IMyMainClassBean {
public int getVar1();
public void setVar1(int var1);
public int getVar2();
public void setVar2(int var2);
public TimeUnit getVar3();
public void setVar3(TimeUnit var3);
}
I added implements to my class:
public class MyMainClassBean implements IMyMainClassBean {...}
Last thing, I edited my xml file:
<!-- other beans -->
<bean id="myid" class="com.my.package.MyMainClassBean">
<property name="var1" value.../>
<property name="var2" value... />
<property name="var3" value.../>
</bean>
<!-- this bean must not be lazily initialized if the exporting is to happen -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=testBean1" value-ref="myid" />
</map>
</property>
</bean>
<!-- other beans -->
Now, when I start my server, it gives me a lot of exceptions (log is really long, so I copied just a part which I think is the most important).
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.my.package
.MyMainClassBean#3d4395fb] with key 'bean:name=testBean1'; nested exception is javax.management.InstanceAlreadyExistsExcep
tion: bean:name=testBean1
at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:602)
at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
eanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBean
Factory.java:1509)
... 163 more
Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
... 167 more
[WARNING] Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myTask' defined
in class path resource [anotherconfigfile.xml]: Cannot resolve reference to bean 'anotherimport' while setting bean property 'targetObj
ect'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherimport' defin
ed in class path resource [anotherconfigfile.xml]: Cannot create inner bean 'myTotallyAnotherClass' of type [com.my.package.another.MyTotallyAnotherClass]
while setting bean property 'myTotallyAnotherClass'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creatin
g bean with name 'myTotallyAnotherClass' defined in class path resource [anotherconfigfile.xml]: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.my.package.another.MyTotallyAnotherClass]: Co
nstructor threw exception; nested exception is java.lang.ExceptionInInitializerError:
javax.management.InstanceAlreadyExistsException: bean:name=testBean1
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
eanFactory.java:1571)
I have no even idea how to debug it.
Thank you for all your hints.
Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
This is trying to tell you that you have 2 beans with the same name ObjectName of bean:name=testBean1 being register with the MBeanExporter. However, unless there are other XML files or more entries in your beans map then there should not be.
I have no even idea how to debug it.
You could put a breakpoint in the JmxMBeanServer.registerMBean(...) method to see what beans are being registered to see if you can figure out why you are getting a duplicate.
As an aside, my SimpleJMX library is an easy way to export your beans via JMX. There is pretty good Spring support as well. Here are the documentation about using with Spring. There is also a Spring test program which demonstrates what you need to do to get it working. Here's the Spring XML file.
This is happening as the bean is trying to initialize even after its initialized once.
I was facing the same issue with RabbitMQ configuration in Spring TestNG testcases.
Use #DirtiesContext as a class level annotation for every testcase class.
This will create applicationcontext and kill it once the testcase class is run and a new applicationcontext will be created for the next testcase class.
Example -
#Test
#ContextConfiguration(classes = { ApplicationConfig.class })
#DirtiesContext
#WebAppConfiguration
public class ATest extends AbstractTestNGSpringContextTests{
#BeforeSuite
public void setup() throws Throwable {
}
#AfterSuite
public void teardown() {
}
}

SpringMVC: Variables in Annotations

I have the following controller defined:
#Controller
#RequestMapping("/test")
public class MyController extends AbstractController
{
#Autowired
public MyController(#Qualifier("anotherController") AnotherController anotherController))
{
...
}
}
I'm wondering if it's possible to use variables in the #Qualifier annotation, so that I can inject different controllers for different .properties files, e.g.:
#Controller
#RequestMapping("/test")
public class MyController extends AbstractController
{
#Autowired
public MyController(#Qualifier("${awesomeController}") AnotherController anotherController))
{
...
}
}
Whenever I try I get:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.example.MyController] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Qualifier(value=${awesomeController})
I've included the following bean in my config.xml file:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/application.properties</value>
</list>
</property>
</bean>
But the bean doesn't work unless I declare the bean explicitly in the xml file.
How do I do this with annotations??
First I think it is bad practice to make the dependency injection rely on configuration properties. You are probably going a wrong direction trying to do this.
However to answer your question: accessing placeHolder properties requires the dependency injection to be finished. To make sure it is, you can put your code that accesses the property inside a #PostContruct annotated method.
You will need to retrieve the bean manually from the applicationContext using getBean() method.
#Value("${awesomeController}")
private String myControllerName;
#PostConstruct
public void init(){
AnotherController myController = (AnotherController) appContext.getBean(myControllerName);
}
I'm not sure if what you're doing is possible but I can suggest a slightly different approach, but only if you're using Spring 3.1+. You could try using Spring Profiles.
Define the different controllers you want, one per profile:
<beans>
<!-- Common bean definitions etc... -->
<beans profile="a">
<bean id="anotherController" class="...AnotherController" />
</beans>
<beans profile="b">
<!-- Some other class/config here... -->
<bean id="anotherController" class="...AnotherController"/>
</beans>
</beans>
Your Controller would lose the #Qualifier and become something like:
#Autowired
public MyController(AnotherController anotherController) {
...
}
Then at runtime you can specify which controller bean you want to use by activating the corresponding profile using a system property, e.g.:
-Dspring.profiles.active="a"
or:
-Dspring.profiles.active="b"
It may be possible to set profiles based on a property file but you can find out more about Spring Profiles from this post on the Spring blog. I hope that helps somewhat.

Categories