beans profile [Spring Boot]

Spring Boot 读取xml文件

xml配置

  • application-bean.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="testImportResource" class="com.diy.sigmund.mybatisoracle.test.TestImportResource"/>
</beans>
public class TestImportResource {

    public TestImportResource() {
        System.out.println("这是一个通过xml配置的测试类,启动时会打印该语句");
    }

    public void getMessage() {
        System.out.println("is testImportResource");
    }

}

Spring Boot 引入使用@ImportResource

/**
 * 旧框架SSM项目移行到SpringBoot中,直接读取旧xml文件,读取xml配置bean(@ImportResource)
 * 
 * 读取xml配置bean测试类使用@ContextConfiguration
 */
@MapperScan("com.diy.sigmund.mybatisoracle.mapper")
@ImportResource(locations = {"classpath:config/xmlConfig/application-bean.xml",
    "classpath:config/xmlConfig/applicationContext.xml"})
@SpringBootApplication
public class MybatisOracleApplication {

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

}

Spring Boot单元测试引入使用@ContextConfiguration

@SpringBootTest
@ContextConfiguration(locations = {"classpath:config/xmlConfig/application-bean.xml"})
class TestImportResourceTest {

  @Autowired
  private TestImportResource testImportResource;

  @Test
  void getMessage() {
    testImportResource.getMessage();
  }
}

Profile

xml配置

  • applicationContext.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:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-4.2.xsd">
  <!-- 定义profile -->
  <beans profile="test1">
    <!--定义包扫描路径-->
    <context:component-scan base-package="com.diy.sigmund.mybatisoracle.service.impl.test1"/>
    <!-- 加载配置文件 -->
    <util:properties id="config"
      location="classpath:config/properties/test1/test1properties.properties"/>
  </beans>

  <beans profile="test2">
    <context:component-scan base-package="com.diy.sigmund.mybatisoracle.service.impl.test2"/>
    <util:properties id="config"
      location="classpath:config/properties/test2/test2properties.properties"/>
  </beans>
</beans>

测试接口

public interface HelloService {

    void sayHello();

}
@Service
public class Test1HelloService implements HelloService {

    private static final Logger LOGGER = LoggerFactory.getLogger(Test1HelloService.class);

    @Value("#{config.name}")
    private String name;

    @Override
    public void sayHello() {
        LOGGER.info("HelloService name is {}", name);
    }
}
@Service
public class Test2HelloService implements HelloService {

    private static final Logger LOGGER = LoggerFactory.getLogger(Test2HelloService.class);

    @Value("#{config.name}")
    private String name;

    @Override
    public void sayHello() {
        LOGGER.info("HelloService name is {}", name);
    }
}

properties

  • test1properties.properties
name=test1
  • test2properties.properties
name=test2

单元测试

@SpringBootTest
@ContextConfiguration(locations = {"classpath:config/xmlConfig/application-bean.xml",
    "classpath:config/xmlConfig/applicationContext.xml"})
@ActiveProfiles("test2")
class HelloServiceTest {

    @Autowired
    private HelloService helloService;

    @Test
    void sayHello() {
        // 2020-11-01 21:43:44.706 INFO 5644 --- [ main] c.d.s.m.s.impl.test2.Test2HelloService : HelloService name is
        // test2
        helloService.sayHello();
    }
}

资料参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值