Search code examples
springservletsjavabeanscode-injectionautowired

Spring Bean and Autowire to datasource


Noob to Spring. I have 2 files: foo-servlet.xml which has the following statement:

    <bean id:"DAO" class="a.b.data.MyDAO"/>   

fooController.java has the following:

@Controller
public class FooController{
    @Autowired
    private FooDAO fooDAO;

    public void setFooDAO (FooDAO fooDAO){ this.fooDAO = fooDAO;}

My question: Is Spring actually replacing / injecting the definition of DAO in the servlet into my FooDAO? I'm trying to understand the 'tie-in' between the bean and how Spring know to substitute that file for my FooDAO in the controller.

Oh, and there is no mention of @Repository or @Component anywhere in this example code...


Solution

  • The XML looks kind of corrupted. I think it needs to be id=DAO As far as i know: Autowiring is either done via the type or the name and the type. So when MyDAO implements FooDao your bean will be considered for Autowiring. But this is just a guess. The code of the Daos and the rest of the configuration would be helpful to give a correct answer to this question. Understanding Spring @Autowired usage This might answer your question as well.