Intellij incorrectly highlighting error in bean definition with spring - spring

Could not autowire bean should be off type ..NamedPArameterJDBCTemplate
Which is wrong on two counts ... ?

There's a space at the beginning of your class attribute value in your XML file.

Related

How to search bean class if defined by context:component-scan

All:
I wonder if someone defines bean in Java-based configuration way, like use #component etc in class rather than defined it in xml, and use context:component-scan, when I want to find out the implementation of that bean class, how can I find where it is?
Suppose I am using Eclipse.
Thanks
You can either search for the class name (as default the bean name is the same as the class name with the first letter as lower case) or do a full text search for the bean name in Eclipse (you can limit the search to *.java files to speed up the search).

Cannot convert value of type [org.springframework.core.env.StandardEnvironment] to required type

I try to upgrade spring from 3.0 to 3.2.8, but I receive the following error
Cannot convert value of type [org.springframework.core.env.StandardEnvironment] to required type [xxx] for property 'environment': no matching editors or conversion strategy found
I guess its some name conflict to an new class StandardEnvironment, but I don't find any place that defines the property 'environment'
Any help please?
It seems that I have a function called setEnvironment which is in conflict with some spring builtin function setEnvironment which is used to set StandardEnvironment when initialize spring framework, so I changed function name to something else, so it worked. really weird problem.
Here is your issue:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'backOfficeContext' defined in class path resource [se/softronic/appia/cache/DomainSpecificEhCacheBeanPostProcessorTest-context.xml]:
The class for bean 'backOfficeContext' contains property BackOfficeEnvironment environment
So, just change the environment property name to something another, or get rid of default-autowire="byName" and just rely on byType by default.

Bean creation is failing (spring)

I am trying to create a bean and than trying to inject the same in my Controller but i am getting bean creation failure error.Here is my code
#Service("springSecurityLoginServiceImpl")
public class SpringSecurityLoginServiceImpl implements SpringSecurityLoginService
{
//impl
}
this is how i am trying to inject it in my controller
#Controller
#RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{
#Autowired
#Qualifier("springSecurityLoginServiceImpl")
SpringSecurityLoginService springSecurityLoginService;
}
There is no entry in Spring-MVC-config xml file except these annotation, but when i am starting server facing the following exception
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
defined in ServletContext resource [/WEB-INF/config/spring-mvc-config.xml]:
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'springSecurityLoginController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.core.servicelayer.user.SpringSecurityLoginService com.storefront.controllers.pages.SpringSecurityLoginController.springSecurityLoginService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.core.servicelayer.user.SpringSecurityLoginService] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true),
#org.springframework.beans.factory.annotation.Qualifier(value=springSecurityLoginServiceImpl)}
i am not sure what i am doing wrong or what extra i have to do
SpringSecurityLoginController class refers SpringSecurityLoginService class, for which a bean isn't defined. That much the error says.
It is true, because you've only defined a bean for the class LoginServiceImpl, which doesn't seem to extend SpringSecurityLoginService in any way.
Spring's bean lookup algorithm first searches for beans of which type is, or extends, SpringSecurityLoginService. Then, it narrows the avaialble options using the Qualifier. In this case, no bean is found in the first place...
See Spring doc:
4.11.3 Fine-tuning annotation-based autowiring with qualifiers
Since autowiring by type may lead to multiple candidates, it is often
necessary to have more control over the selection process. One way to
accomplish this is with Spring's #Qualifier annotation. This allows
for associating qualifier values with specific arguments, narrowing
the set of type matches so that a specific bean is chosen for each
argument.
You need that LoginServiceImpl will implement SpringSecurityLoginService, for instance.
EDIT
Since it was just a typo you might be not including SpringSecurityLoginService's package in component-scan tag, in your spring configuration file (as gkamal has already mentioned). You should have there something like:
<context:component-scan base-package="org.example"/>
where org.example should be replaced by SpringSecurityLoginService's package.

2 beans with same name but in different packages; how to autowire them?

I have an application that has 2 beans with the same name, but which are in different packages. My Spring application fails because it cannot decide on which bean to take. Is there any solution for this? The beans do not currently implement specific interfaces.
See below an edited example of the exception:
Caused by:
org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'dataTransferHandler' for bean class
[aaaaa.ws.handler.DataTransferHandler] conflicts with existing,
non-compatible bean definition of same name and class
[bbbbb.ws.handler.DataTransferHandler]
You will have to give your beans different names - if multiple beans are defined with the same name, then the one defined later will override the one defined earlier - so in your case only one bean will exist with the name of dataTransferHandler.
You can give these two beans different names, so that both can exist and you can inject in the correct one either using:
#AutoWired #Qualifier("dataTransferHandler")
OR
#Resource(name="dataTransferHandler")
You can give attribute primary="true" to the bean defination you want to have the preference when autowired. But the bean names must be different. There is no solution for same bean name.
At run-time when you will get the autowired class then the primary true bean will get the preference for autowiring. Hope this helps you. Cheers.
I asked another question regarding the same problem, and there is a solution that doesn't require using the #Qualifier annotation: if both of your DataTransferHandler classes have a #Component annotation, you can simply add a String argument to one of their constructions (i.e. #Component("Foo")), and that should solve the problem without needing additional changes.
See User9123's answer on my question for more details.

Spring #Qualifier not working when bean is in another jar file

I have a number of Spring beans, some of which are in a shared library jar. I can't seem to get #Qualifier to work.
I have default-autowire set to "byType", this is using Spring 3.1.0.M2 and running as a standalone executable. If I remove "TestTwoBean" from the shared library the project executes as expected.
myproj-shared-lib.jar:
#Service
public class TestOneBean implements ITestBean {
}
#Service
public class TestTwoBean implements ITestBean {
}
myproj.jar:
#Service
public class TestConsumerBean {
#Autowired #Qualifier("testOneBean")
private ITestBean bean;
}
I get the "no unique bean with name" exception at runtime:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'testConsumerBean' defined in file [-]:
Unsatisfied dependency expressed through bean property 'bean': : No
unique bean of type [com.myco.ITestBean] is defined: expected single
matching bean but found 2: [testOneBean, testTwoBean]; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
unique bean of type [com.myco.TestBean] is defined: expected single
matching bean but found 2: [testOneBean, testTwoBean] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1167)
...
Does #Qualifier not work in this situation? Is there a known workaround?
Are you sure you want to use autowire by type AND annotation injection? Autowire by type means spring will attempt to inject detected setters and constructor parameters using by type lookup even if they aren't annotated for injection.
At the same time you are trying to inject fields by name. Your #Service annotated classes produce beans with names defaulting to the class name, "testOneBean" and "testTwoBean" respectively. #Qualifier uses bean names as correct matches. The recommended way of doing "by name" injection though is by using #Resource(name="testOneBean"). I can only guess spring tries injection by type due to autowire mode set to by type (which I doubt you really need).
I would recommend reverting to default autowire mode and using #Resource for wiring by name.

Resources