Spring——属性注入のための Bean 管理アノテーション メソッド

Bean管理でオブジェクトを作成するためにSpringが提供する注釈は何ですか?

@コンポーネント: 通常

@Service: ビジネスロジックレイヤー

@Controller:コントローラ層

@リポジトリ: dao レイヤー

注釈を使用する理由

xml モードでの開発を簡素化し、構成ファイルの構成を完了するために注釈のみが必要です

アノテーション開発を実現するには?

オブジェクトを作成

コンポーネントスキャン

package com.atguigu.spring5.ComponentScan;

import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5
 * @Author: dengLiMei
 * @CreateTime: 2023-02-04  16:56
 * @Description: TODO
 * @Version: 1.0
 */
//value可以不写,如果不写默认是类名的首字母小写
@Component(value = "user")
public class User {
    public void add() {
        System.out.println("aaaa");
    }
}
package com.atguigu.spring5.ComponentScan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.ComponentScan
 * @Author: dengLiMei
 * @CreateTime: 2023-02-10  15:12
 * @Description: TODO
 * @Version: 1.0
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext content= new ClassPathXmlApplicationContext("beans.xml");
        User user = content.getBean("user", User.class);
        user.add();
    }
}
<?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:p="http://www.springframework.org/schema/p"
       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 http://www.springframework.org/schema/context/spring-context.xsd">


    <!--    开启组件扫描
    如果扫描多个包,多个包使用逗号隔开
    扫描包上层目录-->
    <!--   表示: 扫描包中的所有类-->
    <context:component-scan base-package="com.atguigu.spring5">
    </context:component-scan>

    <!--    use-default-filters=“false”:表示现在不使用默认filter,自己配置filter
    context:include-filter,设置扫描哪些内容-->
    <!--    表示:  只扫描注解带Controller的类-->
    <context:component-scan base-package="com.atguigu.spring5" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--    下面配置扫描包所有内容
    context:include-filter,设置扫描哪些内容-->
    <!--    表示:  除了Controller其余类都扫描-->
    <context:component-scan base-package="com.atguigu.spring5">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-filter
        type="" expression=""/>
    </context:component-scan>
</beans>

属性注入

@Autowired: タイプに応じて注入

@Qualifier: 名前による注入、および Autowired

@Resource: タイプまたは名前に応じて注入できます

@Value: 通常の属性注入

javax: Java 拡張パッケージ


完全に注釈付きの開発

構成: 構成クラスとして、xml 構成ファイルを置き換えます

package com.atguigu.spring5.SpringConfiguration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(basePackages = {"com.atguigu.spring5"})
public class SpringConfigure {

}
package com.atguigu.spring5.ScopeTest;

import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.Book
 * @Author: dengLiMei
 * @CreateTime: 2023-02-08  19:28
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public static void main(String[] args) {
        Book book = new Book();
        book.setBname("abc");
    }

    public void testDemo() {
        System.out.println(bname + "---" + "aaa");
    }

}
package com.atguigu.spring5.SpringConfiguration;

import com.atguigu.spring5.ScopeTest.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.SpringConfiguration
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  09:38
 * @Description: TODO
 * @Version: 1.0
 */
public class Main {
    public static void main(String[] args) {
//        加载配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigure.class);
//        创建实例
        Book book = context.getBean("book", Book.class);
//        调用方法
        book.testDemo();
    }
}

出力結果:


春の連載記事:

春 - それは何ですか?効果?コンテンツ?デザインパターン使用?

Spring——属性注入のための Bean 管理 xml メソッド

Spring——属性注入のための Bean 管理アノテーション メソッド

春 - IOC とは

Spring - AOP とは? 使い方は?

春 - トランザクションとは? コミュニケーション行為?トランザクション分離レベルとは何ですか?

Spring - junit4、junit5 の使用法を統合する

コンテンツを交換したい場合は、コメント欄にメッセージを残してください. このドキュメントが気に入ったら、いいね+コレクションの足跡を残してブロガーをサポートしてください~

おすすめ

転載: blog.csdn.net/weixin_43319713/article/details/129338389