一、组件添加
1、@Configuration
| <bean id="user" class="com.cmq.boot.bean.User"> |
| <property name="name" value="cencen"/> |
| <property name="age" value="18"/> |
| </bean> |
| <bean id="pet" class="com.cmq.boot.bean.Pet"> |
| <property name="name" value="cat"/> |
| </bean> |
| #############################Configuration使用示例###################################################### |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Configuration(proxyBeanMethods = false) |
| public class MyConfig { |
| |
| |
| |
| |
| |
| @Bean |
| public User user01(){ |
| User cencen = new User("cencen", 18); |
| |
| cencen.setPet(tomcatPet()); |
| return cencen; |
| } |
| |
| @Bean("tom") |
| public Pet tomcatPet(){ |
| return new Pet("tomcat"); |
| } |
| } |
| |
| |
| ################################@Configuration测试代码如下######################################## |
| |
| |
| |
| |
| |
| @SpringBootApplication |
| public class MainApplication { |
| |
| public static void main(String[] args) { |
| |
| ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args); |
| |
| |
| String[] names = run.getBeanDefinitionNames(); |
| for (String name : names) { |
| System.out.println(name); |
| } |
| |
| |
| |
| Pet tom01 = run.getBean("tom", Pet.class); |
| |
| Pet tom02 = run.getBean("tom", Pet.class); |
| |
| System.out.println("组件:"+(tom01 == tom02)); |
| |
| |
| |
| MyConfig bean = run.getBean(MyConfig.class); |
| System.out.println(bean); |
| |
| |
| |
| User user = bean.user01(); |
| User user1 = bean.user01(); |
| System.out.println(user == user1); |
| |
| |
| User user01 = run.getBean("user01", User.class); |
| Pet tom = run.getBean("tom", Pet.class); |
| |
| System.out.println("用户的宠物:"+(user01.getPet() == tom)); |
| |
| |
| |
| } |
| } |
| |
2、@Bean、@Component、@Controller、@Service、@Repository
3、@ComponentScan、@Import
| * 4、@Import({User.class, DBHelper.class}) |
| * 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名 |
| */ |
| |
| @Import({User.class, DBHelper.class}) |
| @Configuration(proxyBeanMethods = false) |
| public class MyConfig { |
| } |
4、@Conditional
条件装配:满足Conditional指定的条件,则进行组件注入

| |
| @Configuration(proxyBeanMethods = false) |
| |
| @ConditionalOnMissingBean(name = "tom") |
| public class MyConfig { |
| |
| |
| |
| |
| |
| |
| |
| @Bean |
| public User user01(){ |
| User zhangsan = new User("zhangsan", 18); |
| |
| zhangsan.setPet(tomcatPet()); |
| return zhangsan; |
| } |
| |
| @Bean("tom22") |
| public Pet tomcatPet(){ |
| return new Pet("tomcat"); |
| } |
| } |
| |
| public static void main(String[] args) { |
| |
| ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args); |
| |
| |
| String[] names = run.getBeanDefinitionNames(); |
| for (String name : names) { |
| System.out.println(name); |
| } |
| |
| boolean tom = run.containsBean("tom"); |
| System.out.println("容器中Tom组件:"+tom); |
| |
| boolean user01 = run.containsBean("user01"); |
| System.out.println("容器中user01组件:"+user01); |
| |
| boolean tom22 = run.containsBean("tom22"); |
| System.out.println("容器中tom22组件:"+tom22); |
| |
| |
| } |
二、原生配置文件引入
1、@ImportResource
| <?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="haha" class="com.cmq.boot.bean.User"> |
| <property name="name" value="cencen"/> |
| <property name="age" value="18"/> |
| </bean> |
| <bean id="hehe" class="com.cmq.boot.bean.Pet"> |
| <property name="name" value="cat"/> |
| </bean> |
| </beans> |
测试
| @ImportResource("classpath:beans.xml") |
| public class MyConfig { |
| |
| boolean haha = run.containsBean("haha"); |
| boolean hehe = run.containsBean("hehe"); |
| System.out.println("haha:"+haha); |
| System.out.println("hehe:"+hehe); |
| } |
| |
三、配置绑定
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;
| public class getProperties { |
| public static void main(String[] args) throws FileNotFoundException, IOException { |
| Properties pps = new Properties(); |
| pps.load(new FileInputStream("a.properties")); |
| Enumeration enum1 = pps.propertyNames(); |
| while(enum1.hasMoreElements()) { |
| String strKey = (String) enum1.nextElement(); |
| String strValue = pps.getProperty(strKey); |
| System.out.println(strKey + "=" + strValue); |
| |
| } |
| } |
| } |
| |
1.@ConfigurationProperties
- application.properties配置文件
| mycar.brand=BYD |
| mycar.price=10000 |
| |
| |
| |
| @Component |
| @ConfigurationProperties(prefix = "mycar") |
| public class Car { |
| |
| private String brand; |
| private Integer price; |
| |
| public String getBrand() { |
| return brand; |
| } |
| |
| public void setBrand(String brand) { |
| this.brand = brand; |
| } |
| |
| public Integer getPrice() { |
| return price; |
| } |
| |
| public void setPrice(Integer price) { |
| this.price = price; |
| } |
| |
| @Override |
| public String toString() { |
| return "Car{" + |
| "brand='" + brand + '\'' + |
| ", price=" + price + |
| '}'; |
| } |
| } |
2、@EnableConfigurationProperties + @ConfigurationProperties
| - 如果不加@@Component,则需要在配置类上添加@EnableConfigurationProperties开启car的属性配置功能 |
| - 管理第三方bean |
3、@Component + @ConfigurationProperties
| @EnableConfigurationProperties(Car.class) |
| |
| |
| public class MyConfig { |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!