spring boot @propertySource @importResource @Bean [六]
2023-09-14 08:59:24 时间
@propertySource
指定property的配置源。
创建一个person.property:
然后修改person注解;
在运行test之后,结果为:
@importResource
这个importResource是用来兼容spring的。
HelloService:
package com.axm.demo.service;
public class HelloService {
public HelloService(){
}
}
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.axm.demo.service.HelloService"></bean>
</beans>
然后注入:
@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"}) //关键
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
测试:
@Test
void contextLoads() {
boolean has=ioc.containsBean("helloService");
System.out.println(has);
}
结果:
然后再spring boot 有新的方式去注入服务:
添加一个配置文件:
@Configuration
public class MyApplication {
@Bean
public HelloService helloService(){
return new HelloService();
}
}
然后就会自动注入这个HelloService 服务。
相关文章
- Spring Boot 最新发布,支持 Java 19,这速度太快了。。
- 关于spring boot自动注入出现Consider defining a bean of type ‘xxx‘ in your configuration问题解决方案
- Spring 全家桶之 Spring Boot 2.6.4(五)- WebMvcAutoConfiguration(Part C)
- 解释spring框架中bean的生命周期_Spring bean的生命周期
- spring boot整合shiro_Spring框架介绍及使用
- 玩转 SpringBoot 监控统计(SQL监控、慢SQL记录、Spring监控、去广告)
- spring boot 启动的时候required a bean of type 'XXX' that could not be
- 【Spring boot Nacos系列教程】Nacos第三篇:多环境配置
- SpringBoot:模块探究之spring-boot-cli
- 软件测试|后端web开发框架——Spring Boot
- Spring Cloud Zuul 过滤器拆分serivceId和请求路径
- 【Spring源码】- 08 扩展点之mybatis集成
- Spring Boot 3 将于 2022 年 11 月发布,延迟了对 Java 模块系统的支持
- Spring Boot应用中引入Zuul
- Spring错误:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.B详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- java spring boot消息队列 RabbitMQ详解编程语言
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase