SpringBoot Getting Started Episode 1

Open the door of SpringBoot and feel the joy brought by SpringBoot

Welcome to the first episode of Getting Started with SpringBoot

Hello! This is your first time using SpringBoot to open the door to a new world. If you want to learn how to use SpringBoot, you can read this article carefully to understand the basic knowledge of SpringBoot.

What is SpringBoot?

SpringBoot is a kind of spring derived from Spring, SpringMVC+myBatis. SpringBoot can be said to integrate all the features of Spring and SpringMVC. It has the core technology of Spring's IOC control inversion and AOP aspect-oriented programming, simplifying Spring The process of creating objects sublimates Spring's coupling of some functional codes:

After understanding what SpringBoot is, let's get to the point

xml and javaConfig

Spring uses xml as the container configuration file. After 3.0, javaconfig was added, and the java class configuration file is used.

1.1.1 What is javaConfig

javaConfig: It is a pure java method provided by Spring that uses Java classes to configure containers and configure Spring IOC containers.
Advantages:

  1. You can use the object-oriented approach. A configuration class can inherit the configuration class and override methods.
  2. Avoid cumbersome xml configuration

1.1.2xml configuration container

Create 01-pre-boot project
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>

Create data class

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

Create the Spring configuration file bean.xml in the resources directory


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

unit test

 /**
     * 使用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 configuration container

The annotation @Configuration mainly used by javaConfig
: placed on top of the class, this class is equivalent to the xml configuration file, in which beans can be declared
@Bean: placed on top of the method, the return value of the method is the object type, this object is injected into the spring ioc container

Create a configuration class (equivalent to an xml configuration file)

Create a configuration class under the com.demo.config package

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;
    }
}

Test Methods

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 is to import xml configuration, which is equivalent to
creating data classes for resources in xml files

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

Create configuration file 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"/>-->

Create a configuration class

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

Test class

@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 is to read the properties attribute configuration file

Create config.properties in the resources directory

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

Create data class 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 code

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

At this point, the first episode of getting started with SpringBoot is over. If there is anything I did wrong, please feel free to leave a message. I will make changes in time. I hope you can give your valuable opinions.

Guess you like

Origin blog.csdn.net/weixin_43608968/article/details/123138833