1. 程式人生 > >Spring基於註解的方式一

Spring基於註解的方式一

Spring基於註解的方式一

Spring註解簡介

之前的時候我們學習的Spring都是基於Spring配置檔案的形式來編寫,現在很多的情況下使用SpringBoot的時候是基於註解的形式,這裡我們首先要了解的是Java中的註解是什麼意思。對於註解和註釋要做一定的區別。
首先我們介紹一下關於Spring的簡單的註解,主要有以下的一些常用的註解

1.@Controller 控制層物件註解
2.@Service 業務邏輯層註解
3.@Repository 資料訪問層註解
4.@Configuration 配置類註解
5.@ComponentScan 配置掃描類註解
6.@Filter

過濾器註解
7.@Bean 容器註解
8.@Import 匯入外部類
9.@Scope 指定建立物件方式註解
10.@Conditional 按照條件建立註解
當然還有很多的很多的其他的註解,這裡我們介紹的最簡單的起步需要學習的註解就有這些,通過這些註解的學習我們對於很多的Spring原理級別的知識做一個瞭解,在我們使用Spring配置檔案的時候只是對於怎麼樣解析配置檔案來做一個瞭解並沒有對主要的原理級別的知識做總結,現在我們就通過簡單的基於Spring註解的方式來對Spring原理級別的內容做解釋。
首先我們提供一段程式碼

/**
 * 配置類就是類似於配置檔案
 */

@Configuration
//標記配置類 @ComponentScan(value = "com.example" ,includeFilters = { // @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {PersonService.class}), // @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}), @ComponentScan.Filter(type = FilterType.
CUSTOM,classes = MyTypeFilter.class) },useDefaultFilters = false) /** * excludeFilters Filter[] 按照規則排除一些元件 * * includeFilters Filter[] 按照規則包含一些元素 * * FilterType.ANNOTATION 按照註解的方式掃描 * FilterType.ASSIGNABLE_TYPE 按照給定的型別 * FilterType.ASPECTJ 使用ASPECTJ表示式,基本上不太常用 * FilterType.REGEX 使用正則表示式的方式 * FilterType.CUSTOM 使用自定義規則 */ public class MainConfig { //給容器中註冊一個Bean,型別為返回值的型別,ID預設使用方法名作為ID @Bean(value = "person01") public Person person(){ return new Person("lisi",23); } }

這個是一個簡單的註解類,其中 @Configuration 註解標註這個類為我們提供的一個註解類而在我們使用配置檔案配置的時候提供的很多的基於配置檔案的東西,在基於註解配置的時候都有所體現。

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

    <!--只要標註了@controller @Service @ [email protected] Component-->
    <context:component-scan base-package="com.example" use-default-filters="false"></context:component-scan>
    <bean id="person" class="com.example.bean.Person" scope="" init-method="" destroy-method="" >
        <property name="name" value="nihui"></property>
        <property name="age" value="123"></property>
    </bean>
</beans>

比如所在我們配置檔案中提供的 ComponentScan 這個是一個基於配置檔案掃描的註解,這個註解配置我們指定的掃描的配置檔案的包名,這裡我們提供掃描了配置檔案的中的所有包

在這裡插入圖片描述

這裡我們提供了專案工程目錄,在這個目錄中我們可以看到很多的基於配置檔案的類都在其中做了體現。有我們dao層的物件,Controller層物件,Service層的物件,對應我們上面提供的註解,在我們配置類中提供包掃描的方式來將我們配置的類檔案包含到容器中。這裡這裡我們看一下我們怎麼使用這些註解配置類

 //配置檔案測試
    private static void test01() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person.getName() + "+++++" + person.getAge());
    }

這個是我們基於配置檔案的形式來寫的獲取JavaBean的方法

 //配置類
    private static void test02() {
        ApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = annotationConfigApplicationContext.getBean(Person.class);
        System.out.println(person);
        String[] namesForType = annotationConfigApplicationContext.getBeanNamesForType(Person.class);
        for (String str:namesForType) {
            System.out.println(str);
        }
    }

這個是我們基於配置類的獲取容器中的配置檔案的方式,從這裡我們可以看出提供不同的配置方式獲取到的方式是不一樣的,在我們配置檔案中我們關注的重點是怎麼通過配置檔案將配置資訊解析出來然後通過容器將這些java元件管理起來。而配置類則更多的是提供我們怎麼樣通過簡單的程式碼的方式解析這些java元件。但是兩者的底層實現的原理是一樣的從不同的方式中實現了IOC(控制反轉)和DI(依賴注入)。這個是我提供的第一次的關於Spring基於註解的方式來配置。