springboot @import @importResource 注解使用方式

demo项目结构(Maven约定):
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── jiaobuchong
    │   │           ├── config
    │   │           │   ├── CDConfig.java
    │   │           │   ├── CDPlayerConfig.java
    │   │           │   └── SoundSystemConfig.java
    │   │           ├── dao
    │   │           │   └── CompactDisc.java
    │   │           └── soundsystem
    │   │               ├── BlankDisc.java
    │   │               ├── CDPlayer.java
    │   │               └── SgtPeppers.java
    │   └── resources
    │       └── cons-injec.xml
    └── test
        └── java
            └── com
                └── jiaobuchong
                    ├── soundsystem
                        ├── CDPlayerTest.java
                        ├── CDPlayerTest1.java
                        └── CDPlayerTest2.java
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
config包下的三个配置类
CDConfig.java:
package com.jiaobuchong.config;

import com.jiaobuchong.dao.CompactDisc;
import com.jiaobuchong.soundsystem.SgtPeppers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CDConfig {
    @Bean   // 将SgtPeppers注册为 SpringContext中的bean
    public CompactDisc compactDisc() {
        return new SgtPeppers();  // CompactDisc类型的
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
CDPlayerConfig.java:
package com.jiaobuchong.config;

import com.jiaobuchong.dao.CompactDisc;
import com.jiaobuchong.soundsystem.CDPlayer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(CDConfig.class)  //导入CDConfig的配置
public class CDPlayerConfig {
    @Bean(name = "cDPlayer")
    public CDPlayer cdPlayer(CompactDisc compactDisc) {  
    /*这里会注入CompactDisc类型的bean
     这里注入的这个bean是CDConfig.class中的CompactDisc类型的那个bean*/
        return new CDPlayer(compactDisc);
    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
SoundSystemConfig.java:
package com.jiaobuchong.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

@Configuration
@Import(CDPlayerConfig.class)  
@ImportResource("classpath:cons-injec.xml") //导入xml配置项
public class SoundSystemConfig {

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
@Configuration注解表示定义一个配置类,
这里使用注解@Bean就好比如xml配置时的<bean>元素,如:
<bean id="cdPlayer" class="com.jiaobuchong.soundsystem.CDPlayer">
       <property name="cd" ref="compactDisc" />
</bean>
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@ImportResource类似于xml配置时的:
<import resource="cons-injecxml" />
 
 
  • 1
  • 1
dao包下的一个接口:
CompactDisc.java:
package com.jiaobuchong.dao;

public interface CompactDisc {
    void play();
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
soundsystem包下的类:
BlankDisc.java:
package com.jiaobuchong.soundsystem;

import com.jiaobuchong.dao.CompactDisc;

import java.util.List;

public class BlankDisc implements CompactDisc {
    private String title;
    private String artist;
    private List<String> tracks;

    public BlankDisc(String title, String artist, List<String> tracks) {
        this.title = title;
        this.artist = artist;
        this.tracks = tracks;
    }

    public void play() {
        System.out.println("Playing " + title + " by " + artist);
        for (String track : tracks) {
            System.out.println("-Track: " + track);
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
CDPlayer.java:
package com.jiaobuchong.soundsystem;

import com.jiaobuchong.dao.CompactDisc;
import org.springframework.beans.factory.annotation.Autowired;

public class CDPlayer implements CompactDisc {
    private CompactDisc cd;

    @Autowired   //构造函数注入
    public CDPlayer(CompactDisc cd) {
        this.cd = cd;
    }

    public void play() {
        cd.play();
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
SgtPeppers.java:
package com.jiaobuchong.soundsystem;

import com.jiaobuchong.dao.CompactDisc;
import org.springframework.stereotype.Component;

public class SgtPeppers implements CompactDisc {
    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
xml配置文件
cons-injec.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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       ">
       <bean id="compactDisc"
             class="com.jiaobuchong.soundsystem.BlankDisc"
             c:_0="Sgt. Pepper's Lonely Hearts Club Band"
             c:_1="The Beatles">
              <constructor-arg>
                     <list>
                            <value>Sgt. Pepper's Lonely Hearts Club Band</value>
                            <value>With a Little Help from My Friends</value>
                            <value>Lucy in the Sky with Diamonds</value>
                            <value>Getting Better</value>
                            <value>Fixing a Hole</value>
                            <!-- ...other tracks omitted for brevity... -->
                     </list>
              </constructor-arg>
       </bean>
</beans>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
测试类
CDPlayerTest.java:
package com.jiaobuchong.soundsystem;

import com.jiaobuchong.config.CDConfig;
import com.jiaobuchong.config.CDPlayerConfig;
import com.jiaobuchong.dao.CompactDisc;
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;

/*
CDPlayerTest takes advantage of Spring’s SpringJUnit4ClassRunner to have a Spring application
context automatically created when the test starts. And the @Context-Configuration annotation
tells it to load its configuration from the CDPlayerConfig class.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDConfig.class)
public class CDPlayerTest {
    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
//        assertNotNull(cd);
        cd.play();
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
CDPlayerTest1.java:
package com.jiaobuchong.soundsystem;

import com.jiaobuchong.config.CDConfig;
import com.jiaobuchong.config.CDPlayerConfig;
import com.jiaobuchong.dao.CompactDisc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest1 {
    @Autowired
    @Qualifier("cDPlayer")
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
//        assertNotNull(cd);
        cd.play();
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
CDPlayerTest2.java:
package com.jiaobuchong.soundsystem;

import com.jiaobuchong.config.CDPlayerConfig;
import com.jiaobuchong.config.SoundSystemConfig;
import com.jiaobuchong.dao.CompactDisc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SoundSystemConfig.class)
public class CDPlayerTest2 {
    @Autowired
    @Qualifier("cDPlayer")
    private CompactDisc cd;

    @Autowired
    @Qualifier("compactDisc")
    private CompactDisc cd1;

    @Test
    public void cdShouldNotBeNull() {
//        assertNotNull(cd);
        cd.play();
    }

    @Test
    public void testBlankDisc() {
        cd1.play();
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

总结:

@import 和@importResource 都是导入另外一个配置。

然后另外的配置里定义的bean 就可以 被现在配置类使用了。





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
回答: @Component和@Import是Spring框架中的两个不同的注解,它们有不同的作用。 @Component注解是一个通用的注解,用于标识一个类是一个组件。它可以被其他注解所包含,比如@Service、@Repository等。@Component注解的作用是将被注解的类实例化为一个Spring的Bean,并将其纳入Spring容器的管理范围内。示例组件类注入@Component类的实例说明。 @Import注解是用于将其他配置类或者XML配置文件中的bean加载到当前的配置类中。它可以与@Configuration或@SpringBootApplication注解一起使用。@Import注解的作用是将被注解的配置类或者XML配置文件中的bean引入到当前的配置类中,以便在当前配置类中使用这些bean。@ImportResource注解是@Import注解的一种特殊形式,用于将XML配置文件中的bean加载到Application Context中。示例@Configuration类注入moon实例说明。 因此,@Component注解用于标识一个类是一个组件,并将其纳入Spring容器的管理范围内,而@Import注解用于将其他配置类或者XML配置文件中的bean加载到当前的配置类中。 #### 引用[.reference_title] - *1* [@Component @Bean @Configuration 用法](https://blog.csdn.net/qq_29025955/article/details/121998833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [@Component 和,@Bean和@ImportResource的区别](https://blog.csdn.net/belongtocode/article/details/101320822)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值