couldnt resolve placeholder from the config

hey guys. i have this config:
bankAccounts:
- iban: "LT123"
cashRegId: 2
- iban: "LT345"
cashRegId: 2
bankAccounts:
- iban: "LT123"
cashRegId: 2
- iban: "LT345"
cashRegId: 2
and i have this config:
@Component
@Configuration
@Getter
@Setter
public class BankAccountConfig {
@Value("${bankAccounts}")
private List<BankAccount> bankAccounts;
}
@Component
@Configuration
@Getter
@Setter
public class BankAccountConfig {
@Value("${bankAccounts}")
private List<BankAccount> bankAccounts;
}
and the thing is that idk how to tell @Value that i want to have a list. bc rn i need to specify an element. intellij suggests me this: @Value("${bankAccounts[0].iban}") any help? thx
66 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Peter Rader
Peter Rader6mo ago
Hint: You dont need to quote IBANS Try this instead:
bankAccounts:
-
iban: LT123
cashRegId: 2
-
iban: LT345
cashRegId: 2
bankAccounts:
-
iban: LT123
cashRegId: 2
-
iban: LT345
cashRegId: 2
bambyzas
bambyzasOP6mo ago
wait. do i add tab, or space?
Peter Rader
Peter Rader6mo ago
NEVER TAB, spaces only! 😄
bambyzas
bambyzasOP6mo ago
thanks LD
JavaBot
JavaBot6mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
bambyzas
bambyzasOP6mo ago
u mean like that?
No description
Peter Rader
Peter Rader6mo ago
I would add another space in 53 and 54.
bambyzas
bambyzasOP6mo ago
but is it even possible to do what i want? i.e. inject a list of objects in my Config class?
bambyzas
bambyzasOP6mo ago
crap, still doesnt work
No description
Peter Rader
Peter Rader6mo ago
It should, is that the complete Config-class?
bambyzas
bambyzasOP6mo ago
No description
Peter Rader
Peter Rader6mo ago
Oh, you need getter and setter for the private-field. And you need the @ConfigurationProperties("xxx") on that class.
bambyzas
bambyzasOP6mo ago
on that class. on which one? on my BankAccountConfig or BankAccount?
Peter Rader
Peter Rader6mo ago
BankAccountConfig
bambyzas
bambyzasOP6mo ago
but i dont understand what to write between quotes
No description
Peter Rader
Peter Rader6mo ago
Could you create another namespace before the config bankAccounts? Maybe empty is ok.
bambyzas
bambyzasOP6mo ago
my application.yaml looks like this. im not sure i understand what u mean
No description
Peter Rader
Peter Rader6mo ago
I reproduced your problem and it works for me.
bambyzas
bambyzasOP6mo ago
so do i need to have @ConfigurationProperties annotation?
Peter Rader
Peter Rader6mo ago
Yes. I send you my working-example 1 Moment My main class:
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import jakarta.annotation.PostConstruct;

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigurationProperties(BankAccountConfig.class)
public class AppConfig {
public static void main(String... args) {
SpringApplication.run(AppConfig.class, args);
}

@Autowired
BankAccountConfig test;

@PostConstruct
public void post() {
System.out.println(test.getBankAccounts());
}
}
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import jakarta.annotation.PostConstruct;

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigurationProperties(BankAccountConfig.class)
public class AppConfig {
public static void main(String... args) {
SpringApplication.run(AppConfig.class, args);
}

@Autowired
BankAccountConfig test;

@PostConstruct
public void post() {
System.out.println(test.getBankAccounts());
}
}
My BankAccountConfig:
package test;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("payment-import.baltic-gateway")
public class BankAccountConfig {

public List<BankAccount> bankAccounts;

public List<BankAccount> getBankAccounts() {
return bankAccounts;
}

public void setBankAccounts(List<BankAccount> bankAccounts) {
this.bankAccounts = bankAccounts;
}
}
package test;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("payment-import.baltic-gateway")
public class BankAccountConfig {

public List<BankAccount> bankAccounts;

public List<BankAccount> getBankAccounts() {
return bankAccounts;
}

public void setBankAccounts(List<BankAccount> bankAccounts) {
this.bankAccounts = bankAccounts;
}
}
My BankAccount:
package test;

public class BankAccount {
private String iban;
private long cashRegId;

public void setIban(String iban) {
this.iban = iban;
}

public String getIban() {
return iban;
}

public void setCashRegId(long cashRegId) {
this.cashRegId = cashRegId;
}

public long getCashRegId() {
return cashRegId;
}

}
package test;

public class BankAccount {
private String iban;
private long cashRegId;

public void setIban(String iban) {
this.iban = iban;
}

public String getIban() {
return iban;
}

public void setCashRegId(long cashRegId) {
this.cashRegId = cashRegId;
}

public long getCashRegId() {
return cashRegId;
}

}
My Config:
paymentImport:
balticGateway:
bankAccounts:
-
iban: LT123
cashRegId: 2
-
iban: LT345
cashRegId: 2
paymentImport:
balticGateway:
bankAccounts:
-
iban: LT123
cashRegId: 2
-
iban: LT345
cashRegId: 2
Output:
2024-06-18T13:21:08.262+02:00 INFO 4932 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-06-18T13:21:08.262+02:00 INFO 4932 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 388 ms
[test.BankAccount@176996c3, test.BankAccount@411c6d44]
2024-06-18T13:21:08.312+02:00 INFO 4932 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [META-INF/resources/index.html]
2024-06-18T13:21:08.407+02:00 INFO 4932 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2024-06-18T13:21:08.262+02:00 INFO 4932 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-06-18T13:21:08.262+02:00 INFO 4932 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 388 ms
[test.BankAccount@176996c3, test.BankAccount@411c6d44]
2024-06-18T13:21:08.312+02:00 INFO 4932 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [META-INF/resources/index.html]
2024-06-18T13:21:08.407+02:00 INFO 4932 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
You realy need the @EnableConfigurationProperties(BankAccountConfig.class)!
bambyzas
bambyzasOP6mo ago
idk man. previously i had
@ConfigurationProperties(prefix="payment-import.baltic-gateway")
public class BankAccountConfig {
private List<BankAccount> bankAccounts;
@ConfigurationProperties(prefix="payment-import.baltic-gateway")
public class BankAccountConfig {
private List<BankAccount> bankAccounts;
and everything was fine. i didnt even need to have @EnableConfigurationProperties(BankAccountConfig.class)
Peter Rader
Peter Rader6mo ago
@EnableConfigurationProperties(BankAccountConfig.class) should go to the appconfig. Not the BankAccountConfig Do you have setter for BankAccounts in BankAccountConfig?
bambyzas
bambyzasOP6mo ago
idk its a good idea
No description
bambyzas
bambyzasOP6mo ago
nope, didnt need it bc BankAccount is:
@Getter
@Setter
public class BankAccount {
String iban;
Long cashRegId;
}
@Getter
@Setter
public class BankAccount {
String iban;
Long cashRegId;
}
Peter Rader
Peter Rader6mo ago
No no no, I did not meant the application.yml, I meant the AppConfigClass! You need to add @EnableConfigurationProperties(BankAccountConfig.class) to the application-config-java-class! I am pretty sure you need the setter. But I guess the setter is automatically generated by the @Setter annotation.
bambyzas
bambyzasOP6mo ago
yep
Peter Rader
Peter Rader6mo ago
Ok, did you add @EnableConfigurationProperties(BankAccountConfig.class) to the application-config-java-class?
bambyzas
bambyzasOP6mo ago
but i have like 5 config classes. why do i need to add only BankAccountConfig class to my application config java class?
Peter Rader
Peter Rader6mo ago
Hm, did you already have a @EnableConfigurationProperties in your application-config-java-class?
bambyzas
bambyzasOP6mo ago
nope. dont have it anywehere
Peter Rader
Peter Rader6mo ago
BankAccountConfig Is not a application-config-java-class by-the-way.
bambyzas
bambyzasOP6mo ago
i never said it is 😦
Peter Rader
Peter Rader6mo ago
Ah ok.
bambyzas
bambyzasOP6mo ago
np but i still dont get it
bambyzas
bambyzasOP6mo ago
No description
bambyzas
bambyzasOP6mo ago
why cant this work? why is Spring so against it?
bambyzas
bambyzasOP6mo ago
it IS a list of objects
No description
Peter Rader
Peter Rader6mo ago
Because bankAccounts are "configuration-properties" and not plain properties. Only @EnableConfigurationProperties(BankAccountConfig.class) allow spring to call the constructor of the class "BankAccount". The word "ConfigurationProperties" have a very special meaning.
bambyzas
bambyzasOP6mo ago
ok, but why did the old config versin work?
No description
Peter Rader
Peter Rader6mo ago
This works great, do not change that!
bambyzas
bambyzasOP6mo ago
i know it works, but i need to change it
Peter Rader
Peter Rader6mo ago
You might consider to remove the @Component. And you might consider to remove the prefix=. Why do you need to change it?
bambyzas
bambyzasOP6mo ago
i need to separate bank accounts. i want to have two separate lists. smth like this:
No description
bambyzas
bambyzasOP6mo ago
i have two banks: bank1 and bank2. and i want to have a list of accounts for each of them
Peter Rader
Peter Rader6mo ago
oh, I think I got your question wrong the whole time.
bambyzas
bambyzasOP6mo ago
doesnt matter. its ok
Peter Rader
Peter Rader6mo ago
-_-
bambyzas
bambyzasOP6mo ago
and now i need to structure my application.yaml and tweak my BankAccountConfig a little and idk how to do it properly
Peter Rader
Peter Rader6mo ago
paymentImport:
balticGateway:
bank1BankAccounts:
-
iban: LT123
cashRegId: 2
bank2BankAccounts:
-
iban: LT345
cashRegId: 2
paymentImport:
balticGateway:
bank1BankAccounts:
-
iban: LT123
cashRegId: 2
bank2BankAccounts:
-
iban: LT345
cashRegId: 2
This might work
bambyzas
bambyzasOP6mo ago
ok. but u see, i have this config
No description
bambyzas
bambyzasOP6mo ago
and in the 51st line there are accounts of bank1 id like to separate config and properties by the banks like in the 41st line idk if u understood me haha
Peter Rader
Peter Rader6mo ago
In line 43-45 the fields of bank1 do not match the fields in the class BankAccount
bambyzas
bambyzasOP6mo ago
ok, ill show u what i want to achieve
bambyzas
bambyzasOP6mo ago
i want to have this application.yml
No description
Peter Rader
Peter Rader6mo ago
ah, it is getting clearer.
bambyzas
bambyzasOP6mo ago
and in my BankAccountCOnfig i want to have everything like this:
public class BankAccountConfig {
@Value("${paymentImport.balticGateway.bank1.bankAccounts}")
private List<BankAccount> bank1BankAccounts;
@Value("${paymentImport.balticGateway.bank2.iban}")
private List<BankAccount> bank2BankAccounts;
}
public class BankAccountConfig {
@Value("${paymentImport.balticGateway.bank1.bankAccounts}")
private List<BankAccount> bank1BankAccounts;
@Value("${paymentImport.balticGateway.bank2.iban}")
private List<BankAccount> bank2BankAccounts;
}
but @Value("${paymentImport.balticGateway.bank1.bankAccounts}") doesnt work, because Spring wont (for some reason) accept list
Peter Rader
Peter Rader6mo ago
Are you sure you could use @Value in a class that have the @ConfigurationProperties annotation?
bambyzas
bambyzasOP6mo ago
im just showing you pseudocode what i want to do 😄
Peter Rader
Peter Rader6mo ago
Can you give an example of two bankacounts in line 7? The configuration looks like there is only one bank-account possible.
bambyzas
bambyzasOP6mo ago
"LT516126515919194" for example. doesnt matter tbh. why are you asking?
Peter Rader
Peter Rader6mo ago
You wrote this pseudocode:

@Value("${paymentImport.balticGateway.bank2.iban}")
private List<BankAccount> bank2BankAccounts;

@Value("${paymentImport.balticGateway.bank2.iban}")
private List<BankAccount> bank2BankAccounts;
but one iban is a string. String != List<BankAccount>
bambyzas
bambyzasOP6mo ago
huh? you asked me about 7th line not 9th oh, wiat so the question is still there
Peter Rader
Peter Rader6mo ago
I give up, I dont get your question. Maybe someone else can help.
bambyzas
bambyzasOP6mo ago
😄 maybe someone else can help me out?
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server