当前位置: 首页 > news >正文

spring:加载配置类

在前面的学习中,通过读取xml文件将类加载,或他通过xml扫描包,将包中的类加载。无论如何都需要通过读取xml才能够进行后续操作。

在此创建配置类。通过对配置类的读取替代xml的功能。

配置类就是Java类,有以下内容需要执行:

1:对类使用注解@Configuration标注。

2:使用注解@ComponentScan扫描包,将包中的类加载。

 配置类SpringConfig:

package com.annotation.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-04-18* @version   1.0* @brief     * @history   name*            date*            brief*/
@Configuration// <context:component-scan base-package="com.annotation.thirdjar" />
@ComponentScan(basePackages = {"com.annotation.thirdjar", "com.itheima.dao.impl"})
public class SpringConfig {
}

在这里配置类加载了两个包,两个包里的类都可以被调用。

加载配置类:

package com.annotation.config;import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import static org.junit.jupiter.api.Assertions.*;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-04-18* @version   1.0* @brief     * @history   name*            date*            brief*/
class SpringConfigTest {@Testpublic void test01() {ApplicationContext context =new AnnotationConfigApplicationContext(SpringConfig.class);String date = (String) context.getBean("getDateInStringFormat");System.out.println(date);}
}

运行结果显示当前年月日及时间。

调用的类如下:

package com.annotation.thirdjar;import java.util.Date;import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-04-16* @version   1.0* @brief     * @history   name*            date*            brief*/
@Component(value = "factory")
public class DateFactory {public DateFactory() {}@Bean(name = "getDate001")public Date getDate() {return new Date();}
}

package com.annotation.thirdjar;import java.text.SimpleDateFormat;
import java.util.Date;import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-04-15* @version   1.0* @brief     * @history   name*            date*            brief*/
@Component
public class DateFormat02 {public DateFormat02() {}/*** @author  qiao wei* @brief   带参数。基本类型使用@Value,引用类型使用@Qualifier。* @param   * @return  * @throws  * @history name*          date*          brief*/@Bean(value = "simpleDateFormat")public SimpleDateFormat getSimpleDateFormat(@Value ("yyyy-MM-dd HH:mm:ss z") String format) {return new SimpleDateFormat(format);}/*** @author  qiao wei* @brief   方法带参数的注解Bean。* @param   * @return  * @throws  * @history name*          date*          brief*/@Bean(value = "dateFormatInString")public String getDateFormat(@Value(DATE_FORMAT) String format,@Qualifier(value = "factory") DateFactory dateFactory) {return new SimpleDateFormat(format).format(dateFactory.getDate());}/*** @author  qiao wei* @brief   方法带参数的注解Bean。注意与test02相比,参数date调用的是类Factory的getDate方法的返回值。* @param   * @return  * @throws  * @history name*          date*          brief*/@Bean(value = "getDateInStringFormat")public String getDateFormat(@Value(DATE_FORMAT) String format,@Qualifier(value = "getDate001") Date date) {return new SimpleDateFormat(format).format(date);}private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
}

相关文章:

  • es 混合检索多向量
  • Unity-微信截图功能简单复刻-01实现思路
  • http://noi.openjudge.cn/——2.5基本算法之搜索——1998:寻找Nemo
  • 模拟量和数字量的区别
  • #手动控制windows更新时间(非常安全,可随时恢复)
  • 热门与冷门并存,25西电—电子工程学院(考研录取情况)
  • 高阶数据结构 图 (上)
  • 在PyCharm中部署AI模型的完整指南
  • 人脸识别联合行为检测的办公管理新模式
  • 《AI大模型应知应会100篇》第24篇:限定输出格式:如何让AI回答更加结构化
  • IcePlayer音乐播放器项目分析及学习指南
  • C++_设计模式\_观察者模式(Observer Pattern)
  • 第24周:Resnet结合DenseNet
  • easyexcel使用模板填充excel坑点总结
  • 2025年MathorCup数学应用挑战赛D题问题一求解与整体思路分析
  • 51单片机实验二:数码管静态显示
  • yarn的定义,yarn的三大组件及各自作用,yarn的三个资源调度策略
  • 音乐播放对接状态栏MediaSession控制
  • Yocto项目实战教程 · 第4章:4.3小节-层
  • 2025年03月中国电子学会青少年软件编程(Python)等级考试试卷(五级)答案 + 解析
  • 在没有穹顶的剧院,和春天的音乐会来一场约会
  • 祥源文旅:2024年营业收入约8.64亿元,今年旅游经济总体预期更为乐观
  • 杭州挂牌3宗住宅用地,起始总价约30.04亿元
  • 上海浦东打造全新开放平台,年内实现基本功能落地运行
  • 虚假营销屡禁不止,市场监管总局严厉打击直播电商违法行为
  • 陈少洋出任中国航天科工集团党组副书记、董事、总经理