Spring Boot入门笔记

本文深入探讨Spring框架中关键配置注解的使用,包括@Configration、@Bean、@Component、@ComponentScan、@import等,讲解如何替代XML配置,实现更简洁的Java配置。

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

参考:https://www.jianshu.com/p/8e3de55d4373

(1)@Configration标签   

表示这个类可被Spring识别的配置对象的类,只有有这个标记的标签的类才能使用@Bean标签作用于对应的方法上面。

(2)@Bean标签

@Bean(destroyMethod = "destory", initMethod = "init")也可以通过这样的写法来配置bean的初始化方法和销毁方法。

使用示例:

package edu.xatu.cn;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//	 作为Spring的主配置文件
//   这个标签表示这个类可被Spring识别的配置对象的类,只有有这个标记的标签的类才能使用
//   @Bean标签作用于对应的方法上面
@Configuration   
public class AppConfig {
	
//告诉Spring这个方法是Spring帮我们管理的bean通过@Bean标签
	@Bean
	public BeanTest getBeanTest(){
		return new BeanTest();
	}
	
	@Bean
	public BeanTest2 getBeanTest2(){
		return new BeanTest2();
	}
}

原来创建bean对象的方法:

1.配置文件application.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
      
       <bean id="BeanTest" class="edu.xatu.cn.BeanTest"></bean>
</beans>

2.读取配置文件:

//基于Spring容器生成对象
	@Test
	public void test1(){
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		BeanTest bean = context.getBean(BeanTest.class);
		System.out.println(bean);
	}

现在使用Configration注解方法:AppConfig.java上面已经给出

//基于AnnotationConfigApplicationContext来配置
	@Test
	public void test2(){
		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		BeanTest bean1 = context.getBean(BeanTest.class);
		BeanTest2 bean2 = context.getBean(BeanTest2.class);
		System.out.println(bean1);
		System.out.println(bean2);
	}

 (3)@Component,@ComponentScan标签

@ComponentScan 开启组件自动扫描:默认情况下,它会扫描当前类所在的包及其子包中的所有标签对象加载到spring容器

     

(4)对象的关系引用:

BeanTest1中依赖一个BeanTest2

测试类MyTest.java:

//对象的关系引用
	@Autowired
	@Qualifier("bean1")
	private  BeanTest bean1;
	@Test
	public void test3(){
		System.out.println(bean1.getBeanTest2());
	}

配置文件类AppConfig.java

@Bean(destroyMethod = "destory", initMethod = "init")
	public BeanTest beanTest(BeanTest2 bean2){
		BeanTest bean1 = new BeanTest();
		bean1.setOtherBean(bean2);
		return bean1;
	}

使用try来关闭容器(MyTest.java)

//	使用try来关闭容器
    @Test
	public void test4(){
		try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)){
			BeanTest bean1 = context.getBean(BeanTest.class,"bean1");
			System.out.println(bean1);
		};
	}

(5)@import标签

用来导入配置类,主配置文件AppConfig导入其他配置文件类

其他两个配置文件:

@Configuration
public class ConfigRedis {

	@Bean
	public MyRedisTemplate myRedisTemplate(){
		return new MyRedisTemplate();
	}
}
@Configuration
public class ConfigDataSource {

	@Bean
	public MyDataSource myDataSource(){
		return new MyDataSource();
	}
}

测试:

(6)@importresource标签在javaConfig中混用xml,导入资源

配置文件配置了BeanTest2对象

BeanTest1中有BeanTest2的引用

在主配置文件AppConfig.java中使用@importresource标签导入资源文件

测试MyTest.java:

(7)引入外部资源.properties资源文件

属性文件以及实体类

     

主配置文件使用propertysource标签

测试:

第二种方式:

AppConfig.java

属性文件以及实体类

   

配置类

主配置导入其他两个配置文件

测试:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值