Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Tuesday, April 21, 2015

IntelliJ IDEA, Spring Data Plugin, and recognizing @EnableJpaRepositories Annotation

Today I worked through an IntelliJ IDEA 14.1.1 (THE latest and greatest as of today, 4/21/2015) configuration issue that had me baffled.

I could not get IntelliJ to recognize Spring Data Repositories. 

I am using Spring JavaConfig for a Spring MVC Web Application and have the following code-based config class:


/**
 * Spring JavaConfig Web Application Configuration for Spring MVC App.
 *
 * @author Philip Tenn
 */
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.philiptenn.scholarship")
@PropertySource("classpath:application.properties")
@EnableJpaRepositories(basePackages = "com.philiptenn.scholarship.repository")
public class WebAppConfig extends WebMvcConfigurationSupport {
...
}


My issue was that the @EnableJpaRepositories annotation was not causing the IDE to recognize Spring Data JPA Repositories in my basePackage to be recognized as Spring-managed Repositories.

I have this working in other environments and could not figure out why.

I worked through the following JetBrains guide: Enabling JPA Support.  However, this did not enable IntelliJ to recognize the annotation.

Since I had this exact same project working in a different install of IntelliJ in a VM, I figured that it had nothing to do with my project and had to be something global.

Taking an educated guess, went to plugins.  Found a plugin that was disabled that looked like it could be relevant ... :-)





























After enabling the Spring Data Plugin and restarting IntelliJ ... here was my result (SO HAPPY)!



Monday, April 20, 2015

Spring JavaConfig

I recently learned how to use Spring JavaConfig.  Unfortunately at work, we are still using XML-based Spring Configuration.

I wanted to dig into JavaConfig, so I started using it at home on personal development projects.

Just replaced the full contents of the web.xml file with the following:


package com.philiptenn.scholarship.init;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * @author Philip Tenn
 */
public class Initializer implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

    public void onStartup(ServletContext servletContext)
            throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));

        ctx.setServletContext(servletContext);

        Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
                new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

}


Now, my web.xml file is really skinny ...


  
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         metadata-complete="false">

  <display-name>Spring MVC Application</display-name>

</web-app>


Having such a small XML file, that doesn't even contain a Servlet entry for the Spring MVC DispatcherServlet felt strange, like walking on a tightrope without a safety harness. However, everything seems to be working fine. :-)

Sunday, April 19, 2015

Spring Data JPA Setup

I'm having one of those "build your own lightsaber" moments.

At work, we are using Spring Data JPA and Hibernate.

Everything is nice and configured already, the Repositories, the TransactionManager, the EntityManagerRef.

With everything configured, it's pretty easy to just create a new Repository, write a couple of query methods following the nice findByFirstNameAndLastName  DSL provided by Spring Data, and inject the Repository into a Spring Service Bean.

The hard part is the configuration.  I'm trying to do that from scratch on a little personal project this weekend, to make sure I can do it.

Issue #1: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

Was getting this as a Runtime error immediately when deploying in JBoss Wildfly 8.2.0.Final.

In my Maven POM:


        
        
        
          org.hibernate
          hibernate-core
          4.3.7.Final
          provided
        


According to wildfly.org 8.2.0 Release Notes:

  • Hibernate 4.3.7.Final
Is supposed to be included as a component.

I could get around it by changing dependency scope from provided to compile and the error goes away, but that feels wrong.

I really don't want to package Hibernate into my WAR file when the Application Server I've selected (JBoss Wildfly 8.2.0.Final) provides it.