3

I'm using springmvc 4.3.2-RELEASE to build a simple structure. I wrote my configurations into a couple of spring-*.xml ,when I debug my application ,it shows the serviceimpl in my Controller has value while the daoimpl in serviceimpl doesnt.

RoleController

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleInfoService roleInfoService;

    @RequestMapping("index")
    public String Index(){


        return "/role/index";
    }

    @RequestMapping("getall")
    public @ResponseBody List<RoleInfo> getAll() throws SQLException {
        return roleInfoService.getAll();
    }
}

RoleInfoServiceImpl

public class RoleInfoServiceImpl implements IRoleInfoService {

    @Autowired
    RoleInfoDaoImpl roleInfoDao ;

    @Override
   public List<RoleInfo> getAll() throws SQLException {
        return roleInfoDao.getAll();
    }
}

RoleInfoDaoImpl

public class RoleInfoDaoImpl implements IRoleInfoDao {

    @Override
    public List<RoleInfo> getAll() throws SQLException {
        return null;

    }
}

web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>>
    </listener>


    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CharacterEncoding</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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

springmvc.xml

<mvc:default-servlet-handler></mvc:default-servlet-handler>
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="keyy.controller"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp" />
        <property name="prefix" value="/WEB-INF/jsp/"/>
    </bean>

spring-service.xml

<bean class="keyy.service.serviceimpl.RoleInfoServiceImpl"></bean>

spring-dao.xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="driverClass" value="${jdbc.driver}"/>
</bean>
<bean class="keyy.dao.datasourcefactory.datasourcefactoryimpl.DataSourceFactory">

</bean>

Sorry I forgot to put my RoleInfoDaoImpl in the configuration file, but it indeed exitst, here is the full copy of my spring-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="roleInfoDao" class="keyy.dao.daoimpl.RoleInfoDaoImpl"></bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
    </bean>
    <bean class="keyy.dao.datasourcefactory.datasourcefactoryimpl.DataSourceFactory">

    </bean>

</beans>

I cut the dataSourceFactory instance in my RoleInfoDalImpl Class to simplify the question here, the problems still there in both case.


The hierarchy of my project:(my reputation limits the number of images)

-|springmvc  
   -|resources  
       -|spring  
            * spring-dao.xml
            * spring-service.xml   
            * springmvc.xml
        * jdbc.properties
   -|src
       -|keyy
            -|controller
                * RoleController
                * HomeController
            -|dao
                * IRoleInfoDao
               -|daoimpl
                    * RoleInfoDaoImpl
            -|entity
                * RoleInfo
            -|service
                * IRoleInfoService
               -|serviceimpl
                    * RoleInfoServceImpl

RoleInfoServiceImpl has value RoleInfoDaoImpl is null

Well I still cannot recognize what causes my question, I tried rebuild my SpringDemo using JavaConfig and it works fine ----- which I didn't use @ComponentScan or @Service @Repository like using xml. Can anyone explain?Pleasae

Dulani Maheshi
  • 1,070
  • 1
  • 10
  • 30
KeYY
  • 31
  • 3
  • are you missing the bean definition for `RoleInfoDaoImpl` in spring-dao.xml ?!! – Sagar Nov 24 '16 at 03:34
  • something is wrong in configuration files, can you post them all and complete ? – Goro Nov 24 '16 at 03:41
  • where do you keep the spring-*.xml files ? and seems you have not defined RoleInfoDaoImpl bean in your spring-service.xml – kuhajeyan Nov 24 '16 at 05:32
  • You are using component scanning in the context for the `DispatcherServlet` which implies annotation based configuration. However you aren't doing that for the context loaded by the `ContextLoaderLIstener` and as such your annotations are ignored. Either enable component scanning in there as well or add `` to only enable annotation processing for that context. – M. Deinum Nov 24 '16 at 07:40
  • I just refactored the name of springmvc.xml to spring-mvc.xml which I think makes both contextLoaderListener and DispatcherServlet to include them. But in this case, the application works -----RoleInfoDaoImpl has value now. Could anybody know the reason? – KeYY Nov 25 '16 at 03:12
  • I dont think and contextLoaderListener conflict. I honestly saw somebodyelse' application combined these two running perfectly before – KeYY Nov 25 '16 at 03:21
  • in your component base package use keyy.*,User @Service annotation on your Interface implementer class and if Interface in implemented by your single class than Autowired interface not its implementer class – Darshit Nov 25 '16 at 04:42

3 Answers3

1

Service

   @Service 
   public class RoleInfoServiceImpl implements IRoleInfoService {

Dao

   @Repository
   public class RoleInfoDaoImpl implements IRoleInfoDao { 

Note: If you rely on Annotations, it's better to just use the annotations. Now your service is using xml config.

zawhtut
  • 8,335
  • 5
  • 52
  • 76
0

Add keyy.service to your <context:component-scan base-package="keyy.controller"/> or change the base package to just keyy since it will recurse. Doing so will allow Spring to pick up the annotated classes.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • But the question is, the RoleInfoServiceImpl was injected into Controller successfully while step into the ServiceImpl, the DaoImpl wasn't. I think the scan process is executed using the contextLoaderListener instead of because the declaration of my service and dao are in the configuration files, not directly in the source code using annotation. – KeYY Nov 24 '16 at 06:13
  • Not sure I understand. How would Spring know how to read your Autowire annotations in your service layer? What happens if you include this? – riddle_me_this Nov 24 '16 at 18:38
  • well I think notifies context to detect the instance makred as '@Controller @Servivce' ... to be generated and managed by the container. The contextLoaderListener, in my opinion, behaves as combing the definition of the beans in separate configuration files into the original applicationContext.xml that located at contextConfigLocation of DispatcherServlet. – KeYY Nov 25 '16 at 03:11
0

I think you should add <context:annotation-config> in spring-service.xml

Refer this link to understand whats-the-difference-between-mvcannotation-driven-and-contextannotation

Community
  • 1
  • 1
Sagar
  • 818
  • 1
  • 6
  • 13