IntelliJ and Spring Data?

I have seen numerous blogs indicating that "it should be easy to setup Spring with IntelliJ" as the IDE automatically detects if you are using Spring . . .  My question is how do I setup a project to use Spring before I have "started using it". What dependencies should I set up with Maven, etc.?

Thank you.

1 comment
Comment actions Permalink

For maven, add what ever Spring dependencies you need. spring-context at a minimum.

To configure IDEA, you need to add a Spring facet to each module that uses Spring.

Option 1 - the pseudo manual way

  1. Add the necessary maven dependencies and 'reimport' the maven project
  2. Create a Spring context file (on the classpath such as in the resources directory)
    • It can just be a skeleton. As long as it has the <beans> tag and the Spring namespace declaration
  3. Click the "configure" link in the popup that states: "Frameworks detected: Spring framework is detected in the project"
    • If you miss the popup, go to the event log tool window, which is located in the bottom right by default and click the "configure" link for the notification there.
  4. In the "Setup Frameworks" dialog, click OK since the context file you just created should be pre-selected.
  5. Start developing with pleasure.
  6. When you add new context files, Spring will prompt you to configure it. See step 5 in Option 2 below for more detail on this.

Option 2 - The fully manual way (and adding additional support)

  1. Add the necessary maven dependencies and 'reimport' the maven project.
  2. Go to  File > Project Structure > [Project Settings] > Modules.
    • Note that you can also go to the "facets" section. It's just a matter of whether the items are grouped by module, or by facet type.
  3. Select the module you want to have Spring support for.
  4. Click the Add icon at the top (or type Alt+Insert; or right click the module and select Add). Select "Spring" from the list of facets.  
  5. In the Spring Facet panel, click the add icon to add a new context mapping.
    • A context mapping is basically the group of files you load into spring when initializing. It creates relationships and/or dependencies among files. If you use imports in your context files, IDEA will auto discover the dependency relationship. But for dependencies that are configured a run time merely by loading a group of files, you need to create a context group so IDEA knows about the relationship
    • In the context mapping popup, select the spring context file(s) that make up the context for that module.If you do not yet have any, you can come back later and add them manually, or...
    • In the future, if you add an additional context file, you need to add it to a mapping, You can do that by coming into this dialog manually via step 1. More conveniently,  IDEA will prompt you via a yellow banner strip to add the new context file to a mapping. You can just click the "Configure Application Context" and either add it to a new mapping, or an existing one. If you select new mapping, you get in a popup the same dialog you got coming in via the project structure.
  6. If you want to add additional Spring support (such as Spring Data, Spring Batch, Spring Integration, etc.) follow steps 3 & 4, but select the appropriate facet and configure.
  7. Save and start developing with pleasure.

Please sign in to leave a comment.