Search code examples
javaspring

migrate Spring app from XML to annotations


I've inherited a Spring 3 app that uses XML files to define and wire together the beans. I know that since Spring 2 these can mostly be replaced with annotations. I would like Spring to:

  • detect beans by scanning certain packages for classes with whatever annotation is used to indicate a Spring bean
  • attempt to autowire-by-name and field in a bean that I have marked with the relevant annotation

What are steps I need to take to make this happen?


Solution

  • The manual has all the info you need: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-annotation-config

    The TL;DR version is that you need to add <context:annotation-config/> to your spring config and then annotate your beans with @Component and in your setter of properties annotate with @Autowired