Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用非Spring管理的静态方法初始化类_Java_Spring_Spring Boot - Fatal编程技术网

Java 使用非Spring管理的静态方法初始化类

Java 使用非Spring管理的静态方法初始化类,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个名为ConfigManagement的类,它只使用静态方法/字段。其中一个名为initializeConfig()的静态方法将属性对象(指向application.properties)作为输入,填充字段,并使用application.properties文件中的值调用其他一些方法 public class ConfigManagement { private static String signatureAlgorithm; private static String m

我有一个名为ConfigManagement的类,它只使用静态方法/字段。其中一个名为initializeConfig()的静态方法将属性对象(指向application.properties)作为输入,填充字段,并使用application.properties文件中的值调用其他一些方法

public class ConfigManagement {

   private static String signatureAlgorithm;
   private static String myName;
   private static RSAPublicKey myPublicKey;
   private static RSAPrivateKey myPrivateKey;
   private static HashMap<String, RSAPublicKey> peerPubKeys = new HashMap<String, RSAPublicKey>();
   private static boolean isInitialized = false;
/**
 * @return the signatureAlgorithm
 */

public static void initializeConfig(Properties props)  {
    signatureAlgorithm = props.getProperty("cybertrust.crypto.signatureAlgorithm");
    myName = props.getProperty("cybertrust.crypto.myName");
    try {
        try {
            myPublicKey = Loader.getPublicKeyFromCertificateFile(props.getProperty("cybertrust.crypto.myCertificate"));
        }
        catch (Exception e) {
            throw new IllegalStateException("cybertrust.crypto.myCertificate is empty, the file is not found or it contains invalid data");
        }
        try {
            myPrivateKey = Loader.getPrivateKeyFromFile(props.getProperty("cybertrust.crypto.myPrivateKey"));
        }
        catch (Exception e) {
            throw new IllegalStateException("cybertrust.crypto.myPrivateKey is empty, the file is not found or it contains invalid data");
        }
        peerPubKeys.put(myName, myPublicKey);
        int peerCounter = 0;
        do {
            String peerNameProp = String.format("cybertrust.crypto.peerModules.%d.name", peerCounter);
            String peerName = props.getProperty(peerNameProp);
            if (peerName == null)
                break;
            String peerNameCertFileProp = String.format("cybertrust.crypto.peerModules.%d.certificate", peerCounter);
            String peerNameCertFile = props.getProperty(peerNameCertFileProp);
            if (peerNameCertFile == null) // Do not halt the program, produce though an error
                Logger.getLogger("ConfigManagement").log(Level.SEVERE, 
                        String.format("Property %s not found while property %s is defined", peerNameCertFile, peerNameProp));
                // instantiate public key from file
                try {
                    RSAPublicKey peerRsaPubKey = Loader.getPublicKeyFromCertificateFile(peerNameCertFile); 
                    peerPubKeys.put(peerName, peerRsaPubKey);
                }
                catch (Exception e) {
                    Logger.getLogger("ConfigManagement").log(Level.SEVERE, 
                            String.format("File %s specified in property %s not found or does not contains a valid RSA key", peerNameCertFile, peerNameCertFileProp));              }
                peerCounter++;
        } while (true);
    }
    catch (Exception e) {
        throw(e);
    }
    if ((myPublicKey == null) || (signatureAlgorithm == null) || (myName == null))
        throw new IllegalStateException("one of the properties cybertrust.crypto.signatureAlgorithm, cybertrust.crypto.myName, cybertrust.crypto.myPublicKey, cybertrust.crypto.myPrivateKey is not defined");  
    isInitialized = true;
}

private static void testInitialized() {
    if (!isInitialized)
        throw new IllegalStateException("The configuration has not been initialized");
}

public static String getSignatureAlgorithm() {
    testInitialized();
    return signatureAlgorithm;
}
/**
 * @return the myName
 */
public static String getMyName() {
    testInitialized();
    return myName;
}
/**
 * @return the myPublicKey
 */
public static RSAPublicKey getMyPublicKey() {
    testInitialized();
    return myPublicKey;
}
/**
 * @return the myPrivateKey
 */
public static RSAPrivateKey getMyPrivateKey() {
    testInitialized();
    return myPrivateKey;
}

public static RSAPublicKey getPublicKey(String peerName) throws NoSuchElementException {
    testInitialized();
    RSAPublicKey result = peerPubKeys.get(peerName);
    if (result == null)
        throw new NoSuchElementException("No known key for module " + peerName);
    else
        return result;
}
}
在一个简单的Java项目中,我运行
ConfigManagement.initializeConfig(props)和字段已初始化,我可以使用其余的方法。春天可不是那么简单。
我试图将此代码集成到SpringBoot应用程序中,但我不知道如何/在何处初始化此类。
我发布Spring配置以供参考:

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.cybertrust.tms")
//@PropertySource({ "classpath:persistence-mysql.properties" })
@PropertySource({ "classpath:model.properties" })
public class DemoAppConfig implements WebMvcConfigurer {

    @Autowired
    private Environment env;

    private Logger logger = Logger.getLogger(getClass().getName());

    // define a bean for ViewResolver

    @Bean
    public DataSource myDataSource() {

        // create connection pool
        ComboPooledDataSource myDataSource = new ComboPooledDataSource();

        // set the jdbc driver
        try {
            myDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");        
        }
        catch (PropertyVetoException exc) {
            throw new RuntimeException(exc);
        }

        // for sanity's sake, let's log url and user ... just to make sure we are reading the data
        logger.info("jdbc.url=" + env.getProperty("spring.datasource.url"));
        logger.info("jdbc.user=" + env.getProperty("spring.datasource.username"));

        // set database connection props
        myDataSource.setJdbcUrl(env.getProperty("spring.datasource.url"));
        myDataSource.setUser(env.getProperty("spring.datasource.username"));
        myDataSource.setPassword(env.getProperty("spring.datasource.password"));

        // set connection pool props
        myDataSource.setInitialPoolSize(getIntProperty("connection.pool.initialPoolSize"));
        myDataSource.setMinPoolSize(getIntProperty("connection.pool.minPoolSize"));
        myDataSource.setMaxPoolSize(getIntProperty("connection.pool.maxPoolSize"));     
        myDataSource.setMaxIdleTime(getIntProperty("connection.pool.maxIdleTime"));

        return myDataSource;
    }

    private Properties getHibernateProperties() {

        // set hibernate properties
        Properties props = new Properties();

        props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
        props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        props.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

        return props;               
    }


    // need a helper method 
    // read environment property and convert to int

    private int getIntProperty(String propName) {

        String propVal = env.getProperty(propName);

        // now convert to int
        int intPropVal = Integer.parseInt(propVal);

        return intPropVal;
    }   

    @Bean
    public LocalSessionFactoryBean sessionFactory(){

        // create session factorys
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

        // set the properties
        sessionFactory.setDataSource(myDataSource());
        sessionFactory.setPackagesToScan(env.getProperty("hibernate.packagesToScan"));
        sessionFactory.setHibernateProperties(getHibernateProperties());

        return sessionFactory;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {

        // setup transaction manager based on session factory
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);

        return txManager;
    }


    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

    @Bean
    public ConfigManagement configManagement() {
        return new ConfigManagement();
    }


}
和弹簧靴主()


静态解决方案在Spring环境中无法正常工作,因为可以在Spring启动并加载所有bean和属性之前执行静态解决方案

您应该通过使用
@Value

使用@Value注释注入属性非常简单:

@值(${jdbc.url}”) 私有字符串jdbcUrl


为了将此代码集成到Spring项目中,我必须:

通过将类添加到我在问题中发布的配置文件中,使其成为Spring管理的bean,我添加了以下内容:

@Bean
public ConfigManagement configManagement() {
    return new ConfigManagement();
}
从类属性中删除
静态
声明,并按照@user7294900的建议,使用
@Value
注释从application.properties文件中初始化它们

但是,有些类属性不是基元类型,无法直接从application.properties初始化。他们需要在初始化时运行一些“业务逻辑”。为了实现这一点,我必须删除
静态
声明,并在
initializeConfig()
方法中添加
@PostConstruct
注释,该方法处理其余属性的初始化

public class ConfigManagement {

    @Value("${cybertrust.crypto.signatureAlgorithm}")
    private String signatureAlgorithm;
    @Value("${cybertrust.crypto.myName}")
    private String myName;
    @Value("${cybertrust.crypto.myCertificate}")
    private String myCertificate;
    @Value("${cybertrust.crypto.myPrivateKey}")
    private String myPrivateKey;
    private RSAPublicKey myRSAPublicKey;
    private RSAPrivateKey myRSAPrivateKey;
    private HashMap<String, RSAPublicKey> peerPubKeys = new HashMap<String, RSAPublicKey>();
    private boolean isInitialized = false;
    int peerCounter;
    /**
     * @return the signatureAlgorithm
     */

    public ConfigManagement() {

    }

    @PostConstruct
    public void initializeConfig() throws Exception  {

        try {
            try {
                myRSAPublicKey = Loader.getPublicKeyFromCertificateFile("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\" + myCertificate);
            }
            catch (Exception e) {
                throw new IllegalStateException("cybertrust.crypto.myCertificate is empty, the file is not found or it contains invalid data");
            }
            try {
                myRSAPrivateKey = Loader.getPrivateKeyFromFile("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\" + myPrivateKey);
            }
            catch (Exception e) {
                throw new IllegalStateException("cybertrust.crypto.myPrivateKey is empty, the file is not found or it contains invalid data");
            }
            peerPubKeys.put(myName, myRSAPublicKey);

            Properties props = loadProperties("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\src\\main\\resources\\application.properties");
            if (props == null) {
                throw new Exception("Properties file not found");
            }

            peerCounter = 0;
            do {
                String peerNameProp = String.format("cybertrust.crypto.peerModules.%d.name", peerCounter);

                String peerName = props.getProperty(peerNameProp);
                System.out.println("####TEST####\n" + peerNameProp + "\n" + peerName +"\n####TEST####");
                if (peerName == null)
                    break;
                String peerNameCertFileProp = String.format("cybertrust.crypto.peerModules.%d.certificate", peerCounter);
                String peerNameCertFile = props.getProperty(peerNameCertFileProp);
                System.out.println("####TEST####\n" + peerNameCertFileProp + "\n" + peerNameCertFile +"\n####TEST####");

                if (peerNameCertFile == null) // Do not halt the program, produce though an error
                    Logger.getLogger("ConfigManagement").log(Level.SEVERE, 
                            String.format("Property %s not found while property %s is defined", peerNameCertFile, peerNameProp));
                // instantiate public key from file
                try {
                    RSAPublicKey peerRsaPubKey = Loader.getPublicKeyFromCertificateFile("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\" + peerNameCertFile); 
                    peerPubKeys.put(peerName, peerRsaPubKey);
                }
                catch (Exception e) {
                    Logger.getLogger("ConfigManagement").log(Level.SEVERE, 
                            String.format("File %s specified in property %s not found or does not contains a valid RSA key", peerNameCertFile, peerNameCertFileProp));              }
                peerCounter++;
            } while (true);
        }
        catch (Exception e) {
            throw(e);
        }
        if ((myRSAPublicKey == null) || (signatureAlgorithm == null) || (myName == null))
            throw new IllegalStateException("one of the properties cybertrust.crypto.signatureAlgorithm, cybertrust.crypto.myName, cybertrust.crypto.myPublicKey, cybertrust.crypto.myPrivateKey is not defined");
        isInitialized = true;
        peerPubKeys.forEach((key, value) -> System.out.println(key + ":" + value));

    }
....

为什么要使用静态方法而不是使用
@Value
@user7294900注入属性,因为这段代码最初是为简单的Java应用程序编写的,现在我必须将它集成到这个Spring应用程序中。我将尝试重写代码,以便按照您的建议使用
@Value
,然后看看会发生什么。在ConfigManagement类中,我有一些不是基本类型的变量,如RSAPublicKey和HashMap。不能使用
@Value
初始化它们,而是通过以属性作为参数的方法调用来初始化它们。我应该如何初始化它们?@GiannisPappas我认为这是一个单独的问题,但您可以在spring组件中添加加载逻辑
@Bean
public ConfigManagement configManagement() {
    return new ConfigManagement();
}
public class ConfigManagement {

    @Value("${cybertrust.crypto.signatureAlgorithm}")
    private String signatureAlgorithm;
    @Value("${cybertrust.crypto.myName}")
    private String myName;
    @Value("${cybertrust.crypto.myCertificate}")
    private String myCertificate;
    @Value("${cybertrust.crypto.myPrivateKey}")
    private String myPrivateKey;
    private RSAPublicKey myRSAPublicKey;
    private RSAPrivateKey myRSAPrivateKey;
    private HashMap<String, RSAPublicKey> peerPubKeys = new HashMap<String, RSAPublicKey>();
    private boolean isInitialized = false;
    int peerCounter;
    /**
     * @return the signatureAlgorithm
     */

    public ConfigManagement() {

    }

    @PostConstruct
    public void initializeConfig() throws Exception  {

        try {
            try {
                myRSAPublicKey = Loader.getPublicKeyFromCertificateFile("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\" + myCertificate);
            }
            catch (Exception e) {
                throw new IllegalStateException("cybertrust.crypto.myCertificate is empty, the file is not found or it contains invalid data");
            }
            try {
                myRSAPrivateKey = Loader.getPrivateKeyFromFile("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\" + myPrivateKey);
            }
            catch (Exception e) {
                throw new IllegalStateException("cybertrust.crypto.myPrivateKey is empty, the file is not found or it contains invalid data");
            }
            peerPubKeys.put(myName, myRSAPublicKey);

            Properties props = loadProperties("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\src\\main\\resources\\application.properties");
            if (props == null) {
                throw new Exception("Properties file not found");
            }

            peerCounter = 0;
            do {
                String peerNameProp = String.format("cybertrust.crypto.peerModules.%d.name", peerCounter);

                String peerName = props.getProperty(peerNameProp);
                System.out.println("####TEST####\n" + peerNameProp + "\n" + peerName +"\n####TEST####");
                if (peerName == null)
                    break;
                String peerNameCertFileProp = String.format("cybertrust.crypto.peerModules.%d.certificate", peerCounter);
                String peerNameCertFile = props.getProperty(peerNameCertFileProp);
                System.out.println("####TEST####\n" + peerNameCertFileProp + "\n" + peerNameCertFile +"\n####TEST####");

                if (peerNameCertFile == null) // Do not halt the program, produce though an error
                    Logger.getLogger("ConfigManagement").log(Level.SEVERE, 
                            String.format("Property %s not found while property %s is defined", peerNameCertFile, peerNameProp));
                // instantiate public key from file
                try {
                    RSAPublicKey peerRsaPubKey = Loader.getPublicKeyFromCertificateFile("C:\\Users\\Findorgri\\git\\trust-management\\TMS-rest\\" + peerNameCertFile); 
                    peerPubKeys.put(peerName, peerRsaPubKey);
                }
                catch (Exception e) {
                    Logger.getLogger("ConfigManagement").log(Level.SEVERE, 
                            String.format("File %s specified in property %s not found or does not contains a valid RSA key", peerNameCertFile, peerNameCertFileProp));              }
                peerCounter++;
            } while (true);
        }
        catch (Exception e) {
            throw(e);
        }
        if ((myRSAPublicKey == null) || (signatureAlgorithm == null) || (myName == null))
            throw new IllegalStateException("one of the properties cybertrust.crypto.signatureAlgorithm, cybertrust.crypto.myName, cybertrust.crypto.myPublicKey, cybertrust.crypto.myPrivateKey is not defined");
        isInitialized = true;
        peerPubKeys.forEach((key, value) -> System.out.println(key + ":" + value));

    }
....
private static Properties loadProperties(String fileName) throws IOException {
        FileInputStream fis = null;
        Properties prop = null;
        try {
            fis = new FileInputStream(fileName);
            prop = new Properties();
            prop.load(fis);
        } catch(FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        } finally {
            fis.close();
        }
        return prop;
    }