Spring框架学习(二)(Spring配置数据源、Spring注解开发、Spring整合Junit)
      
      
    
   
  
  
    Spring配置数据源、Spring注解开发、Spring整合Junit
Spring 配置数据源
一、数据源(连接池)的作用
1、数据源(连接池)是为了提高程序性能才出现的
2、事先实例化数据源,初始化部分连接资源
3、使用连接资源时从数据源中获取
4、使用完毕后将连接资源归还给数据源
常见的数据源:DBCP、C3P0、BoneCP、Druid等。
二、手动创建数据源
1、手动创建C3P0数据源
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | 
 
 @Test
 public void test1() throws Exception {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setDriverClass("com.mysql.jdbc.Driver");
 dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
 dataSource.setUser("root");
 dataSource.setPassword("root");
 Connection connection = dataSource.getConnection();
 System.out.println(connection);
 connection.close();
 }
 
 | 
2、手动创建Druid数据源
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | 
 
 @Test
 public void test2() throws Exception {
 DruidDataSource dataSource = new DruidDataSource();
 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
 dataSource.setUrl("jdbc:mysql://localhost:3306/test");
 dataSource.setUsername("root");
 dataSource.setPassword("root");
 Connection connection = dataSource.getConnection();
 System.out.println(connection);
 connection.close();
 }
 
 | 
3、配置文件创建C3P0数据源
(1)现在resources目录下新建一个jdbc.properties数据库配置文件
| 12
 3
 4
 
 | jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:
 jdbc.username=root
 jdbc.password=root
 
 | 
(2)使用数据库配置文件加载
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | 
 
 @Test
 public void test3() throws Exception {
 
 ResourceBundle rb = ResourceBundle.getBundle("jdbc");
 String driver = rb.getString("jdbc.driver");
 String url = rb.getString("jdbc.url");
 String username = rb.getString("jdbc.username");
 String password = rb.getString("jdbc.password");
 
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setDriverClass(driver);
 dataSource.setJdbcUrl(url);
 dataSource.setUser(username);
 dataSource.setPassword(password);
 Connection connection = dataSource.getConnection();
 System.out.println(connection);
 connection.close();
 }
 
 | 
三、Spring配置数据源
导入Spring所需要的坐标之后,配置Spring的核心配置文件。在核心文件内部配置标签
| 12
 3
 4
 5
 6
 
 | <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property>
 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
 <property name="user" value="root"></property>
 <property name="password" value="root"></property>
 </bean>
 
 | 
编写测试类,测试运行结果
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 |     
 
 @Test
 public void test() throws Exception {
 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
 ComboPooledDataSource dataSource = (ComboPooledDataSource) app.getBean("dataSource");
 Connection connection = dataSource.getConnection();
 System.out.println(connection);
 connection.close();
 }
 
 
 
 
 
 | 
四、抽取jdbc配置文件
applicationContext.xml加载jdbc.properties配置文件获得连接信息。
首先,需要引入context命名空间和约束路径:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | <?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: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.xsd">
 
 
 <context:property-placeholder location="classpath:jdbc.properties"/>
 
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="driverClass" value="${jdbc.driver}"></property>
 <property name="jdbcUrl" value="${jdbc.url}"></property>
 <property name="user" value="${jdbc.username}"></property>
 <property name="password" value="${jdbc.password}"></property>
 </bean>
 
 </beans>
 
 | 
编写测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 |     
 
 @Test
 public void test() throws Exception {
 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
 ComboPooledDataSource dataSource = (ComboPooledDataSource) app.getBean("dataSource");
 Connection connection = dataSource.getConnection();
 System.out.println(connection);
 connection.close();
 }
 
 
 
 
 
 | 
Spring 注解开发
一、Spring原始注解
Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。
Spring原始注解主要是替代的配置
| 注解 | 说明 | 
| @Component | 使用在类上用于实例化Bean | 
| @Controller | 使用在Web层类上用于实例化Bean | 
| @Service | 使用在Service层类上用于实例化Bean | 
| @Repository | 使用在Dao层类上用于实例化Bean | 
| @Autowired | 使用在字段上用于根据类型依赖注入 | 
| @Qualifier | 结合@Autowired一起使用用于根据名称进行依赖注入 | 
| @Resource | 相当于@Autowired + @Qualifier,按照名称注入 | 
| @Value | 注入普通属性 | 
| @Scope | 标注Bean的作用范围 | 
| @PostConstruct | 使用在方法上标注该方法是Bean的初始化方法 | 
| @PreDestroy | 使用在方法上标注该方法是Bean的销毁方法 | 
注意事项:使用注解开发时,需要在applicationContext.xml中配置组件扫描,作用时指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。
| 12
 
 | <context:component-scan base-package="com.itheima"></context:component-scan>
 
 | 
当出现NoSuchBeanDefinitionException异常时则说明是没有配置上面的组件扫描或配置有误!
二、代码演示注解开发
1、配置UserDaoImpl实体类注解
| 12
 3
 4
 5
 6
 7
 
 | @Repository("userDao")
 public class UserDaoImpl implements UserDao {
 public void save() {
 System.out.println("save running...");
 }
 }
 
 | 
2、配置UserServiceImpl带注入依赖的注解配置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 
 | @Service("userService")
 
 @Scope("singleton")
 public class UserServiceImpl implements UserService {
 @Value("${jdbc.driver}")
 private String driver;
 
 
 
 @Resource(name = "userDao")
 private UserDao userDao;
 
 public void setUserDao(UserDao userDao) {
 this.userDao = userDao;
 }
 
 public void save() {
 System.out.println(driver);
 userDao.save();
 }
 
 @PostConstruct
 public void init(){
 System.out.println("Service对象的初始化方法");
 }
 
 @PreDestroy
 public void dextory(){
 System.out.println("Service对象的销毁方法");
 }
 }
 
 | 
3、配置组件扫描
| 1
 | <context:component-scan base-package="com.itheima"/>
 | 
4、编写测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | 	public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
 UserService userService = app.getBean(UserService.class);
 userService.save();
 ((ClassPathXmlApplicationContext) app).close();
 }
 
 
 
 
 
 
 
 
 | 
三、Spring新注解
使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:
| 注解 | 说明 | 
| @Configuration | 用于指定当前类是Spring配置类,当创建容器时会从该类上加载注解 | 
| @ComponentScan | 用于指定Spring初始化容器时要扫描的包。作用同Spring的xml文件中的  <context:component-scan base-package=”com.itheima”/> | 
| @Bean | 标注在方法上,标注将该方法的返回值存储到Spring容器中 | 
| @PropertySource | 用于加载.properties文件中的配置 | 
| @Import | 用于导入其他配置类 | 
使用类的方式替代文件,注解替代标签。
1、创建Spring框架的配置类——使用类替代文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | @Configuration
 @ComponentScan("com.itheima")
 @Import({DataSourceConfiguration.class})
 public class SpringConfiguration {
 }
 
 
 @PropertySource("jdbc.properties")
 public class DataSourceConfiguration {
 
 @Value("${jdbc.driver}")
 private String driver;
 @Value("${jdbc.url}")
 private String url;
 @Value("${jdbc.username}")
 private String user;
 @Value("${jdbc.password}")
 private String password;
 
 @Bean("dataSource")
 public DataSource getDataSource() throws PropertyVetoException {
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 dataSource.setDriverClass(driver);
 dataSource.setJdbcUrl(url);
 dataSource.setUser(user);
 dataSource.setPassword(password);
 return dataSource;
 }
 }
 
 | 
2、编写测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 |     
 
 
 @Test
 public void test4() {
 
 ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
 UserService userService = (UserService) app.getBean("userService");
 userService.save();
 }
 
 
 
 
 
 
 | 
Spring集成junit开发
一、Spring集成junit-集成简介和开发步骤
1、Spring测试的问题
在测试类中,每个测试方法都有如下两行代码
| 12
 
 | ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");ComboPooledDataSource dataSource = (ComboPooledDataSource) app.getBean("dataSource");
 
 | 
这两行代码的作用是获取容器对象,不写的话会报空指针异常。
2、解决思路
(1)让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它;
(2)将需要进行测试的类直接在测试类中注入。
3、Spring集成Junit的步骤
(1)导入Spring集成junit的坐标
(2)使用@Runwith注解替换原来的运行期
(3)使用@ContextConfiguration指定配置文件
(4)使用@Autowired注入需要测试的对象
(5)创建测试方法进行测试。
4、代码演示
(1)导入Spring集成Junit的坐标
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 |       <dependency><groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>5.2.7.RELEASE</version>
 </dependency>
 
 | 
注意事项:此处的junit的版本必须是4.12或更高的版本才可以,不然在编写测试类时会报ExceptionInInitializerError异常。关于该异常的信息可在分类选项中的问题收集标签中找到相关信息。
(2)配置测试类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes = {SpringConfiguration.class})
 public class SpringJunitTest {
 
 @Autowired
 private UserService userService;
 @Autowired
 private UserDao userDao;
 
 @Test
 public void test2(){
 userDao.save();
 }
 @Test
 public void test1(){
 userService.save();
 
 
 
 
 }
 }
 
 | 
参考资料:2020年 最新版 传智黑马Java SSM 阶段 采用IDEA教学