springmvc之context:property-placeholder

本文介绍了如何在Spring MVC框架中使用property-placeholder标签来加载.properties配置文件,并在Bean定义中通过${}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

context:property-placeholder标签是将.properties文件加载到web容器里,然后再bean中可以使用${}获取properties文件中的值,例如:properties文件中设置了template.suffix=.ftl,那么在springmvc.xml文件中

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

<property name="suffix" value="${template.suffix}"/>
<property name="contentType" value="text/html; charset=utf-8"></property>

</bean>

使用

context:properties-placeholder有ignore-resource-not-found和ignore-unresolvable属性,ignore-unresolvable表示忽略解析不到的属性,ignore-resource-not-found表示忽略找不到属性的文件,如果不忽略,则抛出异常

<?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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 缩小扫描范围到 com.itheima.SpringMVC --> <context:component-scan base-package="com.itheima.SpringMVC"/> <bean id="User" class="com.itheima.SpringMVC.pojo.User"> <property name="username" value="涂大钧"></property> <property name="password" value="123"></property> </bean> <!--数据源--> <context:property-placeholder location="classpath*:jdbc.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置sql--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--别名扫包--> <property name="typeAliasesPackage" value="com.itheima.SpringMVC.pojo"></property> <!--指定XML文件位置--> <!--mybatis-config.xml--> <property name="configLocation" value="classpath:/mybatis-mvc-config.xml" /> </bean> <!--指定Mapper XML文件位置--> <!--扫描Mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.itheima.SpringMVC.Mapper"></property> </bean> <!-- 事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--开启注解驱动的事务管理--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引入外部配置文件 --> <properties resource="jdbc.properties"> <!-- 可以在这里添加默认属性值 --> </properties> <!-- 设置 MyBatis 核心配置 --> <settings> <!-- 开启驼峰命名自动映射,如将数据库字段 user_name 映射到 Java 属性 userName --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 开启二级缓存 --> <setting name="cacheEnabled" value="true"/> <!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 开启 aggressiveLazyLoading 会导致所有关联对象在被访问时立即加载 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 配置日志实现 --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 配置类型别名,简化映射 --> <typeAliases> <package name="com.itheima.SpringMVC.pojo"/> </typeAliases> <!-- 配置插件 --> <plugins> <!-- PageHelper 分页插件 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 可选配置 --> <property name="reasonable" value="true"/> <property name="supportMethodsArguments" value="true"/> <property name="params" value="count=countSql"/> </plugin> </plugins> <!-- 配置数据库环境 --> <environments default="development"> <environment id="development"> <!-- 使用 JDBC 事务管理器 --> <transactionManager type="JDBC"/> <!-- 配置数据源,使用 POOLED 连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 可选配置:连接池参数 --> <property name="poolMaximumActiveConnections" value="10"/> <property name="poolMaximumIdleConnections" value="5"/> </dataSource> </environment> </environments> <!-- 注册映射器 --> <mappers> <package name="com.itheima.SpringMVC.Mapper" /> <!-- <mapper resource="SpringMVC/Mapper/UsersMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/UserMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/IdCardMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/GoodsMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/GoodsTypeMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/OrdersMapper.xml"/>--> <!-- <mapper resource="SpringMVC/Mapper/OrderItemsMapper.xml"/>--> </mappers> </configuration>有问题吗
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值