wicket,spring jpa存储库 - @Autowire上的nullpointer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wicket,spring jpa存储库 - @Autowire上的nullpointer相关的知识,希望对你有一定的参考价值。

我试图建立一个基于wicket,Spring jpa存储库和mysql的应用程序。

问题是,我的服务类只是不想自动装配。如果我想使用我的autowired类,我得到了一个nullpointer异常。

实际上,我得到了双nullpointer:我的BaseServiceConfig.java不会自动执行实现,并且itnerface中的自动装配的存储库也是null。所以,问题是为什么。

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做这个,所有的配置都是在java类中完成的。我来告诉你代码:

存储库界面:

 @Transactional
 public interface DiskRepository extends JpaRepository<Disk, Long> {
 }

服务界面:

public interface DiskService {

public Disk save(Disk entity);

}

服务实施:

@Service 
public class DiskServiceImpl implements DiskService {

@Autowired
DiskRepository diskRepo;

@Override
public Disk save(Disk entity) {
    return diskRepo.save(entity);
}

}

base service config.Java

@Configuration
@EnableJpaRepositories(basePackages = {"hu.diskwars"})
public class BaseServiceConfig {

    @Bean
    DiskService diskService() {
        return new DiskServiceImpl();
    }

}

persistence JP A config.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();
}

}

veb.hml

> <?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.hml:

<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

EDIT:

  • 将SpringInjector添加到我的基础wicket应用程序中 @Override public void init(){super.getComponentInstantiationListeners()。add(new SpringComponentInjector(this)); }
  • 突然间,一个全新的错误突然出现,与我的坚持单位有关。

引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[hu / diskwars / config / PersistenceJPAConfig.class]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是java.lang.IllegalStateException:名称'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/

主要问题是,我甚至没有persistance.xml文件。我曾经(使用相同的名称'diskwarsPU',但我把它丢掉了,当我知道,我可以在没有它的情况下完成所有配置。我清理,再次构建项目,甚至删除了目标文件夹,没有使用。

答案

Wicket对Spring @Autowired注释一无所知。使用Wicket @SpringBean批注和SringComponentInjector将Sring bean注入Wicket组件。

有关如何使用@SpringBean注释的完整示例有问题How can I get a Spring bean injected in my custom Wicket model class?

以上是关于wicket,spring jpa存储库 - @Autowire上的nullpointer的主要内容,如果未能解决你的问题,请参考以下文章

存储库注释不适用于 Spring 数据 JPA

Spring Webflux + JPA:JPA 不支持反应式存储库

Spring Boot JPA 存储库查询

Spring -data-jpa ,存储库类不起作用

Spring JPA 存储库事务性

spring-data-jpa 存储库模式与 Querydsl 查询模式有啥区别?