• 中文
    • English
  • 注册
  • 查看作者
    • Spring:配置连接池

      一.  配置DruidDataSource

      配置之前,我们先回顾一下在《Druid》一文中,我们是如何获取数据库连接的:

      public class JDBCDataSource {
          private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
          private static final String USERNAME = "book";
          private static final String PASSWORD = "zhangjia";
          private static DruidDataSource dataSource;
          //setDriverClassName可以不设置,当不设置驱动类名时,会根据url自动设置
          static {
              dataSource = new DruidDataSource();
              dataSource.setUrl(URL);
              dataSource.setUsername(USERNAME);
              dataSource.setPassword(PASSWORD);
          }
       
          public static DruidDataSource getDataSource() {
              return dataSource;
          }
      }

      有了Spring,我们便可以使用Spring配置DruidDataSource

      <?xml version="1.0" encoding="UTF-8"?>
      <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 id = "dataSource" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver" />
              <property name="url" value="jdbc:mysql://localhost:3306/demo2?useUnicode=true&amp;characterEncoding=utf8"/>
              <property name="username" value="root"/>
              <property name="password" value="zhangjia"/>
          </bean>
      
      </beans>

      二.  测试

      public class Test {
          public static void main(String[] args) throws SQLException {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              //这里之所以可以使用DataSource是因为DruidDataSource间接实现了DataSource接口
              DataSource dataSource = context.getBean(DataSource.class);
              System.out.println("dataSource = " + dataSource);
              Connection connection = dataSource.getConnection();
              System.out.println("connection = " + connection);
              connection.close();
              context.close();
          }
      }

      三.  使用properties方式配置

      在上面的操作中,我们的相关配置信息都是在bean中写死的,除了这种配置方式外,我们还可以采用之Mybatis的方式使用properties文件进行配置。首先创建jdbc.properties

      jdbc.driver=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/demo2?useUnicode=true&characterEncoding=utf8
      jdbc.username=root
      jdbc.password=zhangjia

      接下来首先在applicationContext.xml中读取properties文件,然后将bean的内容修改为以下内容即可:

      <?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:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
      
      <!--    读取properties文件-->
          <content:property-placeholder location="jdbc.properties" />
      
          <bean id = "dataSource" scope="prototype"  class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${jdbc.driver}" />
              <property name="url" value="${jdbc.url}"/>
              <property name="username" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>
      
      
      </beans>

      另外Spring的IOC容器中的bean,默认都是单例模式,可以通过下面的例子验证:

      public class Test {
          public static void main(String[] args) throws Exception {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              DruidDataSource bean1 = context.getBean(DruidDataSource.class);
              DruidDataSource bean2 = context.getBean(DruidDataSource.class);
              System.out.println(bean1.hashCode());
              System.out.println(bean2.hashCode());
              bean1.close();
              bean2.close();
              context.close();
          }
      }
      输出:
      2011986105
      2011986105

      可以看到bean1和bean2的的hashCode是完全相同的,也就是说,Spring的IOC容器中的bean,默认都是单例模式,我们可以通过修改bean的scope属性,来设置是否单例

      • prototype:原型模式

      • singleton:单例模式

      我们在applicationContext.xml文件中,将bean设置为原型模式:

        <bean id="dataSource"  scope="prototype" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${jdbc.driver}"/>
              <property name="url" value="${jdbc.url}"/>
              <property name="username" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>

      此时再次执行上面的测试类,输出:

      171497379
      2012846597

      可以看到bean1和bean2的的hashCode不再相同。

    • 0
    • 0
    • 0
    • 1.7k
    • 请登录之后再进行评论

      登录

      赞助本站

      • 支付宝
      • 微信
      • QQ

      感谢一直支持本站的所有人!

      单栏布局 侧栏位置: