0
我有一個帶有兩個webservices的java項目,第一項工作正常,spring bean是@Autowired並按預期工作。我嘗試在澤西服務中使用相同的bean,並得到一個來自DAO的空指針異常。代碼如下,請幫忙!如何在多servlet web服務中配置Spring bean
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....
}
關於http://stackoverflow.com/questions/6672701/jersey-and-spring-integration-bean-injections-are-null-at-runtime?那裏的答案建議使用SpringServlet,它理解Spring應用程序上下文配置指令。 – BobG
@BobG我試過了。當我切換到springservlet時,我得到一個404,當我點擊網址時,我認爲最好是路由到servlet,但得到一個空的bean ..(兩個邪惡較少)。仍然不知道如何解決... –
它不知道運動衫,但是這裏有一個完整的tuto,用於使用com.sun.jersey.spi.spring.container.servlet.SpringServlet的Jersey + Spring webapp:http://www.mkyong.com/webservices/jax -rs /球衣 - 彈簧 - 積分 - 例如/ – Julien