SpringBoot への扉を開いて SpringBoot がもたらす喜びを感じてください
SpringBoot 入門の最初のエピソードへようこそ
こんにちは!SpringBoot を使用して新しい世界への扉を開くのはこれが初めてです。SpringBoot の使用方法を学びたい場合は、この記事をよく読んで SpringBoot の基礎知識を理解してください。
スプリングブートとは何ですか?
SpringBootはSpringから派生した一種のSpring, SpringMVC+myBatis. SpringBootはSpringとSpringMVCのすべての機能を統合したものと言えます. SpringのIOC制御反転とAOPアスペクト指向プログラミングのコア技術を持ち, Springのプロセスを簡素化しますオブジェクトを作成すると、Spring のいくつかの関数コードの結合が昇華されます。
SpringBoot とは何かを理解したら、本題に入りましょう
XML と javaConfig
Springではコンテナの設定ファイルとしてxmlを使用していましたが、3.0以降ではjavaconfigが追加され、javaクラスの設定ファイルが使用されています。
1.1.1 javaConfigとは
javaConfig: Spring によって提供される純粋な Java メソッドであり、Java クラスを使用してコンテナーを構成し、Spring IOC コンテナーを構成します
。
- オブジェクト指向のアプローチを使用でき、構成クラスは構成クラスを継承し、メソッドをオーバーライドできます。
- 煩雑な 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;
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 構成コンテナ
アノテーション @Configuration は主に javaConfig によって使用されます
。クラスの先頭に配置されます。このクラスは、Bean を宣言できる XML 構成ファイルと同等です。
@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 は、xml ファイルのリソース
作成データ クラスに相当する XML 設定をインポートします。
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 は、プロパティ属性構成ファイルを読み取ります。
リソースディレクトリに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 を使い始めるための最初のエピソードは終了しました。何か間違った点があれば、お気軽にメッセージを残してください。時間内に変更します。貴重なご意見をいただければ幸いです。