【SSM直击大厂】第三章:Spring配置数据源及注解开发

目录

1.Spring配置数据源

1.1 数据源(连接池)的作用

1.2 数据源的开发步骤

 1.3 Spring配置数据源

1.4 抽取jdbc配置文件配置数据源

 1.5 知识要点

2.Spring注解开发

2.1 Spring原始注解

2.2 Spring新注解


1.Spring配置数据源

1.1 数据源(连接池)的作用

  • 数据源(连接池)是提高程序性能如出现的
  • 事先实例化数据源,初始化部分连接资源
  • 使用连接资源时从数据源中获取
  • 使用完毕后将连接资源归还给数据源

常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等。

1.2 数据源的开发步骤

  1. 导入数据源的坐标和数据库驱动坐标
  2. 创建数据源对象
  3. 设置数据源的基本连接数据
  4. 使用数据源获取连接资源和归还连接资源

 1.3 Spring配置数据源

  1. 可以将DataSource的创建权交由Spring容器去完成
  2. DataSource有无参构造方法,而Spring默认就是通过无参构造方法实例化对象的
  3. DataSource要想使用需要通过set方法设置数据库连接信息,而Spring可以通过set方法进行字符串注入
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>

测试从容器当中获取数据源

ApplicationContext applicationContext = new 
ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);

1.4 抽取jdbc配置文件配置数据源

<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

applicationContext.xml加载jdbc.properties配置文件获得连接信息。

首先,需要引入context命名空间和约束路径:

  1. 命名空间:xmlns:context="http://www.springframework.org/schema/context"
  2. 约束路径:http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd

 1.5 知识要点

Spring容器加载properties文件

<!-- 大致模板 -->
<context:property-placeholder location="xx.properties"/>
<property name=" " value="${key}"/>

2.Spring注解开发

2.1 Spring原始注解

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。 Spring原始注解主要是替代<Bean>的配置。

2.2 Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下

  • 非自定义的Bean的配置: <bean>
  • 加载properties文件的配置: <context:property-placeholder>
  • 组件扫描的配置: <context:component-scan>
  • 引入其他文件: <import>
@Configuration
@ComponentScan
@Import
@Configuration
@ComponentScan("com.project")
@Import({DataSourceConfiguration.class})
public class SpringConfiguration { }

@PropertySource

@value
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;}
@Bean
@Bean(name="dataSource")
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource; }
测试加载核心配置类创建Spring容器
@Test
public void testAnnoConfiguration() throws Exception {
ApplicationContext applicationContext = new 
AnnotationConfigApplicationContext(SpringConfiguration.class);
UserService userService = (UserService) 
applicationContext.getBean("userService");
userService.save();
DataSource dataSource = (DataSource) 
applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}

猜你喜欢

转载自blog.csdn.net/qq_52360069/article/details/123614338