and springboot spring, how to use the automatic injector in the static properties of the method in

The first step: Write notes @Component the current class as a bean object. (@ Controller, @ service will do)

Step Two: Write a static variable

The third step: write a comment @PostConstruct annotation methods annotated In this method, the injection will automatically assign values ​​to the static variables defined

Step Four: static variable substitution used in the automatic injector inside the static method

@Component
public class DSHWechatApiUtil extends DSHBaseController {

    @Autowired
    private IThirdPartyAuthDao thirdPartyAuthDao; @Autowired private static IThirdPartyAuthDao staticThirdPartyAuthDao; @PostConstruct public void init() { staticThirdPartyAuthDao = thirdPartyAuthDao; } public static JSONObject getAuthorizerToken(String componentAccessToken, String authorizerAppid, String authorizerRefreshToken) { JSONObject returnObject = new JSONObject(); try { if (DSHUtils.isEmpty(componentAccessToken)) { componentAccessToken = staticThirdPartyAuthDao.selectWechatValue(DSHConstants.WECHAT_PARAMS.COMPONENT_ACCESS_TOKEN); } } catch (Exception e) { e.printStackTrace(); } return returnObject; } }

 

@PostConstruct Notes Role: Java EE 5 annotations is introduced, Spring allows developers to use it in a managed Bean. When the DI container tube Bean is instantiated by the current method @ PostConstruct annotations are automatically triggered to complete some initialization.

note:

 

  • Only one way to use this annotation notes;
  • Annotated method without any parameters;
  • The method is returned annotated void;
  • Annotated method must not throw checked exceptions;
  • The method is non-static method for an annotated;
  • This method will only be executed once;

Guess you like

Origin www.cnblogs.com/AllIhave/p/11590392.html