简体   繁体   中英

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.

你必须写在AccountServiceImpl类@Service(“accountService”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM