2 -【 SpringBoot 配置方式 】- 7 导入 Spring 的配置文件,让配置文件里面的内容生效

本文详细介绍了Spring Boot中两种配置方式:XML配置和配置类。通过示例展示了如何使用XML配置文件和配置类来注册bean,并在测试类中验证bean的存在。

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

1 方式一:编写 xml 配置文件

1.1 新建 HelloService

package com.snow.service;

public class HelloService {
}

1.2 新建 beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloService" class="com.snow.service.HelloService"></bean>
</beans>

1.3 启动项添加注解 @ImportResource

package com.snow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

/**
 *
 * 启动项
 *
 * @author: yang
 * @Date:
 * @Description:
 * @version v1.0
 */
@SpringBootApplication
@ImportResource(locations = "classpath:beans.xml")
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

1.4 测试类

package com.snow.snow_springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * SpringBoot 单元测试
 *
 * 可以再测试期间很方便的类似编码一样进行自动注入等 容器的功能
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootConfigAppTests {

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(){
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

}

控制台打印 true


2 方式二:使用配置类

2.1 删除掉上面的 beans.xml

2.2 编写配置类 MyConfig

package com.snow.config;

import com.snow.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
 *
 * 在配置文件中用<bean><bean/>标签添加组件
 */
@Configuration
public class MyConfig {

    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }

}


2.3 启动项删除注解 @ImportResource

package com.snow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * 启动项
 *
 * @author: yang
 * @Date:
 * @Description:
 * @version v1.0
 */
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

2.4 测试

控制台打印


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-02-15 15:15:49.906  INFO 84131 --- [           main] c.s.s.SpringbootConfigAppTests           : Starting SpringbootConfigAppTests on yangshuodembp with PID 84131 (started by yangshuo in /Users/yangshuo/Desktop/snow_springboot)
2020-02-15 15:15:49.907  INFO 84131 --- [           main] c.s.s.SpringbootConfigAppTests           : No active profile set, falling back to default profiles: default
配置类@Bean给容器中添加组件了...
2020-02-15 15:15:52.253  INFO 84131 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-15 15:15:52.719  INFO 84131 --- [           main] c.s.s.SpringbootConfigAppTests           : Started SpringbootConfigAppTests in 4.284 seconds (JVM running for 6.07)
true
2020-02-15 15:15:53.032  INFO 84131 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不知所起 一往而深

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值