Spring @ Beans Configuration - "Best" Place?

Where is the "best practice" place to host @Configuration beans in a single Spring project? It seems that config is incorrectly installed under /src/main/java , but otherwise the classes will not compile.

How do others approach this problem?

+4
source share
2 answers

It seems that the only solution if you want to separate the configuration from the logic is to add an additional source folder to the Maven configuration:

 <project> ... <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/main/configuration</source> ... </sources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> 

This solution seems to achieve what you want. src/main/configuration will be added to the compilation plugin and will end in target/classes , while in the project source tree they will be placed somewhere else.

+2
source

/src/main/java is the root folder for the java source in the maven project. So this is the correct root folder.

But it is better to use packages in a java project. - Packages are mapped to directories.

Thus, it is often recommended to have some package, for example com.my_domain.my_application.service And this will correspond to the folder /src/main/java/com/my_domain/my_application/service


@Configuration annotated classes are java classes, and therefore they must be compiled. In the mavern project, by default only files in /src/main/java and /src/test/java . If you want to separate the @Configuration classes, this is possible, but not in the src/xxx/resource folder.

The only thing you need to do is decal the additional source directory in maven using maven to build a helper plugin .

  <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.7</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <source>add-source</source> </goals> <configuration> <sources> <source>${basedir}/src/main/javaconfig</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build> 

You now have an additional java /src/main/javaconfig source folder where you can place @Configuration files. The contents in this folder are merged with one of /src/main/java before compilation. (After adding this parameter to pom, you need to instruct eclipse to update the project configuration).

+2
source

Source: https://habr.com/ru/post/1396658/


All Articles