Reputation: 13501
Where is the 'best practice' place to put @Configuration beans in one's Spring project? It seems wrong putting config under /src/main/java
, but otherwise the classes won't get compiled.
How do others approach this problem?
Upvotes: 4
Views: 404
Reputation: 120851
/src/main/java
is the root folder for the java source in maven project.
So it is the correct root folder.
But it is best practice to use packages in java project. -- Packages are mapped to directories.
So it is very common an best practice to have some package likecom.my_domain.my_application.service
And this would correspond to the folder /src/main/java/com/my_domain/my_application/service
@Configuration
annotated classes are java classes, and therfore they must be compiled.
In a default mavern project, only the files in /src/main/java
and /src/test/java
. If you want to seperate the @Configuration
classes, then this is possible, but not in the src/xxx/resource
folder.
The only thing you need to do, is to decalare an additional source direcory in maven with help of maven build 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>
Now you hava a additional java source folder /src/main/javaconfig
where you can put you @Configuration
files. The content in this folder is merged with the one of /src/main/java
before compiling. (After adding this to the pom, you need to instruct eclipse to update the project configuration).
Upvotes: 2
Reputation: 5836
It seems that the only solution, if you want to decouple the configuration from the logic, is to add additional source folder to the Maven config:
<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>
It seems that this solution will achieve what you want. src/main/configuration
will be added to the compilation plugin and will end up in target/classes
while, in the project source tree, those will be placed somewhere else.
Upvotes: 2