java - spring Autowiring 无法命中我的 DAO 类方法

标签 java spring spring-mvc

无法访问我的 DAO 类。下面是我的应用程序文件

    @Controller
    public class HelloWorldController1 {
        @Autowired
        EmpServiceIntf empService;

        public EmpServiceIntf getEmpService() {
            return empService;
        }

         public myMethod(){
            List<Employee> empList = new ArrayList<Employee>();
            empList = empService.getAllemp(abc,pqr);
          }


    --------------------my interface class-----------------

    public interface EmpServiceIntf {
    List<Employee> getAllemp(String abc, String pqr);
    }

    -------------my implementation class----------------

    public class EmpServiceImpl implements EmpServiceIntf {
    private EmpServiceDAO empDAO = new EmpServiceDAO ();
        public List<Employee> getAllemp(String abc, String pqr) {
            return empDAO.getAllemp(abc, pqr);
        }
    }

------------------------applicationContext.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:mvc="http://www.springframework.org/schema/mvc"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Activates various annotations to be detected in bean classes-->
<context:annotation-config />
<context:component-scan base-package="org.nea.rest.unsr" />
<context:component-scan base-package="org.nea.spring.services.implementations.Uniserv" />
<context:component-scan base-package="org.nea.spring.services.interfaces.uniservs" />


<bean id="uniservService"  class="org.nea.spring.services.implementations.Uniserv.UniservServiceJPAImpl" />


    <!--bean class="org.nea.rest.unsr.HelloWorldController1">
        <property name="uniservService" ref="uniservService"></property>
    </bean-->

<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />

</beans>
----------------------------------

现在我的问题是;使用上述配置,当我点击 myMethod() 时,它会转到我的 EmpServiceImpl 类方法并返回到我的 HelloWorldController1 类。但它没有点击我的 EmpServiceDAO 方法。任何人都可以帮助我在哪里添加 n 以便它点击我的 EmpServiceDAO getAllemp 方法

最佳答案

这只是从“Controller”到“DaoImpl”的骨架示例。

@Controller - 对于您的 Controller @Service - 为您服务 @Repository - 为了你的道

@Controller
class YourController {
   @Autowire
   private YourService yourService;

   public String yourControllerMethod() {
       yourService.serviceMethod();
   }
}

interface YourService {
   void serviceMethod();
}

@Service
class YourServiceImpl implements YourService {
   @Autowire
   private YourDao yourDao;

   void serviceMethod(){
      yourDao.daoMethod();
   }
}

interface YourDao {
    void daoMethod();
}

@Repository
class YourDaoImpl implements YourDao {
    void daoMethod() {
    }
}

在你的“spring-mvc.xml”中 <mvc:annotation-driven />

and 

<context:component-scan base-package="com.yourcompany.example" />

to drive @Controller, @Service and @Repository annotations.

关于java - spring Autowiring 无法命中我的 DAO 类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004308/

相关文章:

java - 从起点开始的深度优先算法

java - 应用程序在平板电脑上运行,但在智能手机上崩溃

java - Spring JPA @事务原子

java - SpringMVC、c3p0、hibernate、JPA 应用程序泄漏连接导致 Too Many Connections 错误

java - HTTP 状态 500 - 处理程序处理失败;嵌套异常是 java.lang.UnsatisfiedLinkError : The specified module could not be found

java - 无法在 App Engine 中将静态文件转换为资源文件以使 http 请求通过身份验证过滤器

java - 为什么 Java-8 lambda 需要 invokeDynamic byteCode 来调用接口(interface)方法

java - Spring 应用程序上下文 XML 的命名约定

java - 使用 spring 配置 Apache Ignite mongo

spring-mvc - Tomcat 8 警告无法释放后台缓存逐出进程的缓存