@Autowired 不适用于球衣和弹簧 [英] @Autowired is not working with jersey and spring

查看:30
本文介绍了@Autowired 不适用于球衣和弹簧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当时我正在运行测试时@Autowired 正在工作,但是当我运行网络应用程序并尝试在当时获取数据时,它会抛出空指针异常.

这是我的控制器

在这个BuyerRepo中始终为空

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;
    }
}

在存储库中实体管理器始终为空

这是buyerRepository

this is buyerRepository

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

this is 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

this is 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>

推荐答案

我也遇到过这种情况,你需要添加一些jar文件

I Have encountered this situation, you need to add some jar file

gradle 项目:

编译组:'org.glassfish.jersey.ext',名称:'jersey-spring3',版本:'2.22.2'

Maven 项目:

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.22.2</version>
</dependency>

另一个解决方案是 web.xml 文件:

another solution is web.xml file:

 <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>cn.ice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

这篇关于@Autowired 不适用于球衣和弹簧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆