首页 新闻 搜索 专区 学院

关于spring里<context:component-scan>标签的事

0
悬赏园豆:10 [已解决问题] 解决于 2017-08-11 18:58

是这样的,我新部署了一个springMVC工程,有两个配置文件,application.xml和springServlet-config.xml.<context:component-scan>在springServlet-config.xml里写的,程序成功运行,但是我看网上很多人的博客里的文章的<context:component-scan>标签都在application.xml文件里写的,所以我就将这句话移过去,发现报错了,报了很多Error creating bean with name 'springServlet',创建不了bean类这种错,请问各位大神,这个标签写的位置有什么其他需要注意的地方吗?

坐在家里晒太阳的主页 坐在家里晒太阳 | 初学一级 | 园豆:117
提问于:2017-08-11 16:57
< >
分享
最佳答案
0

叫什么名字无所谓,看你web.xml怎么配置了

收获园豆:10
让我发会呆 | 老鸟四级 |园豆:2642 | 2017-08-11 17:44

srpingServlet-config.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.2.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task.xsd ">

    <!-- 使用默认的注解映射 -->
    <mvc:annotation-driven />
    <!-- <mvc:resources location="/" mapping="/index.html" />
      <context:annotation-config></context:annotation-config>-->
    <!-- world bean类 -->
    <bean id="world" class="com.entity.World" />
        <!-- <property name="user" ref="user" />
        <property name="type" value="stringWorld"></property>
    </bean> -->
    <bean id="user" class="com.entity.User"/>
    <!-- user bean类 -->
    <!-- <bean id="user" class="com.entity.User">
        <property name="id" value="1"></property>
        <property name="name" value="guan"></property>
        <property name="pwd" value="111111"></property>
        <property name="age" value="12"></property>
    </bean> -->
    <!-- <bean id="world" class="com.entity.World">
        <property name="user">
            <idref bean="user"/>
        </property>
        <property name="type" value="stringWorld"></property>
    </bean> -->
    <!-- 自动扫描controller包中的控制器 -->
    <context:component-scan base-package="com.controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/index.do">controllerDoem</prop>
            </props>
        </property>
    </bean>
    
     <bean id="controllerDoem" class="com.controller">
        <property name="view">
            <value>index</value>
        </property>
    </bean>-->

</beans>

application.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:p="http://www.springframework.org/schema/p"
    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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
       
    <!-- <context:component-scan base-package="com.entity" /> -->
<!-- 
    <bean id="configProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath*:META-INF/spring/*.properties</value>
            </list>
        </property>
    </bean>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties"></property>
    </bean>

    当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 
        Bean,当发现 Bean 中拥有@Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
    <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    Jpa 事务配置
    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    jpaVendorAdapter:指定实现JPA的适配器
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="database" value="${database}" />
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLMyISAMDialect" />
    </bean>
    JPA实体管理工厂的配置
    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="1800000" />
        <property name="numTestsPerEvictionRun" value="3" />
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <property name="defaultAutoCommit" value="false" />
    </bean>

    Spring Data Jpa配置
    <jpa:repositories base-package="com.zjn.repository"
        entity-manager-factory-ref="entityManagerFactory"></jpa:repositories> -->
</beans>

请问,我把<context:component-scan base-package="com.controller" />写在application里运行就报错,,求大神解答

坐在家里晒太阳 | 园豆:117 (初学一级) | 2017-08-11 18:09

@坐在家里晒太阳: controller的扫描,放在web.xml中servlet标签里用到的xml里

让我发会呆 | 园豆:2642 (老鸟四级) | 2017-08-11 18:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册