zoukankan      html  css  js  c++  java
  • Spring静态属性的注入

    应用场景:工具类的静态方法使用了service注入

    1. xml的init-method方式

    <bean id="SecurityUtil" class="com.*.*.util.SecurityUtil" init-method="init">
            <property name="propertyConfigurerTmp" ref="propertyConfigurer"/>
    </bean>
        
    <bean id="propertyConfigurer"class="com.*.*.service.PropertyConfigurer"/>
    public class SecurityLogic {
        private PropertyConfigurer propertyConfigurerTmp;
        
    private static PropertyConfigurer propertyConfigurer;
    
        public void init() {
            SecurityLogic.propertyConfigurer = propertyConfigurerTmp;
        }
    
        public static void encrypt(String param) throws Exception {
            String encryptType=propertyConfigurer.getProperty("encryptType");
            //todo
        }
    }

    2. 注解@PostConstruct方式

    @Component
    public class SecurityLogic {
    
        @Autowired
        private PropertyConfigurer propertyConfigurerTmp;
        
        private static PropertyConfigurer propertyConfigurer;
    
        @PostConstruct
        public void init() {
            SecurityLogic.propertyConfigurer = propertyConfigurerTmp;
        }
    
        public static void encrypt(String param) throws Exception {
            String encryptType=propertyConfigurer.getProperty("encryptType");
            //todo
        }
    }

    3. set方法上面添加注解方式

    @Component
    public class SecurityLogic {
    
        private static PropertyConfigurer propertyConfigurer;
    
        @Autowired
        public void setPropertyConfigurer(PropertyConfigurer propertyConfigurer) {
            SecurityLogic.propertyConfigurer = propertyConfigurer;
        }
    
        public static void encrypt(String param) throws Exception {
            String encryptType=propertyConfigurer.getProperty("encryptType");
            //todo
        }
    }
  • 相关阅读:
    day01_02.php的开发环境准备
    day01_01.了解php
    day05_01 鸡汤+内容回顾
    河北省科技信息通用调查系统需求-----------开发日志---第一天
    开发项目注意事项总结
    JavaScript学习心得
    掌握需求过程读后感
    自我检讨
    安卓开发使用get请求想服务器发送数据
    对安卓移动应用开发的学习
  • 原文地址:https://www.cnblogs.com/duanhm234/p/7903884.html
Copyright © 2011-2022 走看看