I have a boot application and in one of my facades I try to Autowire the conversionService like this:
#Autowired
private ConversionService conversionService;
as a result I get this:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] is defined: expected single matching bean but found 3: mvcConversionService,defaultConversionService,integrationConversionService
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1061)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 16 more
To overcome this I have added a Qualifier lilke this:
#Autowired
#Qualifier("mvcConversionService")
private ConversionService c;
and this all works. However all my custom converters are automatically added the to the mvcConversionService. And now I want to extend the ConversionService and add another method to it, however my converters are again added the to the mvcConversionService. Is there a way to tell spring-boot which conversionService to use to automatically register my converters there? I don't want to manually list all the converters to the new conversionService.
I was experiencing a similar issue. The problem seems to be that you need to define which conversion service you want to use. You can do it by XML or using a Spring Boot configuration.
I'm copying here part of answer to a very similar question (the one that worked for me) and marking this question as possible duplicate.
A look into the Spring documentation reveals, that you should
declare a ConversionService bean. In the XML configuration it would
look like this:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="example.MyCustomConverter"/>
</set>
</property>
</bean>
If its a list of converters you're after and they all implement the same interface you can autowire in a list.
e.g.
#Service
public class MyService {
#Autowired
private List<MyConverter> myConverters;
public void myMethod(MyRawThing thing) {
myConverters
.forEach(converter -> converter.doStuff(thing));
}
}
and them iterate through them as required.
A nice feature of this, is that as new implementations are added, you do not need to update the code that uses them.
Explanation
Spring will attempt to auto-create a ConversionService (in your case mvcConversionService) for the application. It has no qualifiers for what Converters will be registered so if you create a component/bean that implements one of the following types, it will be autoregistered to the service.
Converter
GenericConverter
Printer
Parser
Solution #1
Ignore it. If there is no harm done by registering these converters to the mvcConversionService, then it might not be worth your time to work around this restriction. Your converter classes will likely go unused.
Solution #2
Reproduce your own interfaces. You can copy the interface for ConversionService and Converter and name them how you like. Sure, the functionality is intended to be the same but this will prevent your Converter classes from being injected into any other ConversionService instances.
Bump into this because I was having this issue also with Spring Integration.
I'm not so satisfied with this solution but this is a quick one.
#Configuration
public class ConversionServiceConfiguration {
#Bean
#Primary
public ConversionService rewardBoxConversionService(#Qualifier("mvcConversionService") final ConversionService conversionService) {
return conversionService;
}
}
Problem will arise if you need other ConversionService implementation for some of your logic. But, that can be solved by using #Qualifier annotation.
Related
Noob to Spring. I have 2 files:
foo-servlet.xml which has the following statement:
<bean id:"DAO" class="a.b.data.MyDAO"/>
fooController.java has the following:
#Controller
public class FooController{
#Autowired
private FooDAO fooDAO;
public void setFooDAO (FooDAO fooDAO){ this.fooDAO = fooDAO;}
My question: Is Spring actually replacing / injecting the definition of DAO in the servlet into my FooDAO? I'm trying to understand the 'tie-in' between the bean and how Spring know to substitute that file for my FooDAO in the controller.
Oh, and there is no mention of #Repository or #Component anywhere in this example code...
The XML looks kind of corrupted. I think it needs to be id=DAO
As far as i know: Autowiring is either done via the type or the name and the type.
So when MyDAO implements FooDao your bean will be considered for Autowiring.
But this is just a guess. The code of the Daos and the rest of the configuration would be helpful to give a correct answer to this question.
Understanding Spring #Autowired usage
This might answer your question as well.
I am using tomcat-jdbc pool in default spring-boot setup. I would like to run some custom Java code each time new JDBC connection is established in the pool and before it is used for the first time. How to do it, and if there are several possibilities which one is the best?
To extend already accepted answer, you can use Spring AOP without full AspectJ if you use pointcut as this one:
#AfterReturning(pointcut = "execution(* org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection())")
public void afterConnectionEstablished() {
...
}
Well, I can think of two options:
Create your own wrapper class - either by extending Tomcat's DataSource class or by implementing Java's DataSource interface and delegating to the wrapped DataSource - and then add the logic you want to the desired methods and register a bean in a #Configuration class by manually instantiating your tomcat-jdbc DataSource (for examples on how to do so, refer to DataSourceConfiguration.Tomcat class) and wrapping it with your class.
Create an aspect and use Spring's AOP support to intercept calls to getConnection. Since DataSourceclass is in the javax package, I think you'll have to use AspectJ, and for some examples refer to this link
My suggestion would be to go with the first option, it should give you fewer headaches, here's a small example how you'd define your wrapper bean:
#Bean
public DataSource dataSource(DataSourceProperties properties) {
return new MyDataSourceWrapper(tomcatDataSourceFrom(properties));
}
private org.apache.tomcat.jdbc.pool.DataSource tomcatDataSourceFrom(
DataSourceProperties properties) {
// manual instantiation like in DataSourceConfiguration.Tomcat class
}
Is there a way to specify the default type for Spring #autowired annotation? For example, I have bean ValidatorA and bean ValidatorB that extends ValidatorA.
The ValidatorA is used in some other places and is injected using #autowired.
Is there a way to force Spring to inject ValidatorB instead of ValidatorA without changing the existing code?
The only solution I found at How to override the behavior of Spring #Autowired suggests to exclude ValidatorA from the context.
Is there any other way?
For example, with Guice I would just bind the ValidatorA.class to ValidatorB.class and that will do exactly what I need.
I have no problem to add #Qualifier to ValidatorB but ValidatorA should stay unchanged.
Guice provides ability to define "Providers", is there something similar in Spring? Maybe with #Configuration annotation?
I think you could use primary indicator there. If your bean is defined through annotation, add #Primary to ValidatorB definition. If you are using xml config add primary="true" to <bean> definition.
Is there a way to force Spring to inject ValidatorB instead of ValidatorA without changing the existing code?
Without changing the code, I doubt it very much. You could change the app context or alter the code something like this :
#Component
#Qualifier("B")
public class ValidatorB extends ValidatorA {
}
and then inject like so (there are other syntactic options as well)
#Resource
#Qualifier("B")
private ValidatorA validatorB;
I wanted to understand how exactly the #Autowired annotation works.
import com.test.WorkFlowDAO;
public class ServiceCentralBOImpl implements IServiceCentralBO
{
/**
* Logger for logging functionality.
*/
private static final Log log = LogFactory.getLog(ServiceCentralBOImpl.class);
#Autowired
private WorkFlowDAO workFlowDAO;
.
.
.
}
and the bean is declared in my Spring applicationContext.xml file:
<bean id="workflowDAO" class="com.test.WorkFlowDAO">
</bean>
Upon closer inspection you can see the two IDs in the Java class and the context XML file are different.
workFlowDAO and
workflowDAO
[The letter 'f' is different in the two IDs]
Since my application runs just fine even with this configuration; I wanted to know,
how does #Autowired annotation work so that it does not complain when the bean IDs do not match exactly.
In case of simple bean usage; Spring would have complained of mismatching bean names.
I am running a J2EE application with Spring 3.0.5 on Websphere App Server 7.0
Let me know if any more information is required.
#Autowired matches the beans by type. The ID is not considered.
If you declare another bean of the same type in your XML configuration, Spring would complain about not being able to determine the correct bean.
If you want to use IDs together with #Autowired you can do so by utilizing #Qualifier although #Resource is recommended in this case.
Find some more info on that topic here.
Completely agree with the first comment.
If you want your beans to be autowired by name, you may consider using #Qualifier("givenName").
See for all details:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html
This might be an obvious but I'm having a hard time understanding why we need to define the class of a bean in two places....
From the spring reference manual...
...
<bean id="petStore"
class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
// retrieve configured instance
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
Shouldn't the xml fine be enough for the container to know the class of petStore?
You can use the following method:
context.getBean("petStore")
However, as this returns a java.lang.Object, you'd still need to have a cast:
PetStoreServiceImpl petstore = (PetStoreServiceImpl)context.getBean("petStore");
However, this could lead to problems if your "petStore" bean is not actually a PetStoreServiceImpl, and to avoid casts (which since the advent of Generics are being seen as a bit dirty), you can use the above method to infer the type (and let's spring check whether the bean you're expecting is really of the right class, so hence you've got:
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
Hope that helps.
EDIT:
Personally, I would avoid calling context.getBean() to lookup methods as it goes against the idea of dependency injection. Really, the component that uses the petstore bean should have a property, which can then be injected with the correct component.
private PetStoreService petStoreService;
// setter omitted for brevity
public void someotherMethod() {
// no need for calling getBean()
petStoreService.somePetstoreMethod();
}
Then you can hook up the beans in the application context:
You could also do away with the configuration via XML and use annotation to wire up your beans:
#Autowired
private PetStoreService petStoreService;
As long as you've got
in your spring context, the "petStore" bean defined in your application context will automatically be injected. If you've got more than one bean with the type "PetStoreService", then you'd need to add a qualifier:
#Autowired
#Qualifier("petStore")
private PetStoreService petStoreService;
There's no requirement to specify the class in the getBean() method. It's just a question of safety. Note there's also a getBean() that takes only a class so that you can just look up beans by type instead of needing to know the name.