@Autowired在SpringBoot CXF应用程序中提供空指针异常

时间:2016-11-21 11:15:25

标签: soap spring-boot cxf autowired

我已从现有的WSDL文件创建了SpringBoot CXF soap服务。我现在可以从springboot应用程序的嵌入式tomcat上运行的服务访问WSDL。当我尝试从soap-ui向服务发送请求时,控件到达服务方法的实现,然后当它访问@Autowired服务层时,它会抛出所有NPE,因为服务对象为null(我有在调试时看到这个)。如果我通过注释@Autowired对象手动创建(new Service()),流可以到达服务实现,然后DAO对象失败,因为它无法@Autowire DAO Impl,进一步当我创建手动DAO Impl对象时在@PersistenceContext失败,因为实体管理器为空。

我已确保服务使用@Service,带有@Repository的DAO层注释,但问题仍然存在。我还添加了@ComponentScan并提供了所有的包名,但我仍然为所有@Autowired获取null。

以下代码:配置类

@SpringBootApplication
public class EmWebSvcBootApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
    SpringApplication.run(EmWebSvcBootApplication.class, args);
}

public static final String SERVICE_NAME_URL_PATH = "/em";
public static final String EM_ISSUER_SERVICE_NAME_URL_PATH = "/EntitlementIssuer";


@Bean(name=Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {      
    return new SpringBus();
}


@Bean
public ServletRegistrationBean cxfServlet() {
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), SERVICE_NAME_URL_PATH + "/*");
    // Add custom Title to CXF´s ServiceList
    Map<String, String> initParameters = servletRegistrationBean.getInitParameters();
    initParameters.put("service-list-title", "My Test service");
    return servletRegistrationBean;
}

@Bean
public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(springBus(), new EntitlementIssuerEndpointImpl());
    endpoint.setServiceName(entitlementIssuer_Service().getServiceName());
    endpoint.setWsdlLocation(entitlementIssuer_Service().getWSDLDocumentLocation().toString());
    endpoint.publish(EM_ISSUER_SERVICE_NAME_URL_PATH);
    return endpoint;
}

 @Bean EntitlementIssuer_Service entitlementIssuer_Service(){
     return new  EntitlementIssuer_Service();
 }

}

服务类:

@Service
public class EntitlementIssuerServiceImpl implements EntitlementIssuerService {

private static final Logger logger = LoggerFactory.getLogger(EntitlementIssuerServiceImpl.class);

@Autowired
private EntitlementIssuerDAO entitlementIssuerDAO;

@Transactional(readOnly=true)
public List<EntitlementIssuerResponseWrapper> getEntitlementIssuers(EntitlementIssuerRequestWrapper requestWrapper)
        throws EMSystemException, EMBusinessException {

try{
            daoResponse = entitlementIssuerDAO.findEntitlementIssuers(requestWrapper);
        }catch(Throwable t){
        logger.error("Error while getting entitlement issuers: " + t.getMessage());
        throw new EMSystemException("Error while getting entitlement issuers: " + t.getMessage());
    }

}

DAO层:

@Repository
public class EntitlementIssuerDaoImpl implements EntitlementIssuerDAO{

    @PersistenceContext
private EntityManager entityManager;

public EntityManager getEntityManager() {
    return entityManager;
}

@Override
public List<EntitlementIssuer> findEntitlementIssuers(EntitlementIssuerRequestWrapper request) {

    Session session = (Session) entityManager.getDelegate();
    Criteria criteria = session.createCriteria(EntitlementIssuer.class, "entitlementIssuer");

    setupCriteria(request,criteria);
    List<EntitlementIssuer> output = criteria.list();
    return output;

}
}

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <em.wsdl.version>2.2.0</em.wsdl.version>
    <cxf.version>3.1.7</cxf.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!-- Apache CXF -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

我已添加了我正在使用的所有配置,我们非常感谢任何帮助解决此问题。基本上所有@Autowired服务,数据层和实体管理器都没有加载(因为我在调试中看到它们为null)。

1 个答案:

答案 0 :(得分:0)

我已经能够根据来自 https://github.com/codecentric/cxf-spring-boot-starter

的信息解决问题
/**
 * Configuration of SOAP Web services
 */
@Configuration
public class EntitlementIssuerServiceSOAPConfig {

    @Autowired
    private Bus bus;

    @Bean
    public EntitlementIssuerService getEntitlementIssuerServiceWebServiceImpl() {
        return new EntitlementIssuerServiceWebServiceImpl();
    }

    @Bean (name = "EntitlementIssuerServiceWebService")
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, getEntitlementIssuerServiceWebServiceImpl());
        endpoint.publish("/EntitlementIssuerService");
        return endpoint;
    }

}