SpringBoot入门第一集

打开SpringBoot的大门,感受SpringBoot带来的喜悦

欢迎来到SpringBoot入门第一集

你好! 这是你第一次使用 SpringBoot 来开启新世界大门。如果你想学习如何使用SpirngBoot, 可以仔细阅读这篇文章,了解一下SpringBoot的基本知识。

SpringBoot是什么?

SpringBoot是在Spring、SpringMVC+myBatis之上,衍生出来的spring的一种,SpringBoot可以说是集成了Spring、SpringMVC所有特性,拥有Spring的IOC控制反转和AOP面向切面编程的核心技术,简化了Spring创建对象的过程,升华了Spring对于一些功能代码的耦合:

了解了SpringBoot是什么后,接下来进入正题

xml和javaConfig

Spring使用xml作为容器配置文件,在3.0以后加入了javaconfig,使用java类配置文件使用

1.1.1 什么是javaConfig

javaConfig :是Spring提供的使用Java类配置容器,配置Spring IOC容器的纯java方法
优点:

  1. 可以使用面向对象的方式,一个配置类可以继承配置类,可以重写方法
  2. 避免繁琐的xml配置

1.1.2xml配置容器

创建01-pre-boot项目
pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
<!--            编译插件-->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
<!--                插件的版本-->
                <version>3.5.1</version>
<!--                编译级别-->
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
<!--                    编码格式-->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

创建数据类

public class Student {
    
    
    private String name;
    private Integer age;
    private String sex;

resources目录下创建Spring的配置文件 bean.xml


    <bean id="myStudent" class="com.demo.vo.Student">
        <property name="name" value="李明"/>
        <property name="age" value="22"/>
        <property name="sex" value=""/>
    </bean>

单元测试

 /**
     * 使用xml配置文件
     */
    @Test
    public void test(){
    
    
        String config = "bean.xml";
        ApplicationContext app = new ClassPathXmlApplicationContext(config);
        Student myStudent = (Student) app.getBean("myStudent");
        System.out.println(myStudent);
    }

1.1.3 JavaConfig配置容器

javaConfig主要使用的注解
@Configuration:放在类的上面,这个类相当于xml配置文件,可以在其中声明bean
@Bean:放在方法的上面,方法的返回值是对象类型,这个对象注入到spring ioc 容器

创建配置类(等同于xml配置文件)

在com.demo.config包下创建配置类

package com.demo.config;/**
 * @Author: Ma RuiFeng
 * @Description:
 * @Date: Created in 10:58 2022/2/25
 * @Modified By:
 */

import com.demo.vo.Student;
import org.springframework.context.annotation.*;

/**
 * @Configration: 表示当前类是作为配置文件使用的,就是用来配置容器的
 *            位置:类的上面
 *
 *      springconfig这个类就相当于bean.xml
 */

/**
 * @ImportResource(其他配置文件): 在类的上面,可以指定多个value值
 */
@Configuration
@ImportResource(value={
    
    "classpath:catBean.xml","classpath:bean.xml"})
//用注解创建tiger对象
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages  = "com.demo.vo")
public class SpringConfig {
    
    

    /**
     * 创建方法,方法的返回值是对象,在方法的上面加入@Bean
     * 方法的返回值对象就注入到容器中
     *
     * @Bean:吧对象注入到spring容器中,作用相当于<bean></bean>
     *      位置 在方法的上面
     *      说明:@Bean,不指定对象的名称,默认是方法名是id
     */
    @Bean
    public Student createStudent(){
    
    
        Student student = new Student();
        student.setName("张三");
        student.setAge(22);
        student.setSex("男");
        return student;
    }

    /**
     * 指定对象在容器中的名称(指定<bean>的id属性)
     * @Bean 的name属性,指定对象的名称(id)
     */
    @Bean(name="listStudent")
    public Student makeStudent(){
    
    
        Student s1 = new Student();
        s1.setName("lisi");
        s1.setAge(25);
        s1.setSex("nv");
        return s1;
    }
}

测试方法

package com.demo;/**
 * @Author: Ma RuiFeng
 * @Description:
 * @Date: Created in 10:41 2022/2/25
 * @Modified By:
 */

import com.demo.config.SpringConfig;
import com.demo.vo.Cat;
import com.demo.vo.Student;
import com.demo.vo.Tiger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.security.AllPermission;

/**
 *
 *  @Description      :some description
 *  @author           :XXX
 *  @version
 *  @date             :2022/2/25 10:41
 *
 */


public class MyTest {
    
    
    /**
     * 使用xml配置文件
     */
    @Test
    public void test(){
    
    
        String config = "bean.xml";
        ApplicationContext app = new ClassPathXmlApplicationContext(config);
        Student myStudent = (Student) app.getBean("myStudent");
        System.out.println(myStudent);
    }
    /**
     * 使用configuration创建对象
     */
    @Test
    public void test2(){
    
    
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student createStudent = (Student) app.getBean("createStudent");
        System.out.println("使用javaconfig创建的bean对象:"+createStudent);
    }
    /**
     * bean 的id自定义属性名
     */
    @Test
    public void test3(){
    
    
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student listStudent = (Student) app.getBean("listStudent");
        System.out.println("使用javaconfig创建的bean对象:"+listStudent);
    }
    @Test
    public void test4(){
    
    
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        Cat myCat = (Cat) app.getBean("myCat");
        System.out.println("使用javaconfig创建的bean对象:"+myCat);
    }
    @Test
    public void test5(){
    
    
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        Tiger myTiger = (Tiger) app.getBean("tiger");
        System.out.println("使用javaconfig创建的bean对象:"+myTiger);
    }
}

1.1.4 @ImportResource

@ImportResource是导入xml配置,等同于xml文件的resources
创建数据类

public class Cat {
    
    
    private String Idcard;
    private String name;
    private Integer age;
//set//get方法

创建配置文件catBean.xml

<?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 https://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="myCat" class="com.demo.vo.Cat">
        <property name="name" value="tomcat"/>
        <property name="age" value="10"/>
        <property name="idcard" value="104010201"/>
    </bean>
<!--    <context:property-placeholder location="classpath:config.properties"/>-->
<!--    <context:component-scan base-package="com.demo.vo"/>-->
<!--    <import resource="classpath:bean.xml"/>-->

创建配置类

@Configuration
@ImportResource(value={
    
    "classpath:catBean.xml","classpath:bean.xml"})
public class SpringConfig {
    
    }

测试类

@Test
    public void test4(){
    
    
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        Cat myCat = (Cat) app.getBean("myCat");
        System.out.println("使用javaconfig创建的bean对象:"+myCat);
    }

1.1.5 @PropertyResource

@PropertyResource是读取properties属性配置文件

在resources目录下创建config.properties

tiger.name=东北虎
tiger.age=3

创建数据类Tiger

@Component("tiger")
public class Tiger {
    
    
    @Value("${tiger.name}")
    private String name;
    @Value("${tiger.age}")
    private Integer age;
    //get/set方法

@Configuration
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages  = "com.demo.vo")
public class SpringConfig {
    
    


//使用@Bean声明bean

测试代码

 @Test
    public void test5(){
    
    
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
        Tiger myTiger = (Tiger) app.getBean("tiger");
        System.out.println("使用javaconfig创建的bean对象:"+myTiger);
    }

至此到这里,SpringBoot入门第一集就结束了,有哪里做的不对的地方尽情留言,我会及时加以更改,希望你们能够踊跃提出宝贵意见

猜你喜欢

转载自blog.csdn.net/weixin_43608968/article/details/123138833