zoukankan      html  css  js  c++  java
  • spring基础(二)

    替代xml文件

    配置的java文件

      

     1 package com.oracle.config;
     2 
     3 import org.springframework.context.annotation.*;
     4 /**
     5  * 该类是一个配置类,他的作用和bean.xml是一样的
     6  * spring中的新注解
     7  * Configuration
     8  *      作用:指定当前类是一个配置类
     9  *      细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,改注解可以不写
    10  * ComponentScan
    11  *      作用:用于通过注解指定spring在从创建容器时要扫描的包
    12  *      属性:他和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
    13  *              我们使用此注解就等同于在xml中配置了:
    14  *                      <context:component-scan base-package="com.oracle"></context:component-scan>
    15  * Bean
    16  *      作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
    17  *      属性:
    18  *          name:用于指定bean的id。当不写时,默认值是当前方法的名称
    19  *      细节:
    20  *          当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
    21  *          查找的方法和Autowired注解的作用是一样的
    22  * Import
    23  *      作用:用于导入其他的配置类
    24  *      属性:
    25  *          value:用于指定其他配置类的字节码。
    26  *                   当我们使用Import的注解之后,有Import注解的类就是父配置类,而导入的都是子配置类
    27  *  PropertySource
    28  *      作用:用于指定properties文件的位置
    29  *      属性:
    30  *          value:指定文件的名称和位置
    31  *              关键字:classpath,表示类路径下
    32  *
    33  *
    34  *
    35  */
    36 @Configuration
    37 @ComponentScan({"com.oracle"})
    38 @Import(JdbcConfig.class)
    39 @PropertySource("classpath:jdbcConfig.properties")
    40 public class SpringConfiguration {
    41 
    42 
    43 
    44 }

    子配置文件

     1 package com.oracle.config;
     2 
     3 import com.mchange.v2.c3p0.ComboPooledDataSource;
     4 import org.apache.commons.dbutils.QueryRunner;
     5 import org.springframework.beans.factory.annotation.Value;
     6 import org.springframework.context.annotation.Bean;
     7 import org.springframework.context.annotation.Scope;
     8 import org.springframework.stereotype.Component;
     9 import org.springframework.stereotype.Service;
    10 
    11 import javax.sql.DataSource;
    12 
    13 @Component
    14 @Service("jdbcConfig")
    15 public class JdbcConfig {
    16 
    17     @Value("${jdbc.driver}")
    18     private String driver;
    19 
    20     @Value("${jdbc.url}")
    21     private String url;
    22 
    23     @Value("${jdbc.username}")
    24     private String username;
    25 
    26     @Value("${jdbc.password}")
    27     private String password;
    28 
    29     public void testA() {
    30         System.out.println(driver + "," + url + "," + username + "," + password);
    31     }
    32 
    33 
    34     /**
    35      * 用于创建一个queryRunner对象
    36      *
    37      * @param dataSource
    38      * @return
    39      */
    40     @Bean
    41     public QueryRunner createQueryRunner(DataSource dataSource) {
    42         return new QueryRunner(dataSource);
    43     }
    44 
    45     /**
    46      * 创建数据源对象
    47      *
    48      * @return
    49      */
    50     @Bean("dataSource")
    51     @Scope("prototype")
    52     public DataSource createDataSource() {
    53         ComboPooledDataSource ds = new ComboPooledDataSource();
    54         try {
    55             ds.setDriverClass(driver);
    56             ds.setJdbcUrl(url);
    57             ds.setUser(username);
    58             ds.setPassword(password);
    59             return ds;
    60         } catch (Exception e) {
    61             throw new RuntimeException(e);
    62         }
    63     }
    64 }

    spring 整个junit 测试

      

     1 package com.oracle.TestDemo;
     2 
     3 import com.oracle.config.JdbcConfig;
     4 import com.oracle.config.SpringConfiguration;
     5 import com.oracle.domain.Account;
     6 import com.oracle.service.IAccountService;
     7 import org.junit.Before;
     8 import org.junit.Test;
     9 import org.junit.runner.RunWith;
    10 import org.springframework.beans.factory.annotation.Autowired;
    11 import org.springframework.context.ApplicationContext;
    12 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    13 import org.springframework.context.support.ClassPathXmlApplicationContext;
    14 import org.springframework.test.context.ContextConfiguration;
    15 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    16 
    17 import java.applet.AppletContext;
    18 import java.util.List;
    19 
    20 /**
    21  * 使用junit单元测试:测试我们的配置
    22  * spring整合junit的配置
    23  *      1、导入spring提供的junit的jar包(坐标)
    24  *      2、使用junit提供的注解把原有的main方法替换了。替换为spring提供的
    25  *          @RunWith
    26  *      3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并说明位置
    27  *          @ContextConfiguration
    28  *                  location:指定xml文件的位置,加上classpath关键字。表示在类路径下
    29  *                  classes:指定注解类所在的位置
    30  * 注意:当spring版本是5.x的时候,必须使用junit 4.12及以上版本
    31  */
    32 
    33 /**
    34  * 使用junit单元测试
    35  */
    36 @RunWith(SpringJUnit4ClassRunner.class)
    37 @ContextConfiguration(classes = SpringConfiguration.class)
    38 public class Demo01 {
    39 
    40     @Autowired
    41     private  ApplicationContext ac;
    42     @Autowired
    43     private IAccountService accountService;
    44 
    45 
    46     @Test
    47     public void testFindAll() {
    48         /*3、执行方法*/
    49         List<Account> allAccount = accountService.findAllAccount();
    50         for (Account account : allAccount) {
    51             System.out.println(account.getId() + "," + account.getName() + "," + account.getMoney());
    52         }
    53 
    54     }
    55 
    56     @Test
    57     public void testFindOne() {
    58         /*3、执行方法*/
    59         Account accountById = accountService.findAccountById(2);
    60         System.out.println(accountById);
    61     }
    62 
    63     @Test
    64     public void testSave() {
    65         Account account = new Account();
    66         account.setName("ddd");
    67         account.setMoney(1999F);
    68         /*3、执行方法*/
    69         accountService.saveAccount(account);
    70     }
    71 
    72     @Test
    73     public void testUpdate() {
    74         Account account = new Account();
    75         account.setId(1);
    76         account.setName("AAA");
    77         account.setMoney(7744F);
    78         /*3、执行方法*/
    79         accountService.updateAccount(account);
    80     }
    81 
    82     @Test
    83     public void testDelete() {
    84         /*3、执行方法*/
    85         accountService.deleteAccount(6);
    86     }
    87 }
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>org.example</groupId>
     8     <artifactId>Day05</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10     <packaging>jar</packaging>
    11 
    12     <dependencies>
    13         <dependency>
    14             <groupId>org.springframework</groupId>
    15             <artifactId>spring-context</artifactId>
    16             <version>5.0.2.RELEASE</version>
    17         </dependency>
    18         <dependency>
    19             <groupId>commons-dbutils</groupId>
    20             <artifactId>commons-dbutils</artifactId>
    21             <version>1.4</version>
    22         </dependency>
    23 
    24         <dependency>
    25             <groupId>mysql</groupId>
    26             <artifactId>mysql-connector-java</artifactId>
    27             <version>5.1.6</version>
    28         </dependency>
    29 
    30         <dependency>
    31             <groupId>c3p0</groupId>
    32             <artifactId>c3p0</artifactId>
    33             <version>0.9.1.2</version>
    34         </dependency>
    35 
    36         <dependency>
    37             <groupId>junit</groupId>
    38             <artifactId>junit</artifactId>
    39             <version>4.12</version>
    40         </dependency>
    41         <dependency>
    42             <groupId>org.springframework</groupId>
    43             <artifactId>spring-test</artifactId>
    44             <version>5.0.2.RELEASE</version>
    45         </dependency>
    46 
    47     </dependencies>
    48 
    49 </project>
  • 相关阅读:
    埃拉托色尼的素数查找法的优化问题
    Salesforce开发者学习笔记之三:Salesforce平台架构
    Salesforce开发者学习笔记之二:Salesforce开发平台应用场景
    Salesforce开发者学习笔记之一:基本知识
    Salesforce Apex 使用JSON数据的示例程序
    Salesforce Apex页面中调用远端网络服务
    Salesforce Apex 开发环境设置和Hello World示例
    四、Java 安装
    三、Windows 基础
    二、大数据生态圈尝鲜
  • 原文地址:https://www.cnblogs.com/yanghaoyu0624/p/12020664.html
Copyright © 2011-2022 走看看