Im trying to build an application based on wicket, Spring jpa repositories, and Mysql.
The problem is, my Service classes just don't want to autowire. I got a nullpointer exception if i want to use my autowired class.
In fact, i got double nullpointer: My BaseServiceConfig.java doesnt autowire the implementation, and the autowired repository in the itnerface is also null. So, the question is why.
public final class ArmyBuilderPanel extends Panel { @Autowired DiskService diskService; //--> frist null, before setting it. public ArmyBuilderPanel(String id) { super(id); add(new Label("armyPanel", "Content placeholder: Army builder panel")); diskService = new DiskServiceImpl(); diskService.save(new Disk()); //--> second nullpointer } }
Im trying to do this with minimal to no xml, all the coniguration was done in java classes. Let me show you the code:
Repository interface:
@Transactional public interface DiskRepository extends JpaRepository<Disk, Long> { }
Service itnerface:
public interface DiskService { public Disk save(Disk entity); }
Service impementation:
@Service public class DiskServiceImpl implements DiskService { @Autowired DiskRepository diskRepo; @Override public Disk save(Disk entity) { return diskRepo.save(entity); } }
BaseServiceConfig.java
@Configuration
@EnableJpaRepositories(basePackages = {"hu.diskwars"})
public class BaseServiceConfig {
@Bean
DiskService diskService() {
return new DiskServiceImpl();
}
}
PersistenceJPAConfig.java
@Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = {"hu.diskwars"}) public class PersistenceJPAConfig implements DisposableBean { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan(new String[]{"hu.diskwars"}); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalProperties()); return em; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/whdiskwars_db"); dataSource.setUsername("Westy"); dataSource.setPassword("pass"); return dataSource; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } Properties additionalProperties() { return new Properties() { { // Hibernate Specific: setProperty("hibernate.hbm2ddl.auto", "create-drop"); setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); } }; } @Override public void destroy() throws Exception { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
BaseServiceConfig.java (autowiring the implementation)
@Configuration @EnableJpaRepositories(basePackages = {"hu.diskwars"}) public class BaseServiceConfig { @Bean DiskService diskService() { return new DiskServiceImpl(); } }
web.xml
> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"
> xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
>
> <display-name>diskwars</display-name>
> <context-param>
> <param-name>contextClass</param-name>
> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
> </context-param>
>
> <context-param>
> <param-name>contextConfigLocation</param-name>
> <param-value>hu.diskwars.config</param-value> </context-param>
>
> <listener>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
> </listener>
> <filter> <filter-name>wicket.diskwars</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
> <init-param> <param-name>applicationClassName</param-name>
> <param-value>hu.diskwars.view.application.WicketApplication</param-value>
> </init-param> </filter>
>
> <filter-mapping> <filter-name>wicket.diskwars</filter-name>
> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hu.dwdb</groupId> <artifactId>diskwars</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>diskwars</name> <description></description> <properties> <wicket.version>6.13.0</wicket.version> <jetty.version>7.6.13.v20130916</jetty.version> <wtp.version>none</wtp.version> <slf4j.version>1.6.1</slf4j.version> <spring.version>3.2.2.RELEASE</spring.version> <jpa.version>1.2.0.RELEASE</jpa.version> <hibernate.version>4.2.0.Final</hibernate.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${jpa.version}</version> <type>jar</type> </dependency> <!-- WICKET DEPENDENCIES --> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-core</artifactId> <version>${wicket.version}</version> </dependency> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-extensions</artifactId> <version>${wicket.version}</version> </dependency> <!-- LOGGING DEPENDENCIES - LOG4J --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!-- JUNIT DEPENDENCY FOR TESTING --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- JETTY DEPENDENCIES FOR TESTING --> <dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all-server</artifactId> <version>${jetty.version}</version> <scope>provided</scope> </dependency> <!-- HIBERNATE DEPENDENCIES --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MYSQL DRIVER --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> </dependencies> <build> <resources> <resource> <filtering>false</filtering> <directory>src/main/resources</directory> </resource> <resource> <filtering>false</filtering> <directory>src/main/java</directory> <includes> <include>**</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <testResources> <testResource> <filtering>false</filtering> <directory>src/test/resources</directory> </testResource> <testResource> <filtering>false</filtering> <directory>src/test/java</directory> <includes> <include>**</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jetty.version}</version> <configuration> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8080</port> <maxIdleTime>3600000</maxIdleTime> </connector> <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector"> <port>8443</port> <maxIdleTime>3600000</maxIdleTime> <keystore>${project.build.directory}/test-classes/keystore</keystore> <password>wicket</password> <keyPassword>wicket</keyPassword> </connector> </connectors> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <wtpversion>${wtp.version}</wtpversion> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>Apache Nexus</id> <url>https://repository.apache.org/content/repositories/snapshots/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>
Here is a picture of my workspace:
Thanks for the help! https://i.stack.imgur.com/P32oV.png
EDIT:
Added SpringInjector to my base wicket application
@Override public void init() { super.getComponentInstantiationListeners().add(new SpringComponentInjector(this)); }
And suddenly a completly new, error pops out, in conncetion with my persistance unit.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [hu/diskwars/config/PersistenceJPAConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'diskwarsPU': file:/D:/Development/projects/diskwars/target/diskwars-1.0-SNAPSHOT/WEB-INF/classes/, file:/D:/Development/projects/diskwars/target/diskwars-1.0-SNAPSHOT/WEB-INF/classes/
The main problem is, i dont even have a persistance.xml file. I used to (with the same exact name 'diskwarsPU', but i throw it out, when i got to know, that i can do all the configurations without it. I cleaned, builded the project again, even deleted the target folder, no use.