Results 1 to 4 of 4

Thread: Autowire-Problem

  1. #1

    Default Autowire-Problem

    Hey!

    Following situation:

    There's a service interface UserService

    Code:
    public interface UserService
    {
    ...
    	List<User> findAllUsers();
    ...
    }
    ..and the appropriate implementation UserServiceImpl

    Code:
    @Service
    public class UserServiceImpl implements UserService
    {
    	@Autowired
    	UserRepository userRepository;
    
    	public List<User> findAllUsers() {
    		return userRepository.findAll();
    	}
    ...
    }
    Now I try to write a JUnit test for my service implementation, it looks like this so far...

    Code:
    public class UserServiceImplTest
    {
    	@Autowired
    	public UserService userService;
    	
    ...
    }
    But when I run the test, I get the following error:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'net.twentyfourseven.blog.service.UserServiceImplTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public net.twentyfourseven.blog.service.UserService net.twentyfourseven.blog.service.UserServiceImplTest.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.twentyfourseven.blog.service.UserService] 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)}
    The implentation und interface are within the same package, and I scan my path via

    Code:
    <context:component-scan base-package="net.twentyfourseven.blog" />
    <context:annotation-config />
    Does anyones knows whats the problem here????

    Thank you!

    Markus

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Judging from your code your testcase isn't loading anything... Also why would the context be creating your testcase?!
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default Alternate approach to autowire

    Try to use the following to autowire,

    Code:
    	
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    instead of
    Code:
    <context:annotation-config />

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    That is what annotation-config already does and which isn't needed because it is implied by the component-scan...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •