Spring Jdbc + Wildfly 没有@AutoWired服务层
Spring Jdbc + Wildfly does not @AutoWired service layer
大家晚上好,
在使用 spring Jdbctemplate + wildfly 10 进行的一些测试中,我遇到了一些非常奇怪的问题,当我通过 soapui 调用 soap web 服务时,我的服务层没有被注入,这使得 eclipse 向我显示了一个悲伤和蓝色的 NullPointerException。
异常:
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)
依赖关系:
<!-- 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:
<?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:
<?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>
dao层扩展的Class:
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;
}
}
道层实现:
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);
}
}
道层接口:
package com.gtac.usuario.dao;
public interface UsuarioDao {
public int numero();
}
已实施服务
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();
}
}
服务层接口
package com.gtac.usuario.service;
public interface UsuarioService {
public int numero();
}
网络服务层
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;
}
}
有件事我忘了说:
当我第一次启动 wildfly 10 时,它接收到数据源
它在 JdbcTemplate 中设置。
另一件事:如您在 JdbcConnection.java 中所见,我有一行代码在将数据源插入 JdbcTemplate 之后执行 select。效果不错。
我解决了这个问题。解决方案在手册中:
我应该在我的 web 服务层中扩展 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;
}
}
大家晚上好, 在使用 spring Jdbctemplate + wildfly 10 进行的一些测试中,我遇到了一些非常奇怪的问题,当我通过 soapui 调用 soap web 服务时,我的服务层没有被注入,这使得 eclipse 向我显示了一个悲伤和蓝色的 NullPointerException。
异常:
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)
依赖关系:
<!-- 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:
<?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:
<?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>
dao层扩展的Class:
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;
}
}
道层实现:
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);
}
}
道层接口:
package com.gtac.usuario.dao;
public interface UsuarioDao {
public int numero();
}
已实施服务
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();
}
}
服务层接口
package com.gtac.usuario.service;
public interface UsuarioService {
public int numero();
}
网络服务层
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;
}
}
有件事我忘了说: 当我第一次启动 wildfly 10 时,它接收到数据源 它在 JdbcTemplate 中设置。
另一件事:如您在 JdbcConnection.java 中所见,我有一行代码在将数据源插入 JdbcTemplate 之后执行 select。效果不错。
我解决了这个问题。解决方案在手册中:
我应该在我的 web 服务层中扩展 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;
}
}