溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

spring怎么實(shí)現(xiàn)依賴注入DI

發(fā)布時(shí)間:2022-03-10 09:12:45 來源:億速云 閱讀:111 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了spring怎么實(shí)現(xiàn)依賴注入DI的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇spring怎么實(shí)現(xiàn)依賴注入DI文章都會有所收獲,下面我們一起來看看吧。

    spring依賴注入DI

    1、創(chuàng)建一個(gè)maven項(xiàng)目

    mvn archetype:generate -DarchetypeCatalog=internal

    2、修改pom.xml

    引入需要的依賴,首先spring-context,還是spring-test,最后還有junit。

    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <springframework.version>4.3.7.RELEASE</springframework.version>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${springframework.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${springframework.version}</version>
            </dependency>
     
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>utf-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.xueyoucto.xueyou.App</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id> <!-- this is used for inheritance merges -->
                            <phase>package</phase> <!-- bind to the packaging phase -->
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

    3、添加類Person和Body

    package com.xueyou.demo;
    import org.springframework.stereotype.Component;
    @Component
    public class Person {
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        private String name;
    }
    package org.xueyou.demo;
    import org.springframework.stereotype.Component;
    @Component
    public class Body {
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        private int id;
    }

    4、在配置類App中,添加ComponentScan

    需要注意的是,這里需要指定掃描的包

    package com.xueyou.demo;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    /**
     * Hello world!
     */
    @Configuration
    @ComponentScan(basePackages = {"org.xueyou.demo","com.xueyou.demo"})
    public class App {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }

    5、新建一個(gè)測試類

    package com.xueyou.demo;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.xueyou.demo.Body;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = App.class)
    public class Springtest {
        @Autowired
        private Body body;
        @Autowired
        private Person person;
        @Test
        public void testBodyIsNull(){
            Assert.assertNotNull(body);
        }
        @Test
        public void testPersonIsNull(){
            Assert.assertNotNull(person);
        }
    }

    6、運(yùn)行測試類

    結(jié)果如下:

    spring怎么實(shí)現(xiàn)依賴注入DI

    7、從運(yùn)行結(jié)果中我們能看到

    Person類和Student類已經(jīng)被依賴注入到spring中,spring能夠使用這兩個(gè)類了。 

    spring-test依賴無法使用問題

    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.7.RELEASE</version>
                <scope>test</scope>
    </dependency>

    去掉

    <scope>test</scope>

    關(guān)于“spring怎么實(shí)現(xiàn)依賴注入DI”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“spring怎么實(shí)現(xiàn)依賴注入DI”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI