Spring环境搭建之:通过PropertyPlaceholderConfigurer加载属性配置文件:

Spring环境搭建之:通过PropertyPlaceholderConfigurer加载属性配置文件:


01<?xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04       xmlns:p="http://www.springframework.org/schema/p"
05       xsi:schemaLocation="
06        http://www.springframework.org/schema/beans
07        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
08<!-- 使用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息.properties -->
09    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
10        <property name="locations">
11            <list>
12                <!--
13                    这里的classpath可以认为是项目中的src-属性名是 locations,
14                    使用子标签<list></list>可以指定多个数据库的配置文件,这里指定了一个
15                -->
16                <value>classpath:resource/config/jdbc.properties</value>
17            </list>
18        </property>
19    </bean>
20</beans>

此时的数据库配置文件项目路径是这样的

用法2:

读取数据库的配置文件还可以使用下面的方式

01<strong><?xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04       xsi:schemaLocation="
05        http://www.springframework.org/schema/beans
06        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
07 
08<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
09    <property name="locations">
10        <list>
11            <value>/WEB-INF/config_test/jdbc.properties</value>
12        </list>
13    </property>
14</bean></strong>

此时jdbc.properties文件的位置如下图所示

.properties配置文件还可以有多个,这里在<list></list>标签中指定了2个数据的配置文件

01<?xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04       xsi:schemaLocation="
05        http://www.springframework.org/schema/beans
06        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
07<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
08    <property name="locations">
09        <list>
10            <value>classpath:jdbc.properties</value>
11            <value>/WEB-INF/config_test/jdbc.properties</value>
12        </list>
13    </property>
14</bean>

classpath:jdbc.properties对应的文件位置是:

文件内容是:配置的是sqlserver的连接信息

1sqlserver.username=sa
2sqlserver.password=sqlserver
3sqlserver.url=jdbc\:jtds\:sqlserver\://localhost\:1433/J2EE
4sqlserver.driver=net.sourceforge.jtds.jdbc.Driver

 /WEB-INF/config_test/jdbc.properties对应的文件位置是

文件内容是:配置的是oracle的连接信息

1jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
2jdbc.url=jdbc:oracle:thin:@127.0.0.1 :1521:orcl
3jdbc.username=jxbms
4jdbc.password=jxbms

 这样数据库的配置信息被读取之后,在创建datasource的时候就可以使用了

 下面连接oracle 使用apache的dbcp 数据源

01<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
02    <property name="driverClassName">
03        <value>${jdbc.driverClassName}</value>
04    </property>
05    <property name="url">
06        <value>${jdbc.url}</value>
07    </property>
08    <property name="username">
09        <value>${jdbc.username}</value>
10    </property>
11    <property name="password">
12        <value>${jdbc.password}</value>
13    </property>
14    <property name="maxActive">
15        <value>100</value>
16    </property>
17    <property name="maxIdle">
18        <value>3</value>
19    </property>
20    <property name="maxWait">
21        <value>-1</value>
22    </property>
23    <property name="defaultAutoCommit">
24        <value>false</value>
25    </property>
26</bean>

下面连接sqlserver数据库使用的是c3p0数据源

1<bean id="dataSource_oracle" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
2    <property name="driverClass" value="${jdbc.driverClassName}" />
3    <property name="jdbcUrl" value="${jdbc.url}" />
4    <property name="user" value="${jdbc.username}" />
5    <property name="password"  value="${jdbc.password}" />
6</bean>

 使用dbcp数据源令人郁闷的事,使用dbcp和spring提供的JdbcTemplate操作数据库是查询是可以的

但是执行update、delete、insert into 操作时,数据库中的数据没有变化

从网上查询了很多的资料,都无果。最后偶然看到网上有人说,dbcp数据源的事务不会自动提交,

当改成c3p0数据源后好了

随后认为这下终于可以松口气了,谁知道天不遂人愿。当更换一张表进行测试,数据库中的数据还是没有变化,难道c3p0数据源也不好使,

当再次经过代码的折磨之后,

最终发现改动测试java文件,不在一个项目中,把其他的项目关闭就好了
当文档写到这里时,突然发现oracle使用的dbcp数据源有这一项配置

1<property name="defaultAutoCommit">
2    <value>false</value>
3</property>

原来dbcp数据源事务的自动提交功能被关闭了

马上把事务自动提交改成true  进行测试,一切ok,(^ _ ^)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过继承PropertyPlaceholderConfigurer类来获取配置文件中的配置项。 首先,创建一个类,继承自PropertyPlaceholderConfigurer,重写processProperties方法,该方法在Spring应用程序上下文启动时会被调用,可以在该方法中获取配置文件中的属性值,例如: ```java public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private Properties properties; public MyPropertyPlaceholderConfigurer() { this.properties = new Properties(); } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); this.properties.putAll(props); } public String getProperty(String key) { return this.properties.getProperty(key); } } ``` 在该类中,我们重写了processProperties方法,并在该方法中将配置文件中的属性值保存到一个Properties对象中。然后,提供了一个getProperty方法,可以根据属性名获取属性值。 接下来,在Spring Boot启动类中,注入该类的实例,并通过该实例获取配置文件中的属性值,例如: ```java @SpringBootApplication public class MyApp { @Autowired private MyPropertyPlaceholderConfigurer propertyConfigurer; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @PostConstruct public void init() { String propertyValue = propertyConfigurer.getProperty("my.property"); System.out.println("my.property value is: " + propertyValue); } } ``` 在上述代码中,我们通过@Autowired注入了MyPropertyPlaceholderConfigurer实例,并在init方法中,获取了配置文件中的属性值,并打印出来。 在application.yml或application.properties中,可以定义my.property属性值,例如: ```yaml my.property: my custom property value ``` 这样,当应用启动时,就会打印出"my.property value is: my custom property value"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值