プロパティスニペット。Application.propertiesから値を取得する方法は?
# Email (MailProperties)
spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
spring.mail.host= # SMTP server host. For instance `smtp.example.com`
spring.mail.jndi-name= # Session JNDI name. When set, takes precedence to others mail settings.
spring.mail.password= # Login password of the SMTP server.
spring.mail.port= # SMTP server port.
spring.mail.properties.*= # Additional JavaMail session properties.
spring.mail.protocol=smtp # Protocol used by the SMTP server.
spring.mail.test-connection=false # Test that the mail server is available on startup.
spring.mail.username= # Login user of the SMTP server.
some.prop = testingTesting
は、どのように私はJavaクラスでspringmail.protocolまたは複数のプロパティを取得することができますか?
試み
@Component
public class MyBean {
private final String prop;
@Autowired
public MyBean(@Value("${some.prop}") String prop) {
this.prop = prop;
System.out.println("================== " + prop + "================== ");
}
}
私は現在、私のapp.propertiesファイルから私のプロパティをプリントアウトするために、このメソッドを使用しています。私は1つの値以上の注釈を付けたい。
@value "{some.prop, second.prop, 3rd.prop}") String prop, propOne, Prop2
複数の値を呼び出すことは可能ですか?また、このクラスのプロパティをインスタンス化した後、別のクラスでこれらのプロパティを使用するにはどうすればよいですか?
「このクラスのプロパティをインスタンス化した後、別のクラスでこれらのプロパティを使用するにはどうすればよいですか?」 - それらをパラメータとして渡すことによって、通常の方法でクラス間でデータを渡します。 – eis
'MailProperties'を注入しようとしましたか? –
@eis一般的な/簡単な例を教えてもらえますか? myBean mb = new myBean(some.prop、second.prop); mb.getsome.prop? – Jesse