javaspringinversion-of-controltnsping

BeanFactory and the ApplicationContext


I have this class:

@Service
public class SecurityManagerService implements ISecurityManagerService {


    private final UserRepository userRepository;
    private final RoleRepository roleRepository;
    private final RightRepository rightRepository;

    public SecurityManagerService(UserRepository userRepository,
                                  RoleRepository roleRepository,
                                  RightRepository rightRepository) {

        this.userRepository = userRepository;
        this.roleRepository = roleRepository;
        this.rightRepository = rightRepository;
    }

..
}

I want to create the beans for the test classes:

   <bean id="userRepository" class="eu.lopo.repositories.security.UserRepository" abstract="true"/>
    
    <bean id="securityManagerService" class="eu.lopo.services.security.SecurityManagerService"  >
        <constructor-arg><ref bean="userRepository"/></constructor-arg>
       
    </bean>

but I have the error: Abstract bean cannot be used here


Solution

  • I think it's better you can use Annotation to inject your dependencies. Springboot now provides a very elegant and simple way to inject the dependent service.

    @Service public class SecurityManagerService implements ISecurityManagerService {

    private final UserRepository userRepository;
    private final RoleRepository roleRepository;
    private final RightRepository rightRepository;
    
    public SecurityManagerService(@Autowired UserRepository userRepository,
                                  @Autowired RoleRepository roleRepository,
                                  @Autowired RightRepository rightRepository) {
    
        this.userRepository = userRepository;
        this.roleRepository = roleRepository;
        this.rightRepository = rightRepository;
    }
    

    .. }

    Or you can use @@Autowired either by setter or purely without using constructor . Hope this gives you some clue.