@Autowired注释指定null

时间:2017-11-27 03:45:57

标签: java spring maven jboss

NBDropdownValues中的

applicationProperties变量即使在使用@Autowired后也被指定为“null”

其他细节 - 此应用程序打包为战争并部署在Jboss EAP 6.4的顶部。由于Jboss带有restEasy,而我使用Jersey,我不得不在jboss-deployment-structure.xml中排除restEasy模块(注意模块javaee.api被排除在外,但包含了所有依赖项)。

部署期间没有错误,应用程序已正确部署。但是当我点击getValues()(http://127.0.0.1:8080/pva-ui-webapp/webapi/nb-dropdownValues)时,它会抛出空指针异常,因为applicationProperties变量为null。

的web.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <context:annotation-config/>

  <context:property-placeholder location="classpath:/application.properties"/>

  <bean id="applicationProperties" class="com.awpl.beans.ApplicationProperties" scope="prototype">
    <property name="envType" value="${envType}" />
    <property name="mongoClientURI" value="${mongoClientURI}" />
    <property name="language" value="${language}" />
    <property name="mongoDatabaseName" value="${mongoDatabaseName}" />
    <property name="nbDropdownValues" value="${nbDropdownValues}" />
  </bean>

  <bean id="nBDropdownValues" class="com.awpl.resources.NBDropdownValues">
  </bean>

</beans>

应用context.xml中

...

@Path("nb-dropdownValues")
public class NBDropdownValues {

  @Autowired
  private ApplicationProperties applicationProperties;

  public ApplicationProperties getApplicationProperties() {
    return applicationProperties;
  }

  public void setApplicationProperties(ApplicationProperties applicationProperties) {
    this.applicationProperties = applicationProperties;
  }

  Block<Document> printBlock = new Block<Document>() {
    @Override
    public void apply(final Document document) {
      System.out.println(document.toJson());
    }
  };

  JSON json =new JSON();

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public String getValues() {
    System.out.println("applicationProperties -- "+applicationProperties);
    System.out.println("applicationProperties.getMongoClientURI() -- "+applicationProperties.getMongoClientURI());
    MongoClientURI connectionString = new MongoClientURI(applicationProperties.getMongoClientURI());
    MongoClient mongoClient = new MongoClient(connectionString);
    try {
      MongoDatabase database = mongoClient.getDatabase(applicationProperties.getMongoDatabaseName());
      MongoCollection<Document> collection = database.getCollection(applicationProperties.getNbDropdownValues());

      String serialize = json.serialize(collection.find().sort(Sorts.ascending("order")));
      System.out.println(serialize);
      return serialize;

      //return collection.find().sort(Sorts.ascending("order")).toString();

    } catch (Exception e) {
    } finally {
      mongoClient.close();
    }
    return "Got it!";
  }
}

NBDropdownValues.java

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <exclude-subsystems>
      <subsystem name="jaxrs" />
    </exclude-subsystems>
    <exclusions>
      <module name="javaee.api" />
      <module name="javax.ws.rs.api" />
      <module name="org.jboss.resteasy.resteasy-jaxrs" />
    </exclusions>
    <dependencies>
        <module name="javax.activation.api" export="true"/>
        <module name="javax.annotation.api" export="true"/>
        <module name="javax.ejb.api" export="true"/>
        <module name="javax.el.api" export="true"/>
        <module name="javax.enterprise.api" export="true"/>
        <module name="javax.enterprise.deploy.api" export="true"/>
        <module name="javax.inject.api" export="true"/>
        <module name="javax.interceptor.api" export="true"/>
        <module name="javax.jms.api" export="true"/>
        <module name="javax.jws.api" export="true"/>
        <module name="javax.mail.api" export="true"/>
        <module name="javax.management.j2ee.api" export="true"/>
        <module name="javax.persistence.api" export="true"/>
        <module name="javax.resource.api" export="true"/>
        <module name="javax.rmi.api" export="true"/>
        <module name="javax.security.auth.message.api" export="true"/>
        <module name="javax.security.jacc.api" export="true"/>
        <module name="javax.servlet.api" export="true"/>
        <module name="javax.servlet.jsp.api" export="true"/>
        <module name="javax.transaction.api" export="true"/>
        <module name="javax.validation.api" export="true"/>
        <!-- <module name="javax.ws.rs.api" export="true"  services="export"/> -->
        <module name="javax.xml.bind.api" export="true"/>
        <module name="javax.xml.registry.api" export="true"/>
        <module name="javax.xml.soap.api" export="true"/>
        <module name="javax.xml.ws.api" export="true"/>
        <!-- This one always goes last. -->
        <module name="javax.api" export="true"/>
    </dependencies>
  </deployment>
</jboss-deployment-structure>

的JBoss部署-structure.xml

...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>2.26</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>

    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet-core</artifactId>
      <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
      <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>

    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
    </dependency>
    <!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-json-binding</artifactId> </dependency> -->

    <!-- spring support -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.1.9.RELEASE</version>
    </dependency>

    <!-- mongo drivers -->
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver</artifactId>
      <version>3.5.0</version>
    </dependency>
...

的pom.xml

expand

0 个答案:

没有答案