0

その時点でテストを実行しているときは @Autowired が機能していますが、Web アプリを実行してその時点でデータを取得しようとすると、null ポインター例外がスローされます。

これは私のコントローラーです

この BuyerRepo では常に null

import com.retail.exception.InvalidIdException;
import com.retail.model.Buyer;
import com.retail.repository.BuyerRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/buyer")
@Component
public class BuyerController {

    @Autowired
    private BuyerRepo buyerRepo;

    @GET
    @Produces("application/json")
    public Buyer searchFields() throws InvalidIdException {
        String buyerId = "51";
        Buyer buyer;

        try {
            buyer = buyerRepo.getBuyer(Long.parseLong(buyerId));
        } catch (NumberFormatException e) {
            buyer = buyerRepo.getBuyer(buyerId);
        }

        return buyer;
    }
}

リポジトリ内のエンティティ マネージャーは常に null です

これはバイヤーリポジトリです

import com.retail.exception.InvalidIdException;
import com.retail.model.Buyer;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;

@Repository
public class BuyerRepo extends AbstractRepository {

    public Buyer getBuyer(String buyerName) throws InvalidIdException {
        javax.persistence.Query buyerId = entityManager.createNativeQuery("select b.buyer_id from buyer b where b.name = :name").setParameter("name", buyerName);
        Integer id;

        try {
            id = (Integer) buyerId.getSingleResult();

        } catch (NoResultException e) {
            return null;
        }

        return getBuyer(id);
    }

    public Buyer getBuyer(long buyerId) throws InvalidIdException {
        Buyer buyer = entityManager.find(Buyer.class, buyerId);
        if (buyer == null) throw new InvalidIdException("Invalid Article ID");
        return buyer;
    }
}

これはapplicationContext.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"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.retail"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost/retail"/>
        <property name="username" value="retail_user"/>
        <property name="password" value="password"/>
    </bean>

    <bean id="entityManagerOne" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>

        <property name="packagesToScan" value="com.retail"/>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
            </bean>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerOne"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    </beans>

これは servlet-context.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <mvc:annotation-driven />
    <context:annotation-config />

    <context:component-scan base-package="com.retail" />

</beans>

これは web.xml です

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

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>retail</servlet-name>

        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.retail.web</param-value>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>

        <servlet-name>retail</servlet-name>

        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
            /WEB-INF/servlet-context.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>/WEB-INF/views/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>
4

4 に答える 4

2

クラスの代わりにインターフェイスを配線する必要があります。したがって、次の 2 つの方法があります。

  • BuyerRepo に 1 つのインターフェースを実装させるには

  • @Autowired の代わりに @Inject または @Resource を使用する

于 2013-05-19T12:33:51.373 に答える
0

あなたのbuyerRepoにはパブリックセッターがないようです。また、コンストラクターを介して設定することもできません。そのためのセッターを作成し、代わりに @Autowired アノテーションをセッターに付けてみませんか。このような:

@Repository
public class BuyerRepo extends AbstractRepository {

        private BuyerRepo buyerRepo;

        @Autowired
        public void setBuyerRepo(BuyerRepo buyerRepo)
        {
          this.buyerRepo = buyerRepo;
        }
        //...Other code is omitted.
}
于 2013-05-19T12:46:12.840 に答える