Spring Boot 读取xml文件
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
@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配置
<?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">
<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() {
helloService.sayHello();
}
}
资料参考