首页 > spring没有加载到@Component注解的类

spring没有加载到@Component注解的类


其他的Controller和Service都能注入到,但就是这个InitData注入不到
项目的目录结构如上图所示
InitData的内容如下:

@Component
public class InitData implements ApplicationListener {
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        System.out.println("ddddd");
    }
}

business-config.xml文件内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
    <!--
        Repository and Service layers
    -->
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- ========================= RESOURCE DEFINITIONS ========================= -->

        <!-- import the dataSource definition -->
        <import resource="datasource-config.xml"/>
        <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
            <property name="maxUploadSize" value="2000000000"/>
        </bean>

        <context:component-scan base-package="com.cheerland.wechat.service,com.cheerland.wechat.scheduletask"/>
        <!-- enable autowire -->
        <context:annotation-config />
        <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
        <!-- (in this case, JDBC-related settings for the JPA EntityManager definition below) -->
        <context:property-placeholder location="classpath:spring/data-access.properties"/>

         <!-- enables scanning for @Transactional annotations -->
        <tx:annotation-driven />

        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>

        <!-- define the SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage" value="com.cheerland.wechat.domain" />
        </bean>

        <!-- scan for mappers and let them be autowired -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cheerland.wechat.dao" />
        </bean>
        <bean class="com.cheerland.wechat.util.SpringUtils"/>

        <import resource="spring-config-cache.xml"/>
        <import resource="spring-config-shiro.xml"/>
        <import resource="mq_core_config.xml"/>

        <import resource="business_task.xml"/>

        <bean id="wxMpConfigStorage" class="me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage"
              p:aesKey="${aesKey}"
              p:token="${token}"
              p:appId="${appId}"
              p:secret="${secret}"
              p:oauth2redirectUri="${oauth2redirectUri}"
                />
        <bean  id="wxMpService" class="me.chanjar.weixin.mp.api.WxMpServiceImpl" p:wxMpConfigStorage-ref="wxMpConfigStorage"/>
        <bean id="wxMpMessageRouter" class="me.chanjar.weixin.mp.api.WxMpMessageRouter">
            <constructor-arg ref="wxMpService"></constructor-arg>
        </bean>
        <bean class="com.cheerland.wechat.web.handler.WeChatInit" init-method="initWeChat"></bean>
    </beans>

mvc-core-config.xml的内容如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<!--
    - DispatcherServlet application context for PetClinic's web tier.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <import resource="mvc-view-config.xml"/>
    <import resource="spring-mvc-shiro.xml"/>

    <!--
        - POJOs labeled with the @Controller and @Service annotations are auto-detected.
    -->
    <context:component-scan
            base-package="com.cheerland.wechat.web"/>

    <context:component-scan base-package="com.cheerland.wechat.web" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <!-- 拦截器配置 -->
    <mvc:interceptors>

        <!-- 配置Token拦截器,防止用户重复提交数据 -->
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.cheerland.wechat.web.bind.method.TokenInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>


    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="com.cheerland.wechat.web.bind.method.CurrentUserMethodArgumentResolver"/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <!-- 控制器异常处理 -->
    <bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
    </bean>

    <bean class="com.cheerland.wechat.web.exception.DefaultExceptionHandler"/>
    <bean id="userInfoUtil" class="com.cheerland.wechat.util.UserInfoUtil"/>


    <!--  all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files
        (see header.jsp for more details) -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <mvc:resources mapping="/assets/**" location="/assets/"/>
    <!--<mvc:resources mapping="/upload/**" location="/upload/"/>-->
    <mvc:resources mapping="/ueditor/**" location="/ueditor/"/>


    <bean class="com.cheerland.wechat.util.SpringUtils"/>

</beans>

应该是加载到了的,你是想问为什么在spring启动的时候没有显示你的打印语句吗?貌似方法少了Override。

@Component
public class InitData implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("dddd");
    }
}

 <context:component-scan base-package="com.cheerland.wechat.service,com.cheerland.wechat.scheduletask"/>

这个路径没扫描到嘛

【热门文章】
【热门文章】