学海追寻 2016-08-31 05:53 采纳率: 0%
浏览 1540

SSH整合过程中:service老是注入失败?

action对象是用struts2产生的,但是service老是注入失败,其他的dao注入正常,在action中通过getbean的方式可以正常获得service
beans.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config />
    <!-- 分散配置(配置配置文件的位置) -->       
     <context:property-placeholder location= "classpath:jdbc.properties"/>
     <!-- 生成实例对象要扫描的包,扫描该包,将该包下的注解@Component的类实例化 -->
     <context:component-scan base-package="com.slg.surveypark.dao.impl,com.slg.surveypark.service.impl,com.slg.surveypark.struts2.action" />
    <!-- 配置数据源 --> 
    <bean id="dataSource" destroy-method="close"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass"
            value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <property name="maxPoolSize" value="${c3p0.pool.size.max}"/>
        <property name="minPoolSize" value="${c3p0.pool.size.min}"/>
        <property name="initialPoolSize" value="${c3p0.pool.size.ini}"/>
        <property name="acquireIncrement" value="${c3p0.pool.size.increment}"/>
    </bean>




     <!--本地会话工程(spring整合 hibernate配置) -->      
     <bean id="sessionFactory"     
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" />         
         <!-- 扫描一下包内的实体类,有@Entity注解与数据库形成映射 -->
         <property name="packagesToScan">
            <list>
                <value>com.slg.surveypark.model</value>                                 
            </list>
        </property>       
        <property name="hibernateProperties">
            <props>             
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--  hibernate.hbm2ddl.auto 当为update时根据java对象生成数据库里的表 -->
                 <prop key="hibernate.hbm2ddl.auto">update</prop>

            </props>
        </property>
    </bean>      

     <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <aop:config>
        <aop:pointcut id="bussinessService"
            expression="execution(* *..*Service.*(..))" />
        <aop:advisor pointcut-ref="bussinessService"
            advice-ref="txAdvice" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!-- 写操作 -->
            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>

             <!-- 读操作 -->
             <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
             <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
             <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>

             <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" />
        </tx:attributes>
    </tx:advice>      

</beans>

struts.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>  
    <!-- 主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- 开发模式 -->  
    <constant name="struts.devMode" value="true" />

    <package name="surveypark" namespace="/" extends="struts-default">         
         <action name="RegAction_*" class="com.slg.surveypark.struts2.action.RegAction" method="{1}">
         <result name="regPage">/reg.jsp</result>
         <result name="input">/reg.jsp</result>
         <result name="success">/index.jsp</result>
         </action>
    </package>  

</struts>

action类

 package com.slg.surveypark.struts2.action;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.slg.surveypark.model.User;
import com.slg.surveypark.service.UserService;
import com.slg.surveypark.service.impl.UserServiceImpl;
import com.slg.surveypark.util.ValidateUtil;


//@Component("regaction") 
//@Scope("prototype")
//@Controller
public class RegAction extends BaseAction<User> {

    /** 
     * 
     */
    private static final long serialVersionUID = 1L;
    private User model = new User();
    private String confirmPassword;
    public String getConfirmPassword() {
        return confirmPassword;
    }
    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }   

    protected UserService userService;

    public UserService getUserService() {
        return userService;
    }
    @Resource(name="userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return model;
    }

    /**
     * 
     * 这里添加了注解 @SkipValidation 所以会跳过默认的拦截器函数validate()
     */
    @SkipValidation 
    public String toRegPage(){
        return "regPage";
    }


    public String doReg(){
        userService.saveEntity(model);
        return SUCCESS;

    }
    /**
     * struts2的默认验证拦截器,所有的action执行前都会执行此函数,除非有跳过注解,如果验证过程有有问题,则
     * 终端action的执行过程,返回到struts2配置的“input”指向的页面
     */
    public void validate(){

        if(!ValidateUtil.isValid(model.getEmail())){
            addFieldError("email", "email是必填项!");

        }       

        if(!ValidateUtil.isValid(model.getNickName())){
            addFieldError("nickname", "nickname是必填项!");

        }
        if(!ValidateUtil.isValid(model.getPassword())){
            addFieldError("password", "password是必填项!");         

        }
        if(hasErrors()){
            return ;
        }
        if(!model.getPassword().equals(confirmPassword)){
            addFieldError("password", "密码不一致!");
            return ;
        }
        //通过getBean可以获得service
  //      ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
     //   userService = (UserService)ctx.getBean("userService");
        System.out.println(userService);
        if(userService.isRegisted(model.getEmail())){
            addFieldError("email", "email已占用!");
            return;
        }

    }
}

service类

 package com.slg.surveypark.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.slg.surveypark.dao.BaseDao;
import com.slg.surveypark.model.User;
import com.slg.surveypark.service.UserService;
import com.slg.surveypark.util.ValidateUtil;

@Component("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Resource(name="userDao")
    public void setDao(BaseDao<User> dao) {
        super.setDao(dao);
    }
    /**
     * 判断email是否被注册过
     */
    public boolean isRegisted(String email){
        String hql = "select email from User where email =?";
        List<User> users = this.findEntityByHQL(hql, email);
        return ValidateUtil.isValid(users);

    }

}

求指教

  • 写回答

2条回答 默认 最新

  • 糖果给你 2016-08-31 07:42
    关注

    在service的实现类上加一个@Service 注解试试
    @Service
    @Component("userService")
    public class UserServiceImpl extends BaseServiceImpl implements UserService {

    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题