简体   繁体   English

Spring Jdbc + Wildfly没有@AutoWired服务层

[英]Spring Jdbc + Wildfly does not @AutoWired service layer

Good evening guys, I facade some really strange problem during some tests with spring Jdbctemplate + wildfly 10, when I call a soap webservice through soapui my service layer is not injected what makes the eclipse show me a sad and blue NullPointerException. 晚上好,我在使用Spring Jdbctemplate + wildfly 10进行一些测试时遇到了一个非常奇怪的问题,当我通过soapui调用soap webservice时,我的服务层没有注入,这使日食使我感到悲伤和忧郁的NullPointerException。

The Exception: 例外:

Caused by: java.lang.NullPointerException
    at com.gtac.usuario.webservice.soap.UsuarioWebServiceImpl.numero(UsuarioWebServiceImpl.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.webservices.deployers.WSComponentInstanceAssociationInterceptor.processInvocation(WSComponentInstanceAssociationInterceptor.java:56)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:636)
    at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
    at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:198)
    at org.jboss.as.webservices.invocation.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:137)
    at org.jboss.wsf.stack.cxf.JBossWSInvoker.performInvocation(JBossWSInvoker.java:169)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)

Dependencies: 依赖关系:

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.0.Final</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.0.0</version>
        </dependency>

appContext.xml: appContext.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:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">


    <jee:jndi-lookup id="pgDataSource" jndi-name="java:/pgDataSource"
        resource-ref="true" expected-type="javax.sql.DataSource" lookup-on-startup="true" />

    <bean id="jdbcConnection" class="com.gtac.util.dao.JdbcConnection">
        <!-- <constructor-arg name="dataSource" ref="pgDataSource" /> -->
        <property name="dataSource" ref="pgDataSource" />
    </bean>

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

</beans>

web.xml: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="GTAC-Servico" version="3.0"
    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_3_0.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/appContext.xml</param-value>
    </context-param>

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

Class that is extended by the dao layer: 由dao层扩展的类:

package com.gtac.util.dao;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class JdbcConnection {

    /** serialVersionUID */
    private static final long serialVersionUID = 8879088860192903041L;

    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return this.jdbcTemplate;
    }

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        int i = this.jdbcTemplate.queryForObject("select valor from teste", Integer.class);
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}

Dao Layer implementation: 道层实现:

package com.gtac.usuario.dao;

import org.springframework.stereotype.Repository;

import com.gtac.util.dao.JdbcConnection;

@Repository
public class UsuarioDaoImpl extends JdbcConnection implements UsuarioDao{

    @Override
    public int numero() {
        return getJdbcTemplate().queryForObject("select * from teste", Integer.class);
    }



}

Dao's layer interface: 道的层接口:

package com.gtac.usuario.dao;

public interface UsuarioDao {

    public int numero();

}

Service Implemented 服务实施

package com.gtac.usuario.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gtac.usuario.dao.UsuarioDao;

@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class UsuarioServiceImpl implements UsuarioService {

    @Autowired
    private UsuarioDao usuarioDao;

    @Override
    public int numero() {
        return usuarioDao.numero();
    }

}

Service's layer interface 服务的层接口

package com.gtac.usuario.service;

public interface UsuarioService {
    public int numero();
}

WebService layer WebService层

package com.gtac.usuario.webservice.soap;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gtac.usuario.service.UsuarioService;

@WebService(serviceName = "UsuarioWebService")
@SOAPBinding(style = Style.RPC, use = Use.LITERAL)
@Service
public class UsuarioWebServiceImpl {

    @Autowired
    private UsuarioService usuarioService;

    @WebMethod
    public int numero() {
        return this.usuarioService.numero();
    }

    @WebMethod(exclude=true)
    public UsuarioService getUsuarioService() {
        return usuarioService;
    }

    @WebMethod(exclude=true)
    public void setUsuarioService(UsuarioService usuarioService) {
        this.usuarioService = usuarioService;
    }

}

There is something that I forgot to mention: When I start the wildfly 10 for the first time it receives the datasource and it is set in the JdbcTemplate. 我忘了提及一些事情:当我第一次启动wildfly 10时,它接收到数据源,并在JdbcTemplate中进行了设置。

Another thing: As you can see in the JdbcConnection.java, I have a line doing a select just after the dataSource is inserted into the JdbcTemplate. 另一件事:正如您在JdbcConnection.java中看到的那样,在将dataSource插入JdbcTemplate之后,我有一行进行选择。 That works fine. 很好

I solved this problem. 我解决了这个问题。 The solution was in the manual: 解决方案在手册中:

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#remoting-web-services http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#remoting-web-services

I simple should to extend SpringBeanAutowiringSupport in my webservice layer. 我应该在Webservice层中扩展SpringBeanAutowiringSupport。

package com.gtac.usuario.webservice.soap;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gtac.usuario.service.UsuarioService;

@WebService(serviceName = "UsuarioWebService")
@SOAPBinding(style = Style.RPC, use = Use.LITERAL)
@Service
public class UsuarioWebServiceImpl extends SpringBeanAutowiringSupport  {

    @Autowired
    private UsuarioService usuarioService;

    @WebMethod
    public int numero() {
        return this.usuarioService.numero();
    }

    @WebMethod(exclude=true)
    public UsuarioService getUsuarioService() {
        return usuarioService;
    }

    @WebMethod(exclude=true)
    public void setUsuarioService(UsuarioService usuarioService) {
        this.usuarioService = usuarioService;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM