ConflictingBeanDefinitionException : Same class name, different package - java

I have a dao in one package:
package com.mypackage.dao;
#Repository
public class MyDao {
// some code here
}
I have another dao with same class name in a sub package:
package com.mypackage.one.dao;
#Repository
public class MyDao {
// some other code here
}
I have third class where I am trying to inject the bean using autowiring:
import com.mypackage.one.dao;
public class TestClass{
#Autowired
private MyDao myDao;
}
On deployment, I get the following error:
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'myDao' for bean class [com.mypackage.dao.MyDao] conflicts with existing, non-compatible bean definition of same name and class [com.mypackage.one.dao.MyDao]
My applicationContext.xml has:
<context:component-scan base-package="com.mypackage.dao"/>
<context:component-scan base-package="com.mypackage.one.dao"/>
I can not rename the classes as its a legacy application.
What is the solution for this ?
I tried using qualifier, but that is not working.
Added below line:
<bean id="oneMyDao" class="com.mypackage.one.dao.MyDao" />
And changed autowiring to:
#Autowired
#Qualifier("oneMyDao")
private MyDao myDao;
But it still throws the same error.

i think your mixing XML and Java bean configuration for same bean.
your code should be something like this
package com.mypackage.dao;
#Repository
#Qualifier("myDAOBasePackage")
public class MyDao
{
// some code here
}
package com.mypackage.one.dao;
#Repository
#Qualifier("myDAOSubPackage")
public class MyDao {
// some other code here
}
Ues this to inject
#Autowired
#Qualifier("myDAOBasePackage")
private MyDao myDao;

I think that the problem is in definition of beans. You have to give your beans different names.
Please, try to change annotations to #Repository("dao1") for the first dao and to #Repository("dao2") for the second dao. And then try to #Autowire bean using #Qualifier("dao1") or #Qualifier("dao2").
Also you should remove this line
<bean id="oneMyDao" class="com.mypackage.one.dao.MyDao" />
if you use #Repository annotation

Related

How to use #Service annotated class as an #Autowired bean in another class which is outside the component scan package of the Service class?

I have a service interface called ABCService and its implementation called ABCServiceImpl.
ABCService.class
package com.abc.service.ABCService;
public interface ABCService{
//interface methods
}
ABCServiceImpl.class
package com.abc.service.ABCServiceImpl;
#Service
public class ABCServiceImpl implements ABCService{
// implemented methods
}
XYZ.class
package com.abc.util.XYZ;
public class XYZ{
#Autowired
ABCService abcService;
//methods
}
application-context.xml
<context: annotation-config>
<context: component-scan base-package="com.abc.service, com.abc.util"/>
But when I am trying to use the autowired ABCService in class XYZ to access methods in interface ABCService, I get a null pointer exception.
I then removed the #Service annotation from ABCServiceImpl & added the implentation file in the application-context.xml as a bean & created a bean for class XYZ and gave reference of ABCServiceImpl's bean to bean of class XYZ ; it solved the issue
application-context.xml
<bean id="abcService" class="com.abc.service.ABCServiceImpl" />
<bean id="xyz" class="com.abc.util.XYZ" >
<property name="abcService" ref="abcService"></property>
</bean>
but I want to use the #Service annotation itself without explicitly defining a bean in application-context.xml. How do I do it?
If you want to autowire beans without package scanning, or defining them in XML, then your only option is to do it programmatically using the Spring API...
This is possible using
XYZ bean = new XYZ();
context.getAutowireCapableBeanFactory().autowireBean(bean);
Also if you want any #PostConstruct methods to be called, use
context.getAutowireCapableBeanFactory().initializeBean(bean, null);

Spring 3: Autowire a bean declared in xml application context to a #Component bean

I am having an issue with autowiring a bean property in my class declared as a #Component. I've tried a number of different things, but unfortunately I am getting a NoSuchBeanDefinitionException during spring configuration for this particular bean.
Below is a some example code, to model what I have currently.
package com.foo.bar;
#Component
public class MyDeployer implements ApplicationContextAware
{
#Autowired
private ClusterRegistry clusterRegistry; //The bean I am trying to wire
...
}
In my application context, I have the following:
<beans ...... />
<context:component-scan base-package="com.foo.bar" />
<context:annotation-config />
<bean id="clusterRegistry" name="clusterRegistry" class="com.my.implementation.ClusterRegistryFileImpl" />
</beans>
My ClusterRegistryFileImpl class is defined below:
package com.my.implementation;
public final class ClusterRegistryFileImpl implements ClusterRegistry
{
...
}
Note that the ClusterRegistryFileImpl implements the ClusterRegistry interface, and this interface/implementation class has no dependencies. These files also are not using component scan (and are not annotated by #Component, but I wouldn't think they'd need to be if I'm declaring the bean in my appContext.
My integration test looks like
#ContextConfiguration(locations= {
"/com/app/context/path/appContext.xml",
"/com/app/context/path/aDifferentAppContext.xml"
})
public class MyDeployerTest extends AbstractTestNGSpringContextTests
{
#Autowired
private MyDeployer deployer; //class that uses the bean I need
#Test
protected void testDeployerStartup()
{
deployer.startup();
...
}
}
Below is the error:
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.my.implementation.ClusterRegistry] 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)}
Any suggestions or direction would be much appreciated.
Your class
ClusterRegistryFileImpl
should has an annotation ( like #Service if is a service class, or #Repository or #Component).
And i am not sure if the final accessor applies.

Spring injected resource is always null

ISSUE:
I am trying to inject a service into a bean but the service instance is always null.
BACKGROUND:
I have two beans one called from the other. This is how they are defined in XML config:
<context:annotation-config />
<bean class="com.test.MyBeanImpl" name="myBean"/>
<bean id="myService" class="com.test.MyServiceImpl" />
and the beans are implemented like so:
MyServiceImpl.java
class MyServiceImpl implements MyService {
public void getString() {
return "Hello World";
}
}
MyBeanImpl.java
#Component
class MyBeanImpl implements MyBean, SomeOtherBean1, SomeOtherBean2 {
#Resource(name="myBean")
private MyService myService;
public MyBeanImpl() {}
}
QUESTIONS:
Is there some reason related to the fact that my bean implements 3 interfaces that is preventing the Service being injected? If not what other factors could be effecting it?
as you are using annotations Just mark your service class with #Service annotation and use #Autowired annotation to get the instance of your service class
MyServiceImpl.java
package com.mypackage.service;
#Service
class MyServiceImpl implements MyService {
public void getString() {
return "Hello World";
}
}
MyBeanImpl.java
#Component
class MyBeanImpl implements MyBean, SomeOtherBean1, SomeOtherBean2 {
#Autowired
private MyService myService;
public MyBeanImpl() {}
}
also make sure you mention your package name in <context:component-scan /> element in your dispatcher file as
<context:annotation-config />
<context:component-scan base-package="com.mypackage" />
hope this will solve your problem
Make sure the bean you are injecting MyService into is a bean.
/* This must be a bean, either use #Component or place in configuration file */
#Component
public class SomeClass{
#Resource
private MyService myService;
}
Also make sure that within your configuration you have specified that the application uses annotation-based configuration using:
<context:annotation-config/>
Since your using multiple interfaces it may be best to qualify the bean with a name:
<bean class="com.test.MyBeanImpl" name="myBean" />
Then specify the name element on the #Resource annotation
#Resource(name="myBean")
private MyService myService;
Here is a Github Gist that explains these concepts.

Cannot find bean definition , should I annotate an interface too?

I have an interface as show below
public interface UserManager {
void insertUser(User user);
.......
Now I have an implementation class as below
#Service
public class UserManagerImpl implements UserManager {
#Autowired
private UserDAO userDAO;
In my controller
#Controller
public class ExampleGizmoController {
#Autowired
private UserManager userManager;
UserDAOImpl is
#Service
public class UserDAOImpl implements UserDAO {
#Autowired
private SessionFactory sessionFactory;
My application-context.xml
<context:annotation-config/>
<context:component-scan base-package="com.foo" />
which scans all my packages.I have deployed it as war file and when the deployment happens,
The userManager property is not getting autowired to the ExampleGizmoController and shows the error in tomcat as below
Spring-MVC threw load() exception: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.foo.UserManager] 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)}
I was able to make out that autowiring was not happening , even though it was annotation driven and component scanning is turned on. Is there anything else I should do for autowiring to work ?
<mvc:annotation-driven/>
is also required in your config file
Maybe it's stupid... but try to remove implements UserManager from UserManager Impl ..
You should use #Service("userManager"). Its a way of telling Spring that you want to name your UserManagerImpl bean instance with "userManager".
Same error i faced, but i've got one more dao class which retrieves information of user manager. You should add #Repository annotation to dao class.
Your another dao class looks like that;
#Repository("userManagerDao")
public class UserManagerDAOImpl implements UserManagerDao{
public UserManagerDTO createNewUserManager() {
UserManagerDTO userManager = new UserManagerDTO();
userManager.setId(1);
userManager.setFirstName("First Name");
userManager.setLastName("Last Name");
return userManager;
}
}

Spring No matching bean of type, expected at least 1 bean

Getting error
No matching bean of type [foo.bar.service.AccountService] 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)}
my service:
public interface AccountService {
#Service
public class AccountServiceImpl implements AccountService {
So I should get the implementation
Where I am trying to get it :
public class CustomAuthentication implements AuthenticationProvider {
#Autowired
private AccountService accountService;
In my other class also doing same thing, but there it works.
#Controller
public class AccountController {
#Autowired
private AccountService accountService;
When I remove the those 2 lines from my CustomAuthentication, getting no errors.
configuration just incase:
<context:component-scan base-package="foo.bar" />
Only a spring managed object can Autowire another component/service/repository.
public class CustomAuthentication implements AuthenticationProvider need to be spring managed
like
#Controller
public class AccountController
Try annotating the CustomAuthenitcation with #Component
#Component
public class CustomAuthentication implements AuthenticationProvider
and let me know how CustomAuthentication Object is created. CustomAuthentication object should be a proxy obtained by requesting to Spring(ApplicationContext.getBean() or autowired in another bean).
UPDATE:
reason for this error is you have two config files. spring-servlet.xml and spring-security.xml. Beans defined in spring-security.xml is not able to find those in other.
so you should try something like <import resource="spring-servlet.xml"/> in spring-security.xml .
Please try <context:component-scan base-package="foo.bar.*" /> or <context:component-scan base-package="foo.bar.service" />. I believe it would work.
you have to write on AccountServiceImpl class #Service("accountService")

Categories