开发者

Why does @Autowired work in my Controller but not in my Service?

In the MyController, @Autowired works fine to pull in myService without getters/setters:

@Controller
public class MyController
{
    @Autowired
    private MyService myService;

But when I try to apply the @Autowired annotation to t开发者_StackOverflow社区he myOtherService field of MyService, I get an error saying it can't find a necessary setter method for myOtherService -- but it works if I fill in the getter and setter methods for this field:

THIS WORKS:

@Service
public class MyService
{
    private MyOtherService myOtherService;

    public void setMyOtherService(MyOtherService myOtherService)
    {
        this.myOtherService = myOtherService;
    }

    public MyOtherService getMyOtherService()
    {
        return myOtherService;
    }

THIS DOESN'T WORK:

@Service
public class MyService
{
    @Autowired
    private MyOtherService myOtherService;

Does @Autowired only work on controllers?


You gave your answer - you don't have <context:component-scan /> for the service package. If you add it, you'll have annotation autowiring

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜