Spring IoC容器配置元数据的三种方式

1、xml

xml配置文件方式自然需要在xml文件中配置bean

<bean id="intelCpu" class="com.phz.entity.IntelCpu"/>
<bean id="computer" class="com.phz.entity.Computer">
    <property name="intelCpu" ref="intelCpu"/>
</bean>

使用的时候需要通过ClassPathXmlApplicationContext对象获取。

public class MyTest {
    
    

    @Test
    public void test() {
    
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Computer computer = (Computer) applicationContext.getBean("computer");
        computer.play();
    }
}

2、注解

Annotation方式需要配置扫描域,且需要在使用的类最上面声明@ComponentScan("com"),且bean上面加上@Component注解,需要注入bean就添加@Resource注解,获取bean的时候使用ClassPathXmlApplicationContext

<context:component-scan base-package="com"/>
@Component(value = "computer")
public class Computer {
    
    }
public class MyTest {
    
    
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    Computer computer = (Computer) applicationContext.getBean("computer");
}

3、纯JAVA

①、ComponentScan

不需要配置xml文件,仅仅是将xml文件中的ComponentScan节点,用注解的方式配置罢了,首先还是需要在bean上添加@Component,在容器初始化后,再声明@ComponentScan("com"),引导容器从com包下扫描带有@Component标识的类,并将其加载进入容器,在获取bean的时候需要用AnnotationConfigApplicationContext获取

@Component(value = "computer")
public class Computer {
    
    }
@ComponentScan("com")
public class MyTest {
    
    
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyTest.class);
    Computer computer = (Computer) applicationContext.getBean("computer");
}

②、Configuration

这种方式需要配置一个全局配置类ApplicationContextConfig(名称随意),加上@Configuration注解,然后下面使用@Bean注解注入Bean。通过此注解标注,在xml文件中可以配置的属性,这里统统都能找到对应的注解进行配置(你也可以理解为这个类变成了Java代码版本的XML配置文件),读者可自行下去验证。采用这种配置,bean便可不加@Component注解,也不需要配置@ComponentScan注解去主动扫描。

@Configuration
@Slf4j
public class ApplicationContextConfig {
    
    
    
    @Bean(name = "computer")
    public Computer computer() {
    
    
        log.info("Computer加载进容器");
        return new Computer("b");
    }

    @Bean(name = "intelCpu")
    public IntelCpu intelCpu() {
    
    
        log.info("IntelCpu加载进容器");
        return new IntelCpu();
    }
}

调用的时候同样是使用AnnotationConfigApplicationContext对象获取bean,但是传入的参数变为ApplicationContextConfig.class

@Slf4j
public class MyTest {
    
    

    @Test
    public void test() {
    
    
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
        Computer computer = (Computer) applicationContext.getBean("computer");
        computer.play();
    }
}

另一种调用方式是直接通过@Resource注解将我们需要的Bean注入进来

@Slf4j
public class MyTest {
    
    
    @Resource
    private Computer computer;

    @Test
    public void test() {
    
    
        computer.play();
    }
}

需要注意的是,当MyTest组件依赖Computer,要想使用Spring依赖注入得到组件Computer的实例,那么MyTest本身也要是通过Springbean来创建的才行,而不能够直接在MyTest里面注入,上面这段代码实际上是跑不通的,因为笔者在做这个Demo的时候是将调用方法写在了测试类中,而测试类本身是没有被Spring容器所管理,因此在运行中会报空引用异常,而不是NoSuchBean找不到这个bean(读者可尝试在普通Spring Web工程下,创建一个Controller来验证)。

猜你喜欢

转载自blog.csdn.net/qq_43509535/article/details/114370840