SSM_Spring_Day01(概述、快速入门、核心配置文件、API、配置数据源、注解开发、集成Junit)

SSM_Spring_Day01(概述、快速入门、核心配置文件、API、配置数据源、注解开发、集成Junit)

1. spring概述

1.1 Spring是什么(理解)

Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核。

提供了展现层 SpringMVC持久层 Spring JDBCTemplate、Mybatis 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架。

1.2 Spring发展历程 (了解)

在这里插入图片描述

1997 年, IBM提出了EJB 的思想

1998 年,SUN制定开发标准规范 EJB1.0

1999 年,EJB1.1 发布

2001 年,EJB2.0 发布

2003 年,EJB2.1 发布

2006 年,EJB3.0 发布

Rod Johnson ( Spring 之父)

Expert One-to-One J2EE Design and Development(2002)

阐述了 J2EE 使用EJB 开发设计的优点及解决方案

Expert One-to-One J2EE Development without EJB(2004)

阐述了 J2EE 开发不使用 EJB的解决方式(Spring 雏形)

2017 年 9 月份发布了 Spring 的最新版本 Spring5.0 通用版(GA)

1.3 Spring的优势(理解)

1)方便解耦,简化开发

通过 Spring 提供的 IoC容器,可以将对象间的依赖关系交由 Spring 进行控制,避免硬编码所造成的过度耦合。

用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。

2)AOP 编程的支持

通过 Spring的 AOP 功能,方便进行面向切面编程,许多不容易用传统 OOP 实现的功能可以通过 AOP 轻松实现。

3)声明式事务的支持

可以将我们从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活的进行事务管理,提高开发效率和质量。

4)方便程序的测试

可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情。

5)方便集成各种优秀框架

Spring对各种优秀框架(Struts、Hibernate、Hessian、Quartz等)的支持。

6)降低 JavaEE API 的使用难度

Spring对 JavaEE API(如 JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些 API 的使用难度大为降低。

7)Java 源码是经典学习范例

Spring的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对Java 设计模式灵活运用以及对 Java技术的高深 造诣。它的源代码无意是 Java 技术的最佳实践的范例。

1.4 Spring的体系结构(了解)

在这里插入图片描述

https://spring.io/projects/spring-framework#learn

在这里插入图片描述

2. spring快速入门

2.1. Spring程序开发步骤

①导入 Spring 开发的基本包坐标

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.lanqiao</groupId>
  <artifactId>spring_01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>spring_01 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <spring.version>5.2.12.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
  </dependencies>
</project>

②编写 Dao 接口和实现类

public interface UserMapper {

    public  void insert();
    public  void delete();
    public  void update();
    public void  select();
}

/**
 * @author Mr.Hu
 * @create 2021/04/23
 */
public class UserMapperImpl implements UserMapper {
    @Override
    public void insert() {
        System.out.println("保存数据成功");
    }

    @Override
    public void delete() {
        System.out.println("删除数据成功");
    }

    @Override
    public void update() {
        System.out.println("更新数据成功");
    }

    @Override
    public void select() {
        System.out.println("查询数据成功");
    }
}

③创建 Spring 核心配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    在spring容器中注册一个Javabean-->
    <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl"></bean>
</beans>

④在 Spring 配置文件中配置 UserDaoImpl

<!--    在spring容器中注册一个Javabean-->
    <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl"></bean>

⑤使用 Spring 的 API 获得 Bean 实例

   public static void main(String[] args) {
        //在这里通过spring容器获取UserMapper对象
        //获取spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
       //从spring容器中获取bean对象
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        userMapper.insert();
        userMapper.delete();
    }

获取bean 不用强转

UserMapper userMapper = applicationContext.getBean("userMapper",UserMapper.class);

3. Spring的核心配置文件

文件名称:默认的名称为applicationContext.xml

也可以自己来命名,名称随意

 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");

在这里插入图片描述

3.2. Bean标签范围配置

scope:指对象的作用范围,取值如下

1)当scope的取值为singleton

Bean的实例化个数:1个

Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例

Bean的生命周期:

  • 对象创建:当应用加载,创建容器时,对象就被创建了

  • 对象运行:只要容器在,对象一直活着

  • 对象销毁:当应用卸载,销毁容器时,对象就被销毁了

    2)当scope的取值为****prototype时

    Bean的实例化个数:多个

    Bean的实例化时机:当调用getBean()方法时实例化Bean

    • 对象创建:当使用对象时,创建新的对象实例
    • 对象运行:只要对象在使用中,就一直活着
    • 对象销毁:当对象长时间不用时,被 Java 的垃圾回收器回收了
取值范围说明
singleton默认值,单例的
prototype多例的
requestWEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中
sessionWEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中
global sessionWEB 项目中,应用在 Portlet 环境,如果没有 Portlet 环境那么globalSession 相当于 session
  <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl" scope="singleton"></bean>

singleton也是默认取值
在这里插入图片描述

 <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl" scope="prototype"></bean>

在这里插入图片描述

spring容器创建对象 默认使用无参构造

3.3. Bean生命周期配置

  • init-method:指定类中的初始化方法名称
  • destroy-method:指定类中销毁方法名称

3.4. Bean实例化三种方式

1) 使用无参构造方法实例化

 <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl" scope="singleton" ></bean>

它会根据默认无参构造方法来创建类对象,如果bean中没有默认无参构造函数,将会创建失败

2) 工厂静态方法实例化

工厂的静态方法返回Bean实例

public class UserMapperFactory {
    public  static UserMapper createUserMapperInstance(){
        return  new UserMapperImpl(10);
    }
}
 <bean id="userMapper1" class="cn.lanqiao.factory.UserMapperFactory" factory-method="createUserMapperInstance"></bean>
 UserMapper userMapper = applicationContext.getBean("userMapper1",UserMapper.class);
        userMapper.update();

对于这种工厂静态方法实例化对象的时候 此时对象可以没有无参构造

3) 工厂实例方法实例化

工厂的非静态方法返回Bean实例

public class UserMapperFactory1 {
    public UserMapper createUserMapper(){
        System.out.println("调用了实例工厂来创建对象");
        return  new UserMapperImpl();
    }
}
<!--    在spring容器中注册一个Javabean-->

    <bean id="userMapperFactory" class="cn.lanqiao.factory.UserMapperFactory1"></bean>
<!--    使用工厂实例方法来实例化对象-->
    <bean id="userMapper" factory-bean="userMapperFactory" factory-method="createUserMapper" ></bean>
 UserMapper userMapper = applicationContext.getBean("userMapper",UserMapper.class);
        userMapper.update();

工厂方法实例化 主要用来解决实例化的对象没有无参构造的问题

3.5. Bean的依赖注入入门

public interface UserService {
     void  save();
}

public class UserServiceImpl  implements UserService {
    @Override
    public void save() {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        UserMapper userMapper =  context.getBean("userMapper",UserMapper.class);
        userMapper.insert();
    }
}
<!--    在spring容器中注册一个Javabean UserMapper-->
    <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl"></bean>
<!--    将service注册到Spring容器中-->
    <bean id="userService" class="cn.lanqiao.service.impl.UserServiceImpl"></bean>
    public static void main(String[] args) {
        //在这里通过spring容器获取UserMapper对象
        //获取spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        UserService service = applicationContext.getBean("userService" ,UserService.class);
        service.save();
    }

此时虽然可以在service中使用mapper了 但是在servcie中出现了spring容器相关的API 所以产生一个问题:service层与spring容器产生了耦合。

目前对于spring容器来说 已经知道了有service 和mapper这两个bean的存在了

我们希望spring可以帮助我么能自动完成将mapper传递给service这样的操作 而不要在service中出现spring的相关API

3.6. spring的依赖注入

public class UserServiceImpl  implements UserService {
    private UserMapper userMapper;//对于userMapper的对象的引用由spring来帮助我们完成

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void save() {
        userMapper.insert();
    }
}

在这里插入图片描述

3.7. Bean的依赖注入概念

依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现。

在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。

IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。

那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。

简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

3.8. Bean的依赖注入方式

怎么将UserDao怎样注入到UserService内部呢?

  • 构造方法
  • set方法

set注入

    <bean id="userService" class="cn.lanqiao.service.impl.UserServiceImpl">
<!--        属性注入  需要借助于属性的set方法来完成注入-->
        <property name="userMapper" ref="userMapper"></property>
    </bean>

构造注入
在这里插入图片描述

在这里插入图片描述

命名空间注入

p命名空间 对应set注入 此时也要求属性要有set方法
在这里插入图片描述

c命名空间 对应构造注入
在这里插入图片描述

3.9. Bean的依赖注入的数据类型

上面的操作,都是注入的引用Bean,处了对象的引用可以注入,普通数据类型,集合等都可以在容器中进行注入。

注入数据的三种数据类型

  • 普通数据类型
  • 引用数据类型
  • 集合数据类型

其中引用数据类型,此处就不再赘述了,之前的操作都是对UserDao对象的引用进行注入的,下面将以set方法注入为例,演示普通数据类型和集合数据类型的注入。

普通类型数据注入

public class User {
    private int id;
    private String name;
    private int age;
    <bean id="user" class="cn.lanqiao.pojo.User">
        <property name="id" value="1001"></property>
        <property name="name" value="admin"></property>
        <property name="age" value="20"></property>
    </bean>

在这里插入图片描述

构造注入

    <bean id="user" class="cn.lanqiao.pojo.User" c:id="1002" c:name="tom" c:age="21">

    </bean>

集合数据类型

list和set

public class User {
    private int id;
    private String name;
    private int age;
    private List<String> hobby;
  <bean id="user" class="cn.lanqiao.pojo.User">
        <property name="id" value="1001"></property>
        <property name="name" value="admin"></property>
        <property name="age" value="20"></property>
        <property name="hobby">
<!--            对于set集合的使用和list相同-->
            <list>
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>
    </bean>

map的注入

public class User {
    private int id;
    private String name;
    private int age;
    private List<String> hobby;
    private Map<String,Integer> score;
 <bean id="user" class="cn.lanqiao.pojo.User">
        <property name="id" value="1001"></property>
        <property name="name" value="admin"></property>
        <property name="age" value="20"></property>
        <property name="hobby">
<!--            对于set集合的使用和list相同-->
            <list>
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>
        <property name="score">
            <map>
                <entry key="语文" value="85"></entry>
                <entry key="java" value="90"></entry>
            </map>
        </property>
    </bean>

在集合中注入引用类型

public class Address {
    private String city;
    private String street;
public class User {
    private int id;
    private String name;
    private int age;
    private List<String> hobby;
    private Map<String,Integer> score;
    private Map<String,Address> shdz;
    <bean id="user" class="cn.lanqiao.pojo.User">
        <property name="id" value="1001"></property>
        <property name="name" value="admin"></property>
        <property name="age" value="20"></property>
        <property name="hobby">
<!--            对于set集合的使用和list相同-->
            <list>
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>
        <property name="score">
            <map>
                <entry key="语文" value="85"></entry>
                <entry key="java" value="90"></entry>
            </map>
        </property>
<!--        集合中注入引用类型-->
        <property name="shdz">
            <map>
                <entry key="add1" value-ref="add1"></entry>
                <entry key="add2" value-ref="add2"></entry>
            </map>
        </property>
    </bean>

在set集合中注入引用类型

public class User {
    private int id;
    private String name;
    private int age;
    private List<String> hobby;
    private Map<String,Integer> score;
    private Set<Address> shdz;
  <bean id="add1" class="cn.lanqiao.pojo.Address">
        <property name="city" value="北京"></property>
        <property name="street" value="长安大街"></property>
    </bean>
    <bean id="add2" class="cn.lanqiao.pojo.Address">
        <property name="city" value="太原"></property>
        <property name="street" value="迎泽大街"></property>
    </bean>
    <bean id="user" class="cn.lanqiao.pojo.User">
        <property name="id" value="1001"></property>
        <property name="name" value="admin"></property>
        <property name="age" value="20"></property>
        <property name="hobby">
<!--            对于set集合的使用和list相同-->
            <list>
                <value>足球</value>
                <value>篮球</value>
            </list>
        </property>
        <property name="score">
            <map>
                <entry key="语文" value="85"></entry>
                <entry key="java" value="90"></entry>
            </map>
        </property>
<!--        集合中注入引用类型-->
       <property name="shdz">
           <set>
               <ref bean="add1"></ref>
               <ref bean="add2"></ref>
           </set>
       </property>
    </bean>

配置文件的分割和整合

在分模块开发以及简单的分层开发中,为了使得配置文件结构清晰,便于维护,spring允许将配置文件先按照模块或者层次进行分割 分割为不同层的配置文件,然后进行统一的整合。

可以将分割之后的配置文件汇总到一个总的配置文件中

在这里插入图片描述

或者在创建spring容器的时候 同时加载多个配置文件

  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("dao.xml","service.xml");

4. Spring API

ApplicationContext 代表了Spring容器

创建容器方式:

在这里插入图片描述

  • ClassPathXmlApplicationContext:表示从类路径加下加载xml配置文件 构建ApplicationContext容器

    ​ 配置文件必须位于类路径下

  • FileSystemXmlApplicationContext :表示从文件系统中加载xml配置文件 构建ApplicationContext容器

    ​ 配置文件可以位于磁盘上的任意位置

  • AnnotationConfigApplicationContext:表示使用注解的方式来构建 构建ApplicationContext容器

获取Bean的方式

//通过bean的id获取  获取到的对象是一个Object对象 需要强转
Object getBean(String var1) throws BeansException;
// 通过bean的id  及对象的class来获取 此时获取之后 不需要强转
<T> T getBean(String var1, Class<T> var2) throws BeansException;
// 通过bean的id字符串获取  获取的同时还可以传入参数
Object getBean(String var1, Object... var2) throws BeansException;
//在获取的时候 根据传入的Class对象的类型来获取
<T> T getBean(Class<T> var1) throws BeansException;
 UserService  userService = applicationContext.getBean(UserService.class);

<T> T getBean(Class<T> var1, Object... var2) throws BeansException;

5. Spring配置数据源

5.1. 数据源(连接池)的作用

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

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

数据源的开发步骤

①导入数据源的坐标和数据库驱动坐标

②创建数据源对象

③设置数据源的基本连接数据

④使用数据源获取连接资源和归还连接资源

使用C3P0数据源

  <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.36</version>
    </dependency>    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
@Test
public void getC3P0DataSource() throws PropertyVetoException, SQLException {
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/lanqiao");
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setUser("root");
    dataSource.setPassword("root");
    Connection conn =dataSource.getConnection();
    System.out.println(conn);
  }

Druid数据源

<!-- Druid连接池 -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.10</version>
</dependency>
@Test public void testDruid() 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); 
}

将连接属性提炼到一个属性配置文件

jdbc.url=jdbc:mysql://localhost:3306/lanqiao
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
@Test
public void getC3P0DataSource() throws PropertyVetoException, SQLException {
    InputStream stream;
    ResourceBundle rb = ResourceBundle.getBundle("jdbc");
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setJdbcUrl(rb.getString("jdbc.url"));
    dataSource.setDriverClass(rb.getString("jdbc.driver"));
    dataSource.setUser(rb.getString("jdbc.user"));
    dataSource.setPassword(rb.getString("jdbc.password"));
    Connection conn =dataSource.getConnection();
    System.out.println(conn);
  }

5.2. Spring中获取数据源

<!--    配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lanqiao"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>
@Test
      public void getC3p0Bean() throws SQLException {
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
          DataSource dataSource = applicationContext.getBean(ComboPooledDataSource.class);
          System.out.println(dataSource.getConnection());
      }

优化,将连接属性配置到属性文件中

在这里插入图片描述

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--    在spring容器中注册一个Javabean UserMapper-->
    <bean id="userMapper" class="cn.lanqiao.mapper.impl.UserMapperImpl"/>
    <context:property-placeholder location="jdbc.properties"/>
<!--    配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
 </beans>

对于配置文件的位置 必须位于classpath下

 <context:property-placeholder location="classpath*:jdbc.properties"/>

6. Spring注解开发

6.1. 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的销毁方法

dao层
在这里插入图片描述

servcie

在这里插入图片描述

在这里插入图片描述

    public static void main(String[] args) {
        //在这里通过spring容器获取UserMapper对象
        //获取spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        UserService  userService = applicationContext.getBean(UserService.class);
        userService.save();
    }

以上的配置都是按照属性的类型来完成bean(spring组件)的注册和注入
在这里插入图片描述
在这里插入图片描述

普通属性的注入
在这里插入图片描述

在这里插入图片描述

6.2. Spring新注解

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

  • 非自定义的Bean的配置:
  • 加载properties文件的配置:context:property-placeholder
  • 组件扫描的配置:context:component-scan
  • 引入其他文件:
注解说明
@Configuration用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan用于指定 Spring 在初始化容器时要扫描的包。作用和在 Spring 的 xml 配置文件中的<context:component-scan base-package=“com.ssm”/>一样
@Bean使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource用于加载.properties 文件中的配置
@Import用于导入其他配置类

配置类

@Configuration//这是一个spring的配置类
@ComponentScan("cn.lanqiao")
@Import(DataSourceConfiguration.class)
public class SpringConfiguration {
}
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.user}")
    private String user;
    @Value("${jdbc.password}")
    private String password;

    @Bean//该方法的返回值存储到spring容器
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl(this.url);
        dataSource.setDriverClass(this.driver);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        return  dataSource;
    }
}
@Test
      public void getC3p0Bean() throws SQLException {
         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        DataSource dataSource =  context.getBean(DataSource.class);
    System.out.println(dataSource.getConnection());
      }

7. Spring集成Junit

7.1. 原始Junit测试Spring的问题

在测试类中,每个测试方法都有以下两行代码:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 

IAccountService as = ac.getBean("accountService",IAccountService.class

这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常。所以又不能轻易删掉。

7.2. 上述问题解决思路

  • 让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它
  • 将需要进行测试Bean直接在测试类中进行注入

7.3. Spring集成Junit步骤

①导入spring集成Junit的坐标

②使用@Runwith注解替换原来的运行期

③使用@ContextConfiguration指定配置文件或配置类

④使用@Autowired注入需要测试的对象

⑤创建测试方法进行测试

   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
@RunWith(SpringJUnit4ClassRunner.class)
//如果是基于注解 此时这里就是用classes
//如果使用的是配置文件 则属性用户的value或者locations
@ContextConfiguration(classes = {SpringConfiguration.class})
public class App {
    @Autowired
    private UserService userService;
    @Test
    public  void  saveTest(){
        userService.save();
    }
}

这里通过配置文件和 注解的方式 学习了spring的第一大核心 IOC(控制反转)和DI(依赖注入)

DI是IOC这种思想的一种实现方式

约定大于配置

@Resource注解

他是来自于JSR-250 jsr-250 是一个规范

因此使用的时候 需要导入

   <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-annotations-api</artifactId>
      <version>9.0.31</version>
    </dependency>

@Resource(name=“userMapper”)如果有name属性 则是按照名称注入

@Resource(name="userMapper")
private UserMapper userMapper;//对于userMapper的对象的引用由spring来帮助我们完成

在这里插入图片描述

如果没有name属性 则是按照属性的类型注入

@Resource
private UserMapper userMapper;//对于userMapper的对象的引用由spring来帮助我们完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值