.properties
ファイルの値を使用する際に問題があります。静的プロパティ値を注入する
マイmy-properties.properties
ファイルは次のようになります。
email.host=smtp.googlemail.com
email.port=465
その後、私の設定ファイルは次のようになります。
@Configuration
@PropertySource("classpath:my-properties.properties")
class MyProperties{
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
そして私は、この電子メールのクラスでそれを使用しようとしている:
@Component("MyProperties.class")
public class AutomatedEmail {
private String recipient;
private String fullName;
private String tempPassword;
private Email email;
@Value("email.from")
private String from;
...
public AutomatedEmail(){
}
public AutomatedEmail(final String recipient, final String fullName, final String tempPassword) throws EmailException {
this.recipient = recipient;
this.fullName = fullName;
this.tempPassword = tempPassword;
}
しかし、常にnullを返すように戻ってきます。また、Autowiredアプローチを試してみましたが、MyPropertiesクラスの電子メールオブジェクト全体を設定しましたが、コンストラクターを呼び出すとnullになります。
ありがとうございます!私はこれを修正し、 'my-properties.properties'には使用されている値が表示されます。しかし、私はまだコンストラクタを呼び出すと値がnullになるという問題にぶち当たっています。私が推測しているAutomatedEmailオブジェクトを作成すると、値がコンパイル時に割り当てられ、上書きされますか? – John
ああ、私はあなたがやっていることを参照してください! BeanはSpringを通じて管理する必要があります。 'new'キーワードを使ってそれをインスタンス化することはできません。私は私の答えを更新します。 – AndyN
ありがとうございました!間違いなくもっと読書とリファクタリングが必要です! – John