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;
}
}
Related
I have a MyService class which has repository field supposed to be injected.
public class MyService {
#Autowired
private MyRepository repository;
// ...ommited
}
And there is MyRepository interface only implemented by MyRepositoryImpl class with #Mapper annotation of MyBatis.
public interface MyRepository {
// ...ommited
}
#Mapper
public interface MyRepositoryImpl extends MyRepository {
// ...ommited
}
When I try to start SpringBootApplication, NoUniqueBeanDefinitionException is thrown.
#SpringBootApplication(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
#MapperScan(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
(...omitted)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'com.example.MyService':
Unsatisfied dependency expressed through field 'repository';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'com.example.MyRepository' available:
expected single matching bean but found 2: com.example.MyRepositoryImpl,com.example.MyRepository
(...omitted)
Why is MyRepository interface registered as one of bean even though it doesn't have #Component annotation nor isn't included any bean configurations?
And I found that everything work fine if I don't use FullyQualifiedAnnotationBeanNameGenerator as nameGenerator.
Any ideas?
There can be many other ways to mark an interface as a bean. Some of them are:
#RepositoryDefinition above MyRepository interface
MyRepository extends CrudRepository or JpaRepository
Check if any of these exist.
Update 1:-
The problem seems to be in #MapperScan. What it does is scans for all the interfaces in a package and register them as bean; and if I am not wrong MyRepository and MyRepositoryImpl are in the same package. That's the reason why 2 beans are being created with names com.example.MyRepositoryImpl, com.example.MyRepository and basically both are of same type as MyRepositoryImpl extends MyRepository.
Now when you are using #Autowired on repository field of MyService, it gets confused as in which one to inject. To resolve this the easiest approach is to use #Primary over MyRepositoy which will give that interface a priority while injecting. Another approach is to use #Qualifier("uniqueName") over both the interfaces and also above repository field of MyService along with #Autowired specifying which one you want to inject. You can visit official documentation from here to learn more.Hope this helps a bit .
Spring is not able to figure out the bean whose class is supposed to be implementing the Repository interface.
Put the annotation #Repository above the RepositoryImpl Class. It will find it.
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
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.
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")
I have a BaseDAO and some subClass eg:UserDaoImpl,OrderDaoImpl,now in a BaseService
public abstract class Baservice{
#Resource
private BaseDao basedao;
//something crud operate.....
}
#Service
public UserService extends BaseService{
//....
}
how to configure Spring that I can pass a real subclass to basedao
I encount a exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.cloudking.trafficmonitor.BaseDAO] is defined: expected single matching bean but found 3: [roleDAO, userDAO
It seems you have 3 classes which extends BaseDAO so all of them are BaseDao (IS A)
You need to use name attribute of #Resource
For example:
#Component
public RoleDao extends BaseDao{/* some code*/}
and
#Component
public UserDao extends BaseDao{/* some code*/}
now if you want to inject UserDao then
#Resource(name="userDao")
BaseDao baseDao
Use #Qualifier("UserDaoImpl")
or use #Resource(name="UserDaoImpl")
Your application is unable to find the bean BaseDAO. You need to annotate BaseDAO with
#Component (or # Service, not sure about this one)