愀棤 2020-07-05 15:06 采纳率: 33.3%
浏览 176
已采纳

Javaee连接数据库报spring-db.xml上dataSource错误,不明原因,求解答

报错内容:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in file [C:\Users\愀棤\Desktop\nomal\JAVA_EE\spaceworking.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\fruit_email\WEB-INF\classes\spring-db.xml]: Could not resolve placeholder 'mysql.fruitname' in string value "${mysql.fruitname}"

spring-db.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd">

<context:annotation-config /> <!-- 使用注解方式 -->
<context:component-scan base-package="com.*" />


<!-- alibaba数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    <property name="url" value="${mysql.url}" />
    <property name="fruitname" value="${mysql.fruitname}" />
    <property name="password" value="${mysql.password}" />
    <property name="initialSize" value="3" />
    <property name="maxActive" value="30" />
    <property name="minIdle" value="6" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="true" />
    <property name="validationQuery" value="select 1" />
</bean>


<!-- hibernate session工厂1 -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" /><!-- 注入dataSource,数据源:如何链接数据库 -->
    <property name="packagesToScan" value="com.entity" /> <!-- 指定扫描entity包下的对像 ,使UserEntity1与该hibnate关联 -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 方言是哪种数据库,这里指定数据库为MySql -->
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">false</prop>
            <!--validate 加载hibernate时,验证创建数据库表结构 create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 
                create-drop 加载hibernate时创建,退出是删除表结构 update 加载hibernate自动更新数据库结构 以上4个属性对同一配置文件下所用有的映射表都起作用 
                总结: 1.请慎重使用此参数,没必要就不要随便用。 2.如果发现数据库表丢失,请检查hibernate.hbm2ddl.auto的配置值可以为true,false,update -->
        </props>
    </property>
</bean>

<!-- 定义事务这个事务管理器是hibernate的 -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"><!-- 指定Hibernet事务管理器 -->
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务,一般需要在Service,XXXDao层的类前加上@Transactional,不然会提示:No 
    Session found for current thread -->
<tx:annotation-driven transaction-manager="transactionManager"
    proxy-target-class="true" />

  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2020-07-05 15:35
    关注

    'dataSource' 定义时引用了一个 properties 占位符属性 ${mysql.fruitname} 无法找到对应的 定义文件。
    说明 properties 引用路径未指定,看代码也是少了这项配置,前面加上一个:

    <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:config/mysql.properties</value>
                </list>
            </property>
        </bean>
    

    看看你的配置文件是什么对应改正一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名