Reputation: 9
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 the 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?
Upvotes: 0
Views: 2186
Reputation: 597016
You gave your answer - you don't have <context:component-scan />
for the service package. If you add it, you'll have annotation autowiring
Upvotes: 3