conchincradle / issue-tracking-system

0 stars 0 forks source link

spring framework #1

Open conchincradle opened 1 year ago

conchincradle commented 1 year ago

Inversion of Control and Dependency injection

Spring Framework(単にSpringとも呼ばれます)は、JavaのWEBフレームワークで、WEBアプリを作ることができます。

Spring Frameworkでは、DI(Dependency Injection)と呼ばれる仕組みが導入されています。日本語でいうと、「依存性の注入」です。

Javaを使ったアプリ開発は大規模なものが多く、途中で修正や変更などが入ることも多いです。以前までは変更に弱く、変更が入るたびかなりの時間を費やす必要がありました。

しかし、依存性のあるプログラムは外部から取り入れるようになっているので、1つ1つのプログラムを独立させやすくなります。

そのため、変更する場合も最低限の変更で済むので開発が楽になるのです。

「Spring MVC Test」という専用のテストプログラムを使うことができます。

これは、1つ1つの処理がうまくいっているかをテスト(単体テストと呼ばれます)するのではなく、無事に一連の処理を終えるかどうかをテスト(結合テストと呼ばれます)することができます。

それによって、バグの混入を最小限におさえ、安全に開発をすすめることができるようになりました。

[Spring Framework Documentation](https://docs.spring.io/spring-framework/docs/current/reference/html/)

Untitled

Spring MVC 是Spring 提供的一个基于MVC 设计模式的轻量级Web 开发框架,本质上相当于Servlet。 Spring MVC 是结构最清晰的Servlet+JSP+JavaBean 的实现,是一个典型的教科书式的MVC 构架

MVC とはアプリケーションフレームワークのひとつです。

これはModel(モデル)View(ビュー)Controller(コントローラ)の頭文字を取ったもので、全体の処理をこの3つに分割します。

Model は処理の中核を担います。

アプリケーションが使用するデータや、DBに対する処理などは Model が担当することになります。

View はクライアントからの入力やクライアントへの出力を担います。

第15回から第17回まで解説した Thymeleaf などは View が担当することになります。

Controller は上記2つの橋渡しを担います。クライアントからの入力を View から受け取り、それをもとに Model に指示を伝えます。その後、Model から受け取った値を View に伝え、見た目に反映させます。

以上のように、MVC ではプログラム中の処理を分担し管理します。

上記の MVC を基に Spring MVC というフレームワークが作られました。

しかし、 Spring MVC はライブラリが膨大であることや、設定が複雑であることなどから使いこなすのが難しいという問題がありました。

そこで、この Spring MVC をより簡単に使えるように作られたフレームワークが Spring Boot です。

conchincradle commented 1 year ago

https://qiita.com/TEBASAKI/items/267c261db17f178e33eb

conchincradle commented 1 year ago

MVC とはアプリケーションフレームワークのひとつです。

これはModel(モデル)View(ビュー)Controller(コントローラ)

の頭文字を取ったもので、全体の処理をこの3つに分割します。

上記の MVC を基に Spring MVC というフレームワークが作られました。

しかし、 Spring MVC はライブラリが膨大であることや、設定が複雑であることなどから使いこなすのが難しいという問題がありました。

そこで、この Spring MVC をより簡単に使えるように作られたフレームワークが Spring Boot です。

conchincradle commented 1 year ago

@RestControllerでは View に遷移せず、メソッドの戻り値がそのままレスポンスのコンテンツになります。 @Controllerでは後述する@ResponseBodyというアノテーションを使用することで、同様の処理ができますが、 @RestControllerは@ResponseBodyを記述する必要はありません。通常は@Controllerを使いますが、JSON や XML などを返す WebAPI 用の Controller などには@RestControllerを使います。

What is XML file in HTML? XML stands for eXtensible Markup Language. XML is a markup language much like HTML. XML was designed to store and transport data. JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page.

conchincradle commented 1 year ago

@GetMapping 组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写 @RequestMappingというアノテーションを記述することで、 クライアントからのリクエストに対してメソッドやハンドラをマッピングします。 The @GetMapping annotation is a specialized version of @RequestMapping annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET). The @GetMapping annotated methods in the @Controller annotated classes handle the HTTP GET requests matched with given URI expression. The GET Method GET is used to request data from a specified resource.

Note that the query string (name/value pairs) is sent in the URL of a GET request: The POST Method POST is used to send data to a server to create/update a resource.

The data sent to the server with POST is stored in the request body of the HTTP request:

conchincradle commented 1 year ago

Integrated Development Environment IDE + IntelliJ 70%

Ctrl ~ N ——Navigate

Webアプリケーションを実装するとは:

リクエストとControllerを紐付ける

Controllerでレスポンスを生成する

SpringBoot Java

Spring Initilizar で Gradle Project Java Spring Boot 2,7,0 Packaging Jar

Jar —Java Archive without html etc but war has

Java 11

Dependencies:

Spring Web—Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.

conchincradle commented 1 year ago

IndexController: annotation /@Controller , GetMapping ResponseBody(reture index.html 就不需要这个注释了,除非返回文字)

Build.gradle ——dependecies , add Thymeleaf denpendecies from spring initilizr

resources templates add html file

是自己电脑的一个端口,localhost 是本地计算机的默认WEB发布路径,8080是端口号。

如果你访问这个URL,就可以看到你本机上WEB服务器发布的内容。

Spring Dev Tools—-automatic restart (for Java source code change)

Hot Swapping (for template change)——-HTML の変更が自動的に反映されます

Lombok:AllArgsConstructor this id Data : Getter and Setter

RequiredArgsConstructor

conchincradle commented 1 year ago

代码重构 (Code refactoring) [https://zh.wikipedia.org/zh-cn/代码重构](https://zh.wikipedia.org/zh-cn/%E4%BB%A3%E7%A0%81%E9%87%8D%E6%9E%84)

annotaion

学到了什么主要是写

alt + 7——Structure

三層アーキテクチャ

Webアプリケーションのコードを三つの関心事に分類して実装する

プレゼンテーション層 画面からの入力、画面への出力 /@Controller View

ビジネスロジック層 業務ロジック、業務ルール /Service  Controller

データアクセス層 データの保存、取得 @Repositptory  モデル

In MVC : MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model

In Three Tier : A three tier architecture is the client tier never communicates directly with the data tier In a Three-tier model all communication must pass through the middle tier

使いたいオブジェクトを直接newはせず外部から渡してもらうようにする

クラスA がクラスBに依存しているー>クラスA の中でクラスBを使っている

conchincradle commented 1 year ago
DI Dependency Injection 依存性の注入
public class A{ 
    private final B b;
        public A (B b){ // コンストラクタを呼び出す側でnewしてもらう
            this.b = b;
        }
}

before:
private final B b = new B() // new している
conchincradle commented 1 year ago

依赖注入(Dependency of Injection)和控制反转(Inversion of Control 简称:ioc)是一个概念。

当某个角色(java实例class A,调用者)需要另一个角色(另一个Java实例class B,被调用者)的协助时,在传统的程序设计过程中通常由调用者来创建被调用者的实例(在class A里面 初始化class B)。但在Spring里,创建被调用者实例的工作不再由调用者来完成。因此成为控制反转(ioc)。创建被调用者实例的工作由Spring容器来完成,然后注入调用者。因此也称为依赖注入

不管是依赖注入还是控制反转,都说明Spring采用动态灵活的方式来管理各种对象,对象与对象之间的具体实现互相透明。

Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the applicatio

Two ways to perform Dependency Injection in Spring framework

Spring framework provides two ways to inject dependency

DI:Instantiate the dependency of a client class outside of the class and then inject the dependency into the class

provide loose coupling

Modularity/Separation of Concerns

Easier Unit Testing of the Dependencies

Easy swapability of dependency implementations

Inversion of Control:

Give control of dependency and object instantiation to an outside system

conchincradle commented 1 year ago

疎結合になる

密結合のnewは依頼しているクラスB内部で使うオブジェクトについて知っていないといけない

メリット:拡張性がある:クラスA は依頼しているクラスBの内部で使うオブジェクトが変更するとき、変更されません。

テストしやすい

conchincradle commented 1 year ago
// Spring での DI = Bean 登録 + インジェクション
@Service   // Bean 登録 
public class B{}
@Controller
public class A{

    private final B b ;
    public A(B b){ # インジェクション
        this.b = b;
    }
}
conchincradle commented 1 year ago

Bean登録をするにはーアノテーションベースの方法

Bean ≒ DI管理対象のクラスのこと @Component // 付与したクラスBeanであることを示す @Component の代わりにユースケースを表すアノテーションを使える @Controller プレゼンテーション層 @Service ビジネスロジック層 @Repository データアクセス層 @

conchincradle commented 1 year ago

Ctrl B ---return definition

Ctrl + Alt + ← → this can go back and forward

Ctrl D 复制粘贴当前的一行

GetMapping