spring:加载配置类

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

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

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

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

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

配置类SpringConfig:

java 复制代码
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 {
}

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

加载配置类:

java 复制代码
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 {
    
    @Test
    public void test01() {
        ApplicationContext context =
            new AnnotationConfigApplicationContext(SpringConfig.class);
        String date = (String) context.getBean("getDateInStringFormat");
        System.out.println(date);
    }
}

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

调用的类如下:

java 复制代码
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();
    }
}
java 复制代码
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";
}
相关推荐
用户62960593247055 分钟前
从“浏览器根本区分不了”到真正实现:刷新不退出,关闭标签页自动退出,前端判断浏览器关闭和刷新
前端
qetfw6 分钟前
MWU:Vue 3 + FastAPI 的 MaaFramework 跨平台 WebUI 源码
前端·vue.js·python·fastapi·开源项目·效率工具
zy happy6 分钟前
VMware虚拟机添加新的硬盘
java·linux·jvm·docker
mfxcyh7 分钟前
Vue3+Ant Design Vue 实现手动异步文件上传(含大小校验、弹窗上传)
前端·javascript·vue.js
wordbaby9 分钟前
Web端热敏标签打印完整解决方案(QZ Tray + 译维A42)
前端
swipe13 分钟前
03|Axios 请求进了后端之后:Controller、Request、Response 是怎么接住它的?
前端·后端·全栈
meilindehuzi_a18 分钟前
Vue3进阶基础:指令修饰符、v-model表单绑定、动态样式、计算属性与侦听器实战
前端·javascript·vue.js
AI人工智能+电脑小能手23 分钟前
【大白话说Java面试题 第192题】【08_Kafka篇】第8题:死信队列是什么?延时队列是什么?
java·kafka·消息队列·死信队列·延时队列
csdn2015_24 分钟前
vue 前端运行命令
前端·javascript·vue.js
swipe24 分钟前
02|从 `pnpm dev` 到 Spring Boot 启动:后端服务到底怎么跑起来?
前端·后端·全栈