国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Spring6】| Spring IoC注解式開(kāi)發(fā)

這篇具有很好參考價(jià)值的文章主要介紹了【Spring6】| Spring IoC注解式開(kāi)發(fā)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

目錄

一:Spring IoC注解式開(kāi)發(fā)

1.?回顧注解

2.?聲明Bean的四個(gè)注解

3.?Spring注解的使用

4.?選擇性實(shí)例化Bean

5.?負(fù)責(zé)注入的注解(重點(diǎn))

5.1 @Value

5.2?@Autowired與@Qualifier

5.3?@Resource

6.?全注解式開(kāi)發(fā)


一:Spring IoC注解式開(kāi)發(fā)

1.?回顧注解

注解的存在主要是為了簡(jiǎn)化XML的配置,Spring6倡導(dǎo)全注解開(kāi)發(fā)。

我們來(lái)回顧一下:

①第一:注解怎么定義,注解中的屬性怎么定義?

②第二:注解怎么使用?

③第三:通過(guò)反射機(jī)制怎么讀取注解?

注解怎么定義,注解中的屬性怎么定義?

自定義注解:Component

(1)Target注解和Retention注解,這兩個(gè)注解被稱(chēng)為元注解:

①@Target注解用來(lái)設(shè)置Component注解可以出現(xiàn)的位置,以下代表表示Component注解只能用在類(lèi)和接口上。

②Retention注解用來(lái)設(shè)置Component注解的保持性策略,以下代表Component注解可以被反射機(jī)制讀取。
注:使用某個(gè)注解的時(shí)候,如果注解的屬性名是value的話(huà),value可以省略。
注:使用某個(gè)注解的時(shí)候,如果注解的屬性值是數(shù)組,并且數(shù)組中只有一個(gè)元素,大括號(hào)可以省略。

package com.bjpowernode.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Component {
    // 屬性
    String value();
}

注解怎么使用?

(2)語(yǔ)法格式:@注解類(lèi)型名(屬性名=屬性值, 屬性名=屬性值, 屬性名=屬性值......)

并且如果屬性名是value,則在使用的時(shí)候可以省略屬性名,例如:

package com.bjpowernode.annotation;

// @Component(value = "userBean")
@Component("userBean")
public class User {
}

通過(guò)反射機(jī)制怎么讀取注解?

(3)定義需求:當(dāng)Bean類(lèi)上有Component注解時(shí),則實(shí)例化Bean對(duì)象,如果沒(méi)有,則不實(shí)例化對(duì)象!

第一步:先使用Class.forName獲取類(lèi)。

第二步:調(diào)用isAnnotationPresent方法看這個(gè)類(lèi)上是否有@Component注解,參數(shù)是Component.class,返回的是一個(gè)布爾類(lèi)型。

第三步:如果存在,調(diào)用getDeclaredAnnotation方法獲取這個(gè)注解,參數(shù)還是Component.class,然后就可以獲取注解的屬性值。

package com.bjpowernode.annotation;

public class Test {
    public static void main(String[] args) throws Exception{
        // 獲取類(lèi)
        Class<?> clazz = Class.forName("com.bjpowernode.annotation.User");
        // 看這個(gè)類(lèi)上有沒(méi)有Component注解
        if (clazz.isAnnotationPresent(Component.class)){
            // 有這個(gè)注解就獲取注解的屬性并就創(chuàng)建對(duì)象
            // 獲取這個(gè)注解
            Component annotation = clazz.getDeclaredAnnotation(Component.class);
            // 獲取這個(gè)注解的屬性
            System.out.println(annotation.value());
            // 創(chuàng)建對(duì)象
            // User user = new User();
            User user = (User)clazz.newInstance()
            System.out.println(user);
        }
    }
}

執(zhí)行結(jié)果:

【Spring6】| Spring IoC注解式開(kāi)發(fā)

?(4)在(3)的基礎(chǔ)上,提升一下難度;

假設(shè)我們現(xiàn)在只知道包名:com.bjpowernode.annotation;至于這個(gè)包下有多少個(gè)Bean我們不知道!哪些Bean上有注解,哪些Bean上沒(méi)有注解,這些我們都不知道!如何通過(guò)程序全自動(dòng)化掃描判斷?

注:實(shí)際上@ComponentScan注解可以實(shí)現(xiàn)這個(gè)功能,包掃描,有Componenet注解的對(duì)象就會(huì)被創(chuàng)建出來(lái),沒(méi)有的則不會(huì)!

目前只知道一個(gè)包的名字,掃描這個(gè)包下所有的類(lèi),當(dāng)這個(gè)類(lèi)上有@Component注解的時(shí)候,實(shí)例化該對(duì)象,然后放到Map集合中!

User類(lèi):含有Component注解

package com.bjpowernode.annotation;

@Component("userBean")
public class User {
}

Vip類(lèi):沒(méi)有Component注解

package com.bjpowernode.annotation;

public class Vip {
}

Student類(lèi):含有Component注解

package com.bjpowernode.annotation;

@Component("studentBean")
public class Student {
}

?ComponentScan類(lèi):編寫(xiě)測(cè)試

package com.bjpowernode.annotation;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ComponentScan {
    public static void main(String[] args) {

        // 創(chuàng)建Map集合
        Map<String,Object> beanMap = new HashMap<>();

        // 目前只知道一個(gè)包名
        String packageName = "com.bjpowernode.annotation";
        // 把包名轉(zhuǎn)換成路徑的形式,使用正則表達(dá)式
        // 直接使用“.”,正則表達(dá)式中代表任意字符,會(huì)把所有的內(nèi)容都替換成/
        // 正則表達(dá)式中“\.”代表“.” ,而java中雙\\就代表\,有一個(gè)用來(lái)轉(zhuǎn)義
        String packagePath = packageName.replaceAll("\\.", "/");
        // String packagePath = packageName.replace(".", "/"); 這樣也可以
        // System.out.println(packagePath); // com/bjpowernode/annotation
        // com是類(lèi)的根路徑下的一個(gè)目錄,獲取系統(tǒng)類(lèi)加載器,從根目錄下進(jìn)行掃描,返回一個(gè)URL
        URL url = ClassLoader.getSystemClassLoader().getResource(packagePath);
        // 獲取絕對(duì)路徑
        String path = url.getPath();
        // 開(kāi)始掃描,獲取一個(gè)絕對(duì)路徑下的所有文件
        File file = new File(path);
        File[] files = file.listFiles();
        // 流式編程
        Arrays.stream(files).forEach(f -> { // f.getName得到的是對(duì)應(yīng)的.class字節(jié)碼文件
            // 拼串成全限定包名
            String className = packageName+"."+f.getName().split("\\.")[0];
            // System.out.println(className);
            try {
                // 通過(guò)反射機(jī)制創(chuàng)建對(duì)象
                // 獲取類(lèi)
                Class<?> clazz = Class.forName(className);
                // 檢查類(lèi)上有沒(méi)有Component注解
                if (clazz.isAnnotationPresent(Component.class)) {
                    // 有的話(huà)獲取注解
                    Component annotation = clazz.getDeclaredAnnotation(Component.class);
                    // 獲取注解的屬性值
                    String key = annotation.value();
                    // 創(chuàng)建對(duì)象
                    Object obj = clazz.newInstance();
                    // 把注解的屬性值作為key,對(duì)象作為value放到Map集合當(dāng)中
                    beanMap.put(key,obj);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // 打印這個(gè)Map集合
        System.out.println(beanMap);


    }
}

執(zhí)行結(jié)果:應(yīng)該創(chuàng)建User和Student對(duì)象

【Spring6】| Spring IoC注解式開(kāi)發(fā)

2.?聲明Bean的四個(gè)注解

負(fù)責(zé)聲明Bean的注解(實(shí)例化),常見(jiàn)的包括四個(gè):

①@Component注解:平常使用

②@Controller注解:一般控制器上使用

③@Service注解:一般service類(lèi)上使用

④@Repositorys注解:一般dao類(lèi)上使用

源碼如下:

@Component注解

package com.powernode.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Component {
    String value();
}

@Controller注解

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

@Service注解

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

@Repository注解

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

(1)通過(guò)源碼可以看到,@Controller、@Service、@Repository這三個(gè)注解都是@Component注解的別名。也就是說(shuō):這四個(gè)注解的功能都一樣,用哪個(gè)都可以!

(2)那么為什么會(huì)提供那么多種方式呢?只是為了增強(qiáng)程序的可讀性,建議:

①控制器類(lèi)上使用:Controller注解

②service類(lèi)上使用:Service注解

③dao類(lèi)上使用:Repository注解

(3)都是只有一個(gè)value屬性,value屬性用來(lái)指定bean的id,也就是bean的名字!

【Spring6】| Spring IoC注解式開(kāi)發(fā)

3.?Spring注解的使用

如何使用以上的注解呢?

第一步:加入aop的依賴(lài)(不用管);

第二步:在配置文件中添加context命名空間;

第三步:在配置文件中指定掃描的包;

第四步:在Bean類(lèi)上使用注解;

第一步:加入aop的依賴(lài)

我們可以看到當(dāng)加入spring-context依賴(lài)之后,會(huì)關(guān)聯(lián)加入aop的依賴(lài),所以這一步不用做

【Spring6】| Spring IoC注解式開(kāi)發(fā)

第二步:在配置文件中添加context命名空間

<?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">

</beans>

第三步:在配置文件中指定要掃描的包?

(1)就像我們前面回憶反射機(jī)制第四部分的代碼一樣,我們只需要知道包名,然后會(huì)掃描這個(gè)包下的所有類(lèi),通過(guò)反射機(jī)制,對(duì)含有注解的進(jìn)行對(duì)象的創(chuàng)建!相當(dāng)于我們只需要配置包名,就可以通過(guò)注解創(chuàng)建對(duì)象,不用一個(gè)個(gè)去進(jìn)行配置了!

(2)注:就是使用context命名空間的component-scan進(jìn)行組件掃描,使用屬性base-package執(zhí)行要掃描的包!

<?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">
    <!--給Spring框架指定要掃描哪些包中的類(lèi)-->
    <context:component-scan base-package="com.bjpowernode.spring.bean"/>
</beans>

第四步:在Bean類(lèi)上使用注解

User類(lèi):含有Component注解

package com.bjpowernode.spring.bean;

import org.springframework.stereotype.Component;

@Component("userBean")
public class User {
    
}

Vip類(lèi):含有Component注解

package com.bjpowernode.spring.bean;

import org.springframework.stereotype.Component;

@Component("vipBean")
public class Vip {

}

編寫(xiě)測(cè)試程序

package com.bjpowernode.spring.test;

import com.bjpowernode.spring.bean.User;
import com.bjpowernode.spring.bean.Vip;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springAnnotationTest {
    @Test
    public void testAnnotation(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User userBean = applicationContext.getBean("userBean", User.class);
        System.out.println(userBean);
        Vip vipBean = applicationContext.getBean("vipBean", Vip.class);
        System.out.println(vipBean);

    }
}

執(zhí)行結(jié)果:成功創(chuàng)建兩個(gè)對(duì)象

【Spring6】| Spring IoC注解式開(kāi)發(fā)

問(wèn)題1:如果把value屬性徹底去掉,spring會(huì)被Bean自動(dòng)取名嗎?

答:會(huì)的,并且默認(rèn)名字是:Bean類(lèi)名首字母小寫(xiě);例如:Vip類(lèi)對(duì)應(yīng)的名字就是vip

package com.bjpowernode.spring.bean;

import org.springframework.stereotype.Component;

// 自動(dòng)取名就是vip
@Component
public class Vip {

}

問(wèn)題2:如果是多個(gè)包怎么辦?有兩種解決方案:

①第一種:在配置文件中指定多個(gè)包,用 “逗號(hào),” 隔開(kāi)。

②第二種:指定多個(gè)包的共同父包,犧牲一部分效率。

<?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">
    <!--多個(gè)包的解決方案-->
    <!--第一種:使用逗號(hào)隔開(kāi)。-->
    <context:component-scan base-package="com.powernode.spring6.bean,com.powernode.spring6.dao"/>
    <!--第二種:也可以指定這多個(gè)包共同的父包,但是這肯定要犧牲一部分效率-->
    <context:component-scan base-package="com.powernode.spring6"/>
</beans>

4.?選擇性實(shí)例化Bean

需求:假設(shè)在某個(gè)包下有很多Bean,有的Bean上標(biāo)注了@Component,有的標(biāo)注了@Controller,有的標(biāo)注了@Service,有的標(biāo)注了@Repository,現(xiàn)在由于某種特殊業(yè)務(wù)的需要,只允許其中所有的Controller注解的參與Bean管理,其他的都不實(shí)例化!這應(yīng)該怎么辦呢?

為了便于理解,我們將這幾個(gè)類(lèi)都定義到同一個(gè)java源文件中,并且采用不同的注解方式!

package com.bjpowernode.spring.bean;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Component
public class A {
    public A() {
        System.out.println("A的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
    }
}

@Controller
class B {
    public B() {
        System.out.println("B的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
    }
}

@Service
class C {
    public C() {
        System.out.println("C的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
    }
}

@Repository
class D {
    public D() {
        System.out.println("D的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
    }
}

@Controller
class E {
    public E() {
        System.out.println("E的無(wú)參數(shù)構(gòu)造方法執(zhí)行");
    }
}

第一種解決方案:use-default-filters="false"

如果這個(gè)屬性是false,表com.bjpowernode.spring.bean包下所有的帶有聲明Bean的注解全部失效;然后在使用include-filter標(biāo)簽包含讓那個(gè)注解生效(使用全限定類(lèi)名)

<?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">
    <!--讓所有注解都失效-->
    <context:component-scan base-package="com.bjpowernode.spring.bean" use-default-filters="false">
        <!--讓@Controller注解生效-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
       <!--可配置多個(gè)
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
    </context:component-scan>
</beans>

測(cè)試程序

當(dāng)ClassPathXmlApplicationContext方法創(chuàng)建出來(lái),類(lèi)中對(duì)應(yīng)的構(gòu)造方法就會(huì)執(zhí)行!

package com.bjpowernode.spring.test;

import com.bjpowernode.spring.bean.User;
import com.bjpowernode.spring.bean.Vip;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springAnnotationTest {
    @Test
    public void testChoose(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-choose.xml");
    }
}

執(zhí)行結(jié)果:只有Controller注解生效,對(duì)應(yīng)的應(yīng)該是B和E

【Spring6】| Spring IoC注解式開(kāi)發(fā)

第二種解決方案:use-default-filters="true"

如果這個(gè)屬性的值是true,表示com.bjpowernode.spring.bean下的所有的帶有聲明Bean的注解全部生效;use-default-filters="true" 默認(rèn)值就是true,不用寫(xiě)也行。然后在使用exclude-filter標(biāo)簽去除讓那個(gè)注解失效(使用全限定類(lèi)名)

<?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">


    <context:component-scan base-package="com.bjpowernode.spring.bean" use-default-filters="true">
        <!--讓@Service、@Repository失效-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
</beans>

執(zhí)行結(jié)果:只有Service和Repository注解失效,對(duì)應(yīng)的應(yīng)該是A、B、E

【Spring6】| Spring IoC注解式開(kāi)發(fā)

5.?負(fù)責(zé)注入的注解(重點(diǎn))

(1)@Component @Controller @Service @Repository這四個(gè)注解是用來(lái)聲明Bean的,聲明后這些Bean將被實(shí)例化。

(2)接下來(lái)我們看一下,如何給Bean的屬性賦值,給Bean屬性賦值需要用到這些注解:

@Value、@Autowired、@Qualifier、@Resource。

5.1 @Value

當(dāng)屬性的類(lèi)型是簡(jiǎn)單類(lèi)型時(shí),可以使用@Value注解進(jìn)行注入。

第一種情況:什么都不提供,直接在屬性上使用

package com.bjpowernode.spring.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Person {
    @Value("張三")
    private String name;
    @Value("18")
    private int age;
    
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

第二種情況:提供set方法,在set方法上使用

package com.bjpowernode.spring.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Person {
    
    private String name;
    private int age;

    // 提供了set方法
    @Value("張三")
    public void setName(String name) {
        this.name = name;
    }
    @Value("18")
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

第三種情況:提供構(gòu)造方法,在構(gòu)造方法的形參上使用

注:直接聲明構(gòu)造方法上不可以,一定是聲明在在構(gòu)造方法的參數(shù)上才可以!

package com.bjpowernode.spring.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Person {
 
    private String name;
    private int age;
    
    // 提供了構(gòu)造方法的形參上
    public Person(@Value("張三")String name, @Value("18")int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

spring-annotation-di.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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--指定要掃描的包-->
    <context:component-scan base-package="com.bjpowernode.spring.bean"/>

</beans>

執(zhí)行結(jié)果

通過(guò)測(cè)試得知:@Value注解:可以出現(xiàn)在屬性上、setter方法上、以及構(gòu)造方法的形參上,可見(jiàn)Spring給我們提供了多樣化的注入;很靈活了!

【Spring6】| Spring IoC注解式開(kāi)發(fā)

5.2?@Autowired與@Qualifier

@Autowired注解可以用來(lái)注入非簡(jiǎn)單類(lèi)型。被翻譯為:自動(dòng)連線的,或者自動(dòng)裝配。

單獨(dú)使用@Autowired注解,默認(rèn)根據(jù)類(lèi)型裝配?!?strong>默認(rèn)是根據(jù)byType】

@Autowired注解和@Qualifier注解聯(lián)合使用,才能根據(jù)名字裝配根據(jù)byName

@Autowired源碼

源碼中有兩處需要注意:

①第一處:該注解可以標(biāo)注在哪里?構(gòu)造方法上、方法上、形參上、屬性上、注解上

②第二處:該注解有一個(gè)required屬性,默認(rèn)值是true,表示在注入的時(shí)候要求被注入的Bean必須是存在,如果不存在則報(bào)錯(cuò)。如果required屬性值設(shè)置為false,表示被注入的Bean存在或者不存在都沒(méi)關(guān)系,存在的話(huà)就注入,不存在的話(huà),也不報(bào)錯(cuò)!

package org.springframework.beans.factory.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
	boolean required() default true;
}

第一種情況:什么都不提供,直接在屬性上注入

UserDao接口

package com.bjpowernode.spring.dao;

public interface UserDao {
    void insert();
}

接口的實(shí)現(xiàn)類(lèi)UserDaoImpl

package com.bjpowernode.spring.dao.impl;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {
    @Override
    public void insert() {
        System.out.println("正在插入信息");
    }
}

UserService類(lèi):在屬性上直接注入

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    // 面向接口編程
    @Autowired // 不需要指定任何屬性,直接使用即可
    private UserDao userDao;

    public void insertDate(){
        userDao.insert();
    }
}

spring-annotation-di.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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置兩個(gè)包下的公共子包-->
    <context:component-scan base-package="com.bjpowernode.spring"/>
</beans>

測(cè)試程序

package com.bjpowernode.spring.test;

import com.bjpowernode.spring.bean.Person;
import com.bjpowernode.spring.bean.User;
import com.bjpowernode.spring.bean.Vip;
import com.bjpowernode.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springAnnotationTest {

    @Test
    public void testAnnotationDI(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-annotation-di.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        userService.insertDate();
    }
}

執(zhí)行結(jié)果:構(gòu)造方法和setter方法都沒(méi)有提供,經(jīng)過(guò)測(cè)試,仍然可以注入成功

【Spring6】| Spring IoC注解式開(kāi)發(fā)

問(wèn)題:目前UserDao下只有一個(gè)實(shí)現(xiàn)類(lèi),可根據(jù)類(lèi)型完成自動(dòng)裝配;那如果在有多個(gè)實(shí)現(xiàn)類(lèi),那么在private UserDao userDao面向接口編程,Spring就不知道裝配哪一個(gè)實(shí)現(xiàn)類(lèi)?

答:此時(shí)就要使用@Autowired注解和@Qualifier注解聯(lián)合使用,根據(jù)名字進(jìn)行轉(zhuǎn)配!

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    // 不需要指定任何屬性,直接使用即可
    @Autowired 
    // 要自動(dòng)裝配那個(gè)實(shí)現(xiàn)類(lèi),就把實(shí)現(xiàn)類(lèi)的名字寫(xiě)上
    @Qualifier("userDaoImpl")
    private UserDao userDao;

    public void insertDate(){
        userDao.insert();
    }
}

第二種情況:出現(xiàn)在setter方法上

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    
    private UserDao userDao;
    // 出現(xiàn)在set方法上
    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

第三種情況:出現(xiàn)在構(gòu)造方法上

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    
    private UserDao userDao;

    // 出現(xiàn)在構(gòu)造方法上
    @Autowired
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

第四種情況:出現(xiàn)在構(gòu)造方法的屬性上

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    
    private UserDao userDao;

    // 出現(xiàn)在構(gòu)造方法的屬性上
    public UserService(@Autowired UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

第五種情況:直接省略不寫(xiě)(前提要有構(gòu)造方法)

注:如果一個(gè)類(lèi)中構(gòu)造方法只有一個(gè),并且構(gòu)造方法上的參數(shù)和屬性能夠?qū)?yīng)上;此時(shí)@Autowired注解能夠省略!

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    
    private UserDao userDao;

    // 一個(gè)構(gòu)造方法并且參數(shù)能和上面的屬性對(duì)應(yīng)上,直接可以省略不寫(xiě)
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

總結(jié):

①@Autowired注解可以出現(xiàn)在:屬性上、構(gòu)造方法上、構(gòu)造方法的參數(shù)上、setter方法上。

②當(dāng)帶參數(shù)的構(gòu)造方法只有一個(gè),@Autowired注解可以省略。

③@Autowired注解默認(rèn)根據(jù)類(lèi)型注入;如果要根據(jù)名稱(chēng)注入的話(huà),需要配合@Qualifier注解一起使用。

5.3?@Resource

(1)@Resource注解也可以完成非簡(jiǎn)單類(lèi)型注入,那它和@Autowired注解有什么區(qū)別?

@Resource注解是JDK擴(kuò)展包中的,也就是說(shuō)屬于JDK的一部分;所以該注解是標(biāo)準(zhǔn)注解,更加具有通用性。(JSR-250標(biāo)準(zhǔn)中制定的注解類(lèi)型,JSR是Java規(guī)范提案)

@Autowired注解是Spring框架自己的。

@Resource注解默認(rèn)根據(jù)名稱(chēng)裝配byName;未指定name時(shí),使用屬性名作為name;

通過(guò)name找不到的話(huà)會(huì)自動(dòng)啟動(dòng)通過(guò)類(lèi)型byType裝配。

④@Autowired注解默認(rèn)根據(jù)類(lèi)型裝配byType,如果想根據(jù)名稱(chēng)裝配,需要配合@Qualifier注解一起用。

@Resource注解主要用在屬性上、setter方法上。

@Autowired注解用在屬性上、setter方法上、構(gòu)造方法上、構(gòu)造方法參數(shù)上

(2)@Resource注解屬于JDK擴(kuò)展包,所以不在JDK當(dāng)中,需要額外引入以下依賴(lài):【如果是JDK8的話(huà)不需要額外引入依賴(lài),高于JDK11或低于JDK8需要引入以下依賴(lài)

注意:如果用Spring6,要知道Spring6不再支持JavaEE,它支持的是JakartaEE9!

Spring6+版本引入的依賴(lài)

<dependency>
  <groupId>jakarta.annotation</groupId>
  <artifactId>jakarta.annotation-api</artifactId>
  <version>2.1.1</version>
</dependency>

spring5-版本引入的依賴(lài)

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
</dependency>

@Resource注解的源碼如下

雖然可以用在類(lèi)上,但是主要是用在屬性上和方法上!

【Spring6】| Spring IoC注解式開(kāi)發(fā)

第一種情況:出現(xiàn)在屬性上,使用時(shí)必須加上name屬性(根據(jù)名字裝配)

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {

    // 出現(xiàn)在屬性上,必須加上name屬性
    @Resource(name = "userDaoImpl")
    private UserDao userDao;
    
    public void insertDate() {
        userDao.insert();
    }
}

第二種情況:出現(xiàn)在set方法上,使用時(shí)必須加上name屬性(根據(jù)名字裝配);不能出現(xiàn)在構(gòu)造方法上

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
   
    private UserDao userDao;
    // 出現(xiàn)在set方法上,必須加上name屬性
    @Resource(name = "userDaoImpl")
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

第三種情況:不指定名字,還是根據(jù)name自動(dòng)裝配,把屬性作為name(根據(jù)名字裝配)

UserDaoImpl類(lèi),名字修改為屬性名

package com.bjpowernode.spring.dao.impl;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

// 這里也改成屬性的名字
// @Repository("userDaoImpl")
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void insert() {
        System.out.println("正在插入信息");
    }
}

UserService類(lèi):使用Resource注解,不指定名字,默認(rèn)把屬性名作為名字

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    // 這里未指定名字,會(huì)把屬性名作為名字
    @Resource
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

執(zhí)行結(jié)果:

通過(guò)測(cè)試得知,當(dāng)@Resource注解使用時(shí)沒(méi)有指定name的時(shí)候,還是根據(jù)name進(jìn)行查找,這個(gè)name是屬性名。

【Spring6】| Spring IoC注解式開(kāi)發(fā)

第四種情況:當(dāng)使用屬性名字裝配時(shí),再找不到;才會(huì)按照類(lèi)型進(jìn)行裝配(根據(jù)類(lèi)型裝配)

UserDaoImpl類(lèi),名字修改為不是屬性名

package com.bjpowernode.spring.dao.impl;

import com.bjpowernode.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

// 名字不是屬性名
@Repository("xxx")
public class UserDaoImpl implements UserDao {
    @Override
    public void insert() {
        System.out.println("正在插入信息");
    }
}

UserService類(lèi):使用Resource注解,不指定名字,默認(rèn)把屬性名作為名字,但是與上面名字不匹配,此時(shí)也能執(zhí)行成功;此時(shí)不是根據(jù)名字進(jìn)行裝配,而是根據(jù)類(lèi)型進(jìn)行裝配了!

package com.bjpowernode.spring.service;

import com.bjpowernode.spring.dao.UserDao;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
    // 這里未指定名字,會(huì)把屬性名作為名字
    @Resource
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void insertDate() {
        userDao.insert();
    }
}

總結(jié):

①自己指定名字,@Resource注解默認(rèn)根據(jù)名稱(chēng)【byName】注入,只要UserDaoImpl類(lèi)的@Repository("userDaoImpl")注解的名稱(chēng)與UserService類(lèi)里面的userDao屬性上面的@Resource(“userDaoImpl”)注解名稱(chēng)保持一致即可!【byName】

②當(dāng)不指定名字時(shí),直接使用@Resource注解,此時(shí)是根據(jù)屬性u(píng)serDao的名字進(jìn)行注入;需要保證UserDaoImpl類(lèi)的@Repository("userDao")注解的名稱(chēng)就是userDao屬性的名字!【byName】

③如果②中UserDaoImpl類(lèi)的@Repository("userXxx")注解的名稱(chēng)與屬性u(píng)serDao的名字沒(méi)有保持一致,根據(jù)屬性名找不到時(shí),才會(huì)根據(jù)類(lèi)型進(jìn)行注入!【byType】

④byType注入時(shí),某種類(lèi)型的Bean只能有一個(gè)(實(shí)現(xiàn)類(lèi)有多個(gè)就不知道注入哪一個(gè)了),此時(shí)@Resource注解也可以省略不寫(xiě)!

6.?全注解式開(kāi)發(fā)

所謂的全注解開(kāi)發(fā)就是不再使用spring配置文件了;寫(xiě)一個(gè)配置類(lèi)來(lái)代替配置文件!

第一步:配置類(lèi)代替spring配置文件

主要使用到兩個(gè)注解:

①@Configuration:就相當(dāng)于xml文件中的基礎(chǔ)配置;

②@ComponentScan:包掃描,用來(lái)指定包名;

package com.bjpowernode.spring.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.bjpowernode.spring")
public class Spring6Configuration {
    // 類(lèi)中什么都不用寫(xiě)
}

第二步:編寫(xiě)測(cè)試程序

不需要在編寫(xiě)Spring.xml配置文件了,直接在測(cè)試代碼中引用上面的類(lèi)即可!

②不再new ClassPathXmlApplicationContext()對(duì)象了,而是創(chuàng)建AnnotationConfigApplicationContext對(duì)象,參數(shù)就是上面我們的配置類(lèi)Spring6Configuration對(duì)應(yīng)的.class!

package com.bjpowernode.spring.test;

import com.bjpowernode.spring.bean.Person;
import com.bjpowernode.spring.bean.User;
import com.bjpowernode.spring.bean.Vip;
import com.bjpowernode.spring.config.Spring6Configuration;
import com.bjpowernode.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springAnnotationTest {

    @Test
    public void testNoXML(){
        // 創(chuàng)建AnnotationConfigApplicationContext對(duì)象
        ApplicationContext context = new AnnotationConfigApplicationContext(Spring6Configuration.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.insertDate();
    }
}

執(zhí)行結(jié)果:全注解式開(kāi)發(fā)也沒(méi)問(wèn)題

【Spring6】| Spring IoC注解式開(kāi)發(fā)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-411561.html

到了這里,關(guān)于【Spring6】| Spring IoC注解式開(kāi)發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • spring6-實(shí)現(xiàn)簡(jiǎn)易版IOC容器

    我們都知道,Spring框架的IOC是基于Java反射機(jī)制實(shí)現(xiàn)的,下面我們先回顧一下java反射。 1、回顧Java反射 Java 反射機(jī)制是在運(yùn)行狀態(tài)中,對(duì)于任意一個(gè)類(lèi),都能夠知道這個(gè)類(lèi)的所有屬性和方法;對(duì)于任意一個(gè)對(duì)象,都能夠調(diào)用它的任意方法和屬性;這種動(dòng)態(tài)獲取信息以及動(dòng)態(tài)調(diào)

    2024年02月08日
    瀏覽(19)
  • spring6-IOC容器

    spring6-IOC容器

    IOC 是 Inversion of Control 的簡(jiǎn)寫(xiě),譯為“控制反轉(zhuǎn)”,它不是一門(mén)技術(shù),而是一種設(shè)計(jì)思想,是一個(gè)重要的面向?qū)ο缶幊谭▌t,能夠指導(dǎo)我們?nèi)绾卧O(shè)計(jì)出松耦合、更優(yōu)良的程序。 Spring 通過(guò) IoC 容器來(lái)管理所有 Java 對(duì)象的實(shí)例化和初始化,控制對(duì)象與對(duì)象之間的依賴(lài)關(guān)系。我們將

    2024年02月08日
    瀏覽(20)
  • 【Spring教程九】Spring框架實(shí)戰(zhàn):全面深入詳解IOC/DI注解開(kāi)發(fā)

    【Spring教程九】Spring框架實(shí)戰(zhàn):全面深入詳解IOC/DI注解開(kāi)發(fā)

    歡迎大家回到《 Java教程之Spring30天快速入門(mén)》,本教程所有示例均基于Maven實(shí)現(xiàn),如果您對(duì)Maven還很陌生,請(qǐng)移步本人的博文《 如何在windows11下安裝Maven并配置以及 IDEA配置Maven環(huán)境》,本文的上一篇為《 IOC/DI配置管理第三方bean 加載properties文件》。 Spring的IOC/DI對(duì)應(yīng)的配置開(kāi)

    2024年02月03日
    瀏覽(26)
  • 11Spring IoC注解式開(kāi)發(fā)(上)(元注解/聲明Bean的注解/注解的使用/負(fù)責(zé)實(shí)例化Bean的注解)

    11Spring IoC注解式開(kāi)發(fā)(上)(元注解/聲明Bean的注解/注解的使用/負(fù)責(zé)實(shí)例化Bean的注解)

    注解的存在主要是為了簡(jiǎn)化XML的配置。Spring6倡導(dǎo)全注解開(kāi)發(fā)。 注解開(kāi)發(fā)的優(yōu)點(diǎn) :提高開(kāi)發(fā)效率 注解開(kāi)發(fā)的缺點(diǎn) :在一定程度上違背了OCP原則,使用注解的開(kāi)發(fā)的前提是需求比較固定,變動(dòng)較小。 自定義一個(gè)注解: 該注解上面修飾的注解包括:Target注解和Retention注解,這兩個(gè)注

    2024年01月21日
    瀏覽(30)
  • 2023新版Spring6全新講解-核心內(nèi)容之IoC

    2023新版Spring6全新講解-核心內(nèi)容之IoC

    ??IoC 是 Inversion of Control 的簡(jiǎn)寫(xiě),譯為“控制反轉(zhuǎn)”,它不是一門(mén)技術(shù),而是一種設(shè)計(jì)思想,是一個(gè)重要的面向?qū)ο缶幊谭▌t,能夠指導(dǎo)我們?nèi)绾卧O(shè)計(jì)出松耦合、更優(yōu)良的程序。 ??Spring 通過(guò) IoC 容器來(lái)管理所有 Java 對(duì)象的實(shí)例化和初始化,控制對(duì)象與對(duì)象之間的依賴(lài)關(guān)系

    2024年02月06日
    瀏覽(18)
  • 使用 spring 的 IoC 的實(shí)現(xiàn)賬戶(hù)的CRUD(2)雙層實(shí)現(xiàn)+注解開(kāi)發(fā)

    使用 spring 的 IoC 的實(shí)現(xiàn)賬戶(hù)的CRUD(2)雙層實(shí)現(xiàn)+注解開(kāi)發(fā)

    在http://t.csdn.cn/yucl4的基礎(chǔ)上進(jìn)行注解開(kāi)發(fā) 【分析】 xml文件其中spring容器中的bean, 因此通過(guò)注解把這些放到容器中即可 @component:相當(dāng)xml中的bean的id: 如果不指定 value 屬性,默認(rèn) bean 的 id 是當(dāng)前類(lèi)的類(lèi)名, 首字母小寫(xiě)。 @Controller @Service @Repository是三個(gè)衍生 @Autowired 自動(dòng)按照類(lèi)型

    2024年02月03日
    瀏覽(27)
  • Spring6學(xué)習(xí)技術(shù)|IoC+基于xml管理bean

    Spring6學(xué)習(xí)技術(shù)|IoC+基于xml管理bean

    尚硅谷Spring零基礎(chǔ)入門(mén)到進(jìn)階,一套搞定spring6全套視頻教程(源碼級(jí)講解) 控制反轉(zhuǎn)。是一種設(shè)計(jì)思想。 通過(guò)id,通過(guò)class,和雙重方式。 普通屬性:String, Interger (set和構(gòu)造器:感覺(jué)還是set比較方便) 特殊屬性:null,特殊的大于小于等(xml轉(zhuǎn)義字符,cdata) 對(duì)象:外部

    2024年02月21日
    瀏覽(28)
  • 【Spring教程十】Spring框架實(shí)戰(zhàn):全面深入詳解IOC/DI之--純注解開(kāi)發(fā)模式下的依賴(lài)注入&&注解讀取properties配置文件

    【Spring教程十】Spring框架實(shí)戰(zhàn):全面深入詳解IOC/DI之--純注解開(kāi)發(fā)模式下的依賴(lài)注入&&注解讀取properties配置文件

    歡迎大家回到《 Java教程之Spring30天快速入門(mén)》,本教程所有示例均基于Maven實(shí)現(xiàn),如果您對(duì)Maven還很陌生,請(qǐng)移步本人的博文《 如何在windows11下安裝Maven并配置以及 IDEA配置Maven環(huán)境》,本文的上一篇為《 全面深入詳解IOC/DI注解開(kāi)發(fā)》 Spring為了使用注解簡(jiǎn)化開(kāi)發(fā),并沒(méi)有提供

    2024年02月04日
    瀏覽(25)
  • 【Spring教程11】Spring框架實(shí)戰(zhàn):IOC/DI注解開(kāi)發(fā)管理第三方bean的全面深入詳解

    【Spring教程11】Spring框架實(shí)戰(zhàn):IOC/DI注解開(kāi)發(fā)管理第三方bean的全面深入詳解

    歡迎大家回到《 Java教程之Spring30天快速入門(mén)》,本教程所有示例均基于Maven實(shí)現(xiàn),如果您對(duì)Maven還很陌生,請(qǐng)移步本人的博文《 如何在windows11下安裝Maven并配置以及 IDEA配置Maven環(huán)境》,本文的上一篇為《 純注解開(kāi)發(fā)模式下的依賴(lài)注入和讀取properties配置文件》 前面定義bean的時(shí)

    2024年02月04日
    瀏覽(29)
  • 一篇文章帶你搞懂spring6的概念、spring入門(mén)與容器IoC詳解(尚硅谷筆記)

    一篇文章帶你搞懂spring6的概念、spring入門(mén)與容器IoC詳解(尚硅谷筆記)

    Spring 是一款主流的 Java EE 輕量級(jí)開(kāi)源框架 ,Spring 由“Spring 之父”Rod Johnson 提出并創(chuàng)立,其目的是用于簡(jiǎn)化 Java 企業(yè)級(jí)應(yīng)用的開(kāi)發(fā)難度和開(kāi)發(fā)周期。Spring的用途不僅限于服務(wù)器端的開(kāi)發(fā)。從簡(jiǎn)單性、可測(cè)試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。Spring 框架

    2023年04月16日
    瀏覽(24)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包