因为第三方给的bean实例封装在jar包中,无法通过给类添加注解@Component后再获取bean实例,我们可以通过给方法添加注解@Bean,获取bean实例(方法的返回值就是第三方bean实例)。
可获取第三方bean的类
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();
}
}
配置类,将DateFactory加载。
package com.annotation.c