1. 程式人生 > >Springboot 靜態資源類裡靜態屬性注入

Springboot 靜態資源類裡靜態屬性注入

@Component
public class ADUserUtils {

    @Resource
    private  ADConfig adc;

    private  static ADConfig adc1;

    @PostConstruct
    public void init() {
        this.adc1 = adc;
    }

    public static List<User> getADUsers() {
        adc1.readConf();
}
@Configuration
@Component
public class ADConfig {

    @Value("${ad.username}")
    private  String adminName;

    @Value("${ad.password}")
    private  String adminPassword;

    @Value("${ad.ldapURL}")
    private  String ldapURL;

    @Bean
    public LdapContext readConf() {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");//"none","simple","strong"
        env.put(Context.SECURITY_PRINCIPAL, adminName);
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);
        env.put(Context.PROVIDER_URL, ldapURL);
        LdapContext ctx=null;
        try {
          ctx = new InitialLdapContext(env, null);
            System.out.println("222");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return ctx;
    }
}