spring03

学习了spring的数据源的使用以及spring的作用域引入外部属性文件

对应的bean的xml文件和properties文件如下

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring 使用外部属性文件 -->
<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"
    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-4.3.xsd">
    
    <!--
    <bean id="datasourse" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="root"></property>
    <property name="password" value="000000"></property>
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/javawebholiday?useSSL=false"></property>
    </bean>  -->
    <!-- 导入属性文件 -->
    
    <context:property-placeholder location="classpath:db2.properties"/>
    <bean id="datasourse" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>
    <property name="driverClass" value="${driverClass}"></property>
    <property name="jdbcUrl" value="${jdbcUrl}"></property>
    </bean>
    

</beans>
user=root
password=
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/javawebholiday?useSSL=false
<?xml version="1.0" encoding="UTF-8"?>
<!-- 对于bean的作用于的相关的知识点 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- bean 的作用于是通过scope来显示,配置作用于。 
    默认是singleton,容器初始化时初始化bean的实例,在整个容器的生命周期只创建这一个bean
    prototype原型的,容器创建时初始化时不创建bean的实例,二是在每次的请求。都会创建一个新的bean-->
    <bean id="car" class="lib3.Car" scope="prototype">
    <property name="price" value="90000"></property>
    <property name="brand" value="adiu"></property>
    </bean>
    

</beans>
原文地址:https://www.cnblogs.com/dazhi151/p/12598852.html