首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

cfx Spring 跟 Spring MVC一起用报错

2012-12-16 
cfx Spring 和 Spring MVC一起用报错问题描述:在做WebService的时候,用CFX技术和Spring进行整合,然后通过W

cfx Spring 和 Spring MVC一起用报错
问题描述:
     在做WebService的时候,用CFX技术和Spring进行整合,然后通过WebService的方法调用业务逻辑代码,发现用Spring的依赖注入没有成功(失效)。去调web.xml中有关WebService代码,程序运行成功。

web.xml文件如下


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beanRefServer.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--
默认的spring配置文件是在WEB-INF下的applicationContext.xml 
-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value><!-- 强制进行转码 -->
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 默认所对应的配置文件是WEB-INF下的{servlet-name}-servlet.xml,这里便是:spring3-servlet.xml -->
<servlet>
<servlet-name>spring3</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring3</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


Controller代码

@Controller
@RequestMapping("/user")
public class UserController {

private IUserService userServer;
private IDeptService deptServer;

public IDeptService getDeptService() {
return deptServer;
}

public void setDeptService(IDeptService deptServer) {
this.deptServer = deptServer;
}

public IUserService getUserService() {
return userServer;
}

public void setUserService(IUserService userServer) {
this.userServer = userServer;
}

@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(@RequestParam String name,@RequestParam String password,Model model,HttpServletRequest request) throws Exception{


TUser user1 = userServer.getUserByName(name);
if(user1 == null) {
model.addAttribute("message", "用户不存在");
return "login";
}else if(password == null || !password.equals(user1.getPassword()) ){
model.addAttribute("message", "密码错误");
return "login";
}else {
request.getSession().setAttribute(Constants.USER_INFO_SESSION, user1);
return "welcome";
}
}
以下略...



上面代码中 login()方法时,userServer为null,注入没有成功,请问为何


[最优解释]
关键的SPRING配置不给,怎么让别人排错?
据我经验 你那就没注入
[其他解释]
IUserService userServer这个实现类有没有问题?
spring配置文件里注册了它吗?
还是使用注解的?
[其他解释]
谢谢楼上各位的回贴,在web.xml文件中注释掉WebService相关配置,或者两者都单独运行都能成功

spring配置文件如下:


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" 
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">

        <import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:endpoint id="Loginn" implementor="hy.ea.service.Impl.LoginImpl"
address="/login">  
</jaxws:endpoint>
<!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 -->
<!-- 约定优于配置,约定优于配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="ro34dt1234"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
       <property name="mappingDirectoryLocations">
         <list><!-- 这里直接映射的pojo类所在的包,简单方便不用没次加一个pojo类都需要到这里来添加 -->


            <value>classpath:com/impf2010/model</value>
         </list>
       </property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>

<!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。-->
<context:component-scan base-package="com.impf2010">
<context:exclude-filter type="regex" expression="com.impf2010.web.*"/>
</context:component-scan>

<!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<aop:config>
<aop:advisor pointcut="execution(* com.impf2010.service.*Service.*(..))" advice-ref="txAdvice"/>
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
</beans>        



WebService代码:

@WebService(endpointInterface = "hy.ea.service.Login", serviceName = "Login")
public class LoginImpl implements Login {
 
public String login(TUser user) {
try {
if(null == user)
return "传入User为空";
TUser user1 = userServer.getUserByName(user.getName());
if(user1 != null)
return "成功";
else{
return "调用返回空";
}
} catch (Exception e) {
e.printStackTrace(); 
return e.getMessage();

}

private IUserService userServer;

/**
 * @return the userServer
 */
public IUserService getUserServer() {
return userServer;
}

/**
 * @param userServer the userServer to set
 */
public void setUserServer(IUserService userServer) {
this.userServer = userServer;
}
}


UserService代码如下:

@Service("userServer")
public class UserServiceImpl implements IUserService {

private IUserDao userDao;

public IUserDao getUserDao() {
return userDao;
}

public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}

public void addUser(TUser user) throws Exception {
userDao.save(user);
}

public void deleteUser(int id) throws Exception {
userDao.delete(getUserById(id));
}

public TUser getUserById(int id) throws Exception {


return userDao.findById(id);
}

public TUser getUserByName(String name) throws Exception {
List list = userDao.findByName(name);
return list==null 
[其他解释]
 list.size() == 0 ? null : (TUser)list.get(0);
}

public List<TUser> getUserList() throws Exception {
return userDao.findAll();
}

public void updateUser(TUser user) throws Exception {
userDao.merge(user);
}
}



UserDao代码如下:


+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#findByName(java.lang.Object)
 */
public List findByName(Object name) {
return findByProperty(NAME, name);
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#findByAge(java.lang.Object)
 */
public List findByAge(Object age) {
return findByProperty(AGE, age);
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#findByDeptId(java.lang.Object)
 */
public List findByDeptId(Object deptId) {
return findByProperty(DEPT_ID, deptId);
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#findAll()
 */
public List findAll() {
log.debug("finding all TUser instances");
try {
String queryString = "from TUser";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#merge(com.impf2010.model.TUser)
 */
public TUser merge(TUser detachedInstance) {
log.debug("merging TUser instance");
try {
TUser result = (TUser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#attachDirty(com.impf2010.model.TUser)
 */
public void attachDirty(TUser instance) {
log.debug("attaching dirty TUser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

/* (non-Javadoc)
 * @see com.impf2010.dao.IUserDao#attachClean(com.impf2010.model.TUser)
 */
public void attachClean(TUser instance) {
log.debug("attaching clean TUser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static IUserDao getFromApplicationContext(ApplicationContext ctx) {
return (IUserDao) ctx.getBean("TUserDAO");
}
}

热点排行