我有一个带有两个webservices的java项目,第一个工作正常,spring bean是@Autowired并按预期工作。我试图在泽西服务中使用相同的bean,并得到一个来自DAO的空指针异常。代码如下,请帮忙!
Web.xml中
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/sprservice/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jersey-services</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mypackage</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-services</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
应用context.xml中
<context:annotation-config />
<context:component-scan base-package="com.mypackage"/>
<mvc:annotation-driven/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mywebservice" class="com.mypackage.webservice.Webservice">
</bean>
<beans profile="default">
<jdbc:embedded-database id="dataSource"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
</beans>
<beans profile="prod">
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="****"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="*****"/>
<property name="username" value="******"/>
<property name="password" value="******"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- change this to 'verify' before running as a production app -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
</beans>
服务-beans.xml文件
<beans>
<import resource="classpath:applicationContext.xml"/>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxrs:server id="myContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="mywebservice"/>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
WebService.java
Path("ws")
public class WebService {
private static final Logger logger = LogManager.getLogger("WebService");
@Resource
private LocationDAO locationdao;
@GET
@Path("getLocations")
@Produces({MediaType.APPLICATION_JSON})
public List<Location> getLocation(){
logger.info("VA-- GETTING LOCATION");
if(locationdao == null){
logger.error("VA-- DAO IS NULL");
}
return locationdao.listLocations();
}
LocationDAO.java
@Service
public class LocationDAOImpl implements LocationDAO{
private static final Logger logger = LogManager.getLogger("LocationDAOImpl");
@PersistenceContext
EntityManager em;
@Transactional
public void addLocation(Location location) {
//Do stuff....
}
答案 0 :(得分:0)
固定。解决方案是:
&LT; bean id =“mywebservice”class =“com.mypackage.webservice.Webservice”/&gt;
这一行导致空指针错误,因为我正在将XML与注释混合在一起。我得到了解决方案here的想法。
这改善了我的应用程序的整体流程,并将逻辑分成了清晰的层。