spring学习(15):required属性

本文详细探讨了Spring框架中`required`属性的应用,通过实例解析了CompactDisc、CDplay、appconfig、power和apptest类如何配置及交互。在实际运行中,展示了`required`属性对于依赖注入的影响和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

CompactDisc类

package soundSystem;


import org.springframework.stereotype.Component;
//注解@Component

public class CompactDisc {

    public CompactDisc() {
        super();
        System.out.println("compactdisc无参构造方法");
    }
    public void play(){
        System.out.println("正在播放音乐....");
    }
}

CDplay类

package soundSystem;

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

@Component
public class CDPlayer {
    @Autowired(required = false)
    private CompactDisc cd;
    @Autowired
    private Power power;
    public CDPlayer() {
        super();
        System.out.println("CDPlayer无参构造方法");
    }
/*@Autowired
    public CDPlayer(CompactDisc cd) {
        this.cd = cd;
        System.out.println("CDPlayer有参构造方法");
    }*/
/*
    public CDPlayer(CompactDisc cd, Power power) {
        this.cd = cd;
        this.power = power;
        System.out.println("CDplayer的参数配置");
    }
    */

    public void play(){
        power.supply();
        if(cd!=null) {
            cd.play();
        }
    }
}

 

appconfig类

package soundSystem;

import org.springframework.context.annotation.ComponentScan;
//spring注解类
@ComponentScan
public class Appconfig {
}

power类

package soundSystem;

import org.springframework.stereotype.Component;

@Component
public class Power {
    public Power() {
        super();
    }
    public void supply(){
        System.out.println("电源宫殿中..");
    }
}

apptest类

package soundSystem;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//@RunWith(SpringJunit4ClassRunner.class)
//@ContextConfiguration(class=)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)
public class AppTest {
    @Autowired
    private CDPlayer player;
    @Test
    public void testPlay(){
       player.play();
    }
}

运行结果

[INFO ] 2019-10-30 18:42:32,195 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getDefaultTestExecutionListenerClassNames(AbstractTestContextBootstrapper.java:260)
Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
[INFO ] 2019-10-30 18:42:32,203 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小歌谣

放弃很容易 但是坚持一定很酷

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值