我試圖建立一個基於wicket,Spring jpa倉庫和Mysql的應用程序。wicket,spring jpa repository - @Autowire上的空指針
問題是,我的服務類只是不想自動裝配。如果我想使用我的自動裝配類,我得到一個空指針異常。
事實上,我得到了雙重空指針:我的BaseServiceConfig.java沒有自動執行實現,並且itnerface中的自動裝配存儲庫也是空的。所以,問題是爲什麼。
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 } }
我試着用最少要做到這一點沒有XML,所有的coniguration在java類來完成。讓我告訴你的代碼:
庫接口:
@Transactional public interface DiskRepository extends JpaRepository<Disk, Long> { }
服務itnerface:
public interface DiskService { public Disk save(Disk entity); }
服務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(自動裝配的執行)
@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>
以下是我的工作區圖片:
感謝您的幫助! http://i.stack.imgur.com/P32oV.png
編輯:
新增SpringInjector到我的基地檢票應用
@覆蓋 公共無效的init(){ super.getComponentInstantiationListeners()增加(新SpringComponentInjector(本)) ; }
突然一個完全新的,錯誤彈出,與我的堅持單位conontion。
引起:org.springframework.beans.factory.BeanCreationException: 錯誤創建名爲 '的entityManagerFactory' 在類中定義 路徑資源豆[HU/diskwars /配置/ PersistenceJPAConfig.class]: 調用init方法失敗;嵌套異常是 java.lang.IllegalStateException:衝突持久性單元 名稱'diskwarsPU'的定義: file:/ D:/Development/projects/diskwars/target/diskwars-1.0-SNAPSHOT/WEB-INF/classes/, 文件:/ d:/Development/projects/diskwars/target/diskwars-1.0-SNAPSHOT/WEB-INF/classes/
的主要問題是,我不甚至有persistance.xml文件。我曾經(用同樣的名字'diskwarsPU',但是當我知道,我可以在沒有它的情況下完成所有的配置。 我清理完了,重新編譯了項目,甚至刪除了目標文件夾,沒有用的。
看起來你嘗試創建同一個bean兩次,我猜這是包掃描。 –