Digital Dave
Digital Dave

Reputation: 23

How to work with spring annotations in large applications?

If you are starting a Spring project, you may know where you have placed the @controller, @requestmapping annotations and other annotations and the corresponding controller class.

Lets say few months down the line a new developer comes into your team, how would he figure out which classes to work with? Because unlike the xml based approach where all the configurations is centrally located in an config.xml file, we don't have nothing of that sort with annotation as per my knowledge (I am new to spring), we write the respective annotations in a class itself.

Upvotes: 0

Views: 65

Answers (1)

denzal
denzal

Reputation: 1253

In Spring, two ways to define your configuration :

  • Either in a XML file or
  • Java class

So just like xml, in Java also you need to create a configuration class which will have all the required configurations details. You can define all of your beans, filters etc here.

For example , You create a configuration class MvcConfig with the below annotations

    @Configuration
    @EnableWebMvc
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @ComponentScan(basePackages = {"com.abc.example"})

Now your base package is com.abc.example. In all the applications, the best practice is to keep all of you controller\service\DAO classes in specific packages like

    Controller : com.abc.example.controller,
    Service : com.abc.example.service,
    DAO : com.abc.example.dao

So anybody who comes in will know where are all the respective classes located and from where to start.

Example configuration class :

    @Configuration
    @EnableWebMvc
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @ComponentScan(basePackages = {"com.abc.example"})
    public class MvcConfig extends WebMvcConfigurerAdapter

Upvotes: 1

Related Questions