非spring类调用spring方法

在spring中我们常用 @RestController @Controller @Service @Component @Repository等奖这个类加入spring管理。

在调用时,我们用@Resource @Autowired进行引入这些类,使用@Value引入配置项

那么在非Spring管理的类,即没有这些注解给类中如何引入呢,还用@Service一致注解下去?这个类有很多父类和子类呢,那简直是地狱。

事实上,我们可以通过新建工具类让非Spring管理的类也能调用这些方法。

工具类代码

@Component
public final class SpringContextHolder implements BeanFactoryPostProcessor, ApplicationContextAware {

    /**
     * 日志记录器
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextHolder.class);

    /**
     * ApplicationContext
     */
    private static ApplicationContext context;

    /**
     * ServletContext
     */
    private static ServletContext servletContext;

    /**
     * Environment
     */
    private static Environment environment;

    /**
     * MessageSourceAccessor
     */
    private static MessageSourceAccessor messages;

    /**
     * Constructor<br>
     * 私有化工具类的构造函数
     */
    private SpringContextHolder() {
    }

    /**
     * get ApplicationContext<br>
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getSpringContext() {

        return context;
    }

    /**
     * get {@link ServletContext}<br>
     *
     * @return {@link ServletContext}
     */
    public static ServletContext getServletContext() {

        return servletContext;
    }

    /**
     * get Environment<br/>
     *
     * @return
     */
    public static Environment getEnvironment() {

        return environment;
    }

    /**
     * 根据名字获得spring context中的bean<br>
     *
     * @param name bean的名称
     * @return bean
     */
    public static Object getBean(String name) {

        return context.getBean(name);
    }

    /**
     * 根据类型获得spring context中的bean<br>
     *
     * @param requiredType bean的类型
     * @return bean
     */
    public static <T> T getBean(Class<T> requiredType) {

        return context.getBean(requiredType);
    }

    /**
     * 根据名称和类型获得spring context中的bean<br>
     *
     * @param name         bean 的名称
     * @param requiredType bean的类型
     * @return bean
     */
    public static <T> T getBean(String name, Class<T> requiredType) {

        return context.getBean(name, requiredType);
    }

    /**
     * 获取properties的值,没有获取到返回null<br>
     *
     * @return 该key对应的value值
     */
    public static String getProperty(String key) {

        return environment.getProperty(key);
    }

    /**
     * 获取properties的值,没有获取到抛出异常<br>
     *
     * @return 该key对应的value值
     * @throws IllegalStateException if the key cannot be resolved
     */
    public static String getRequiredProperty(String key) {

        return environment.getRequiredProperty(key);
    }

    /**
     * set Servlet Context<br>
     *
     * @param sc ServletContext
     */
    public static void setServletContext(ServletContext sc) {

        servletContext = sc;
    }

    /**
     * 获取国际化访问工具<br>
     *
     * @return 国际化访问工具
     */
    public static MessageSourceAccessor getMessageSourceAccessor() {

        return messages;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {

        init(applicationContext);
    }

    /**
     * 对相关的属性进行赋值<br/>
     *
     * @param applicationContext ApplicationContext
     */
    private static void init(ApplicationContext applicationContext) {

        context = applicationContext;
        environment = context.getEnvironment();

        if (context instanceof WebApplicationContext) {

            servletContext = ((WebApplicationContext) context).getServletContext();
        }

        messages = new MessageSourceAccessor(context, Locale.SIMPLIFIED_CHINESE);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {

        LOGGER.info("Spring context holder initialized successful");
    }
}

使用工具类调用Servcie,mapper

FTPConfig ftp = SpringContextHolder.getBean(FTPConfig.class);
AttendanceRecordRepository attendanceRecordRepository = SpringContextHolder.getBean(AttendanceRecordRepository.class);                

使用工具类获取属性

#直接获取
String isKeepHttpConnection = SpringContextHolder.getProperty("netty.http.connection");
#指定默认值
 Integer isKeepHttpConnection = Integer.valueOf(Optional.ofNullable(SpringContextHolder.getProperty("netty.http.connection")).orElse("1"));       

那么什么时候用工具类,什么使用加注解呢?个人见解,如果业务相关且数据库处理比重较多,可以用注解,如果非数据库处理比重大比如一些工具解析转换。那还是用工具类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值