美文网首页
Spring 静态注入

Spring 静态注入

作者: simler | 来源:发表于2018-10-23 16:55 被阅读20次

[toc]

Spring 静态注入

一、 @Value静态注入方式

@Slf4j
@Component
public class ReplaceUtil {


    public static String env;

    @Value("${movie.env}")
    public void setEnv(String env) {
        this.env = env;
    }
    /**
     * @Author shadow
     * @param targetUrl
     */
    public static String appendHead(String targetUrl) {
        if (StringUtils.isEmpty(targetUrl)) {
            return null;
        }
        StringBuilder head = new StringBuilder();
        head.append("dev".equals(env) ? "http:" : "https:").append(targetUrl);
        return head.toString();
    }
}

二、注解@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
    }
}

三、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
    }
}

相关文章

网友评论

      本文标题:Spring 静态注入

      本文链接:https://www.haomeiwen.com/subject/urpazftx.html