Java

Java Spring-bean的作用域

2020-01-10  本文已影响0人  一亩三分甜

使用bean的scope属性来配置bean的作用域

singleton:默认值。容器初始时创建bean实例,在整个容器的生命周期内只创建这一个bean,单例的。

    <bean id="car" class="com.cloud.spring.beans.autowire.Car" scope="singleton">
        <property name="brand" value="Audi"></property>
        <property name="price" value="300000"></property>
    </bean>

prototype:圆形的。容器初始化时不创建bean的实例。而在每次请求时都创建一个新的Bean实例,并返回。

    <bean id="car" class="com.cloud.spring.beans.autowire.Car" scope="prototype">
        <property name="brand" value="Audi"></property>
        <property name="price" value="300000"></property>
    </bean>

使用外部属性文件

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///firstDB?rewriteBatchedStatements=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"></property>
    </bean>
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-properties.xml");
        DataSource dataSource = (DataSource)context.getBean("dataSource");
        System.out.println(dataSource.getConnection());
//输出
com.mchange.v2.c3p0.impl.NewProxyConnection@4671e53b     

从外部properties文件中获取

    <!-- 导入属性文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <bean id="dataSource" 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="${url}"></property>
    </bean>
 
db.properties文件中   
user=root
password=123
url=jdbc:mysql://localhost:3306/firstDB?rewriteBatchedStatements=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8
driverClass=com.mysql.jdbc.Driver

发现报错信息java.sql.SQLException: Access denied for user 'fish'@'localhost' (using password: YES),提示user变成了电脑主机名称。通过断点发现通过c3p0访问数据库的user为fish。


Snip20200110_2.png
Snip20200110_5.png

将配置文件beans-properties.xml和db.properties文件中的属性换个除user以外的任何一个名字即可。如换成username。

    <!-- 导入属性文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 使用外部属性文件的的属性 -->
        <property name="user" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${url}"></property>
    </bean>
    
username=root
password=123
url=jdbc:mysql://localhost:3306/firstDB?rewriteBatchedStatements=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8
driverClass=com.mysql.jdbc.Driver
//输出
com.mchange.v2.c3p0.impl.NewProxyConnection@5204062d

因此,Mac电脑中使用c3p0线程池,不能使用关键字user作为c3p0 properties文件中的键名,如果windows电脑中,则不能使用关键字username作为c3p0 properties文件中的键名。否则会报相同的错误。

上一篇 下一篇

猜你喜欢

热点阅读