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
JavaBot8mo 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 Rader8mo 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
i hate SQL so much its unreal
wait. do i add tab, or space?
Peter Rader
Peter Rader8mo ago
NEVER TAB, spaces only! 😄
i hate SQL so much its unreal
thanks LD
JavaBot
JavaBot8mo 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.
i hate SQL so much its unreal
u mean like that?
No description
Peter Rader
Peter Rader8mo ago
I would add another space in 53 and 54.
i hate SQL so much its unreal
but is it even possible to do what i want? i.e. inject a list of objects in my Config class?
i hate SQL so much its unreal
crap, still doesnt work
No description
Peter Rader
Peter Rader8mo ago
It should, is that the complete Config-class?
Peter Rader
Peter Rader8mo ago
Oh, you need getter and setter for the private-field. And you need the @ConfigurationProperties("xxx") on that class.
i hate SQL so much its unreal
on that class. on which one? on my BankAccountConfig or BankAccount?
Peter Rader
Peter Rader8mo ago
BankAccountConfig
i hate SQL so much its unreal
but i dont understand what to write between quotes
No description
Peter Rader
Peter Rader8mo ago
Could you create another namespace before the config bankAccounts? Maybe empty is ok.
i hate SQL so much its unreal
my application.yaml looks like this. im not sure i understand what u mean
No description
Peter Rader
Peter Rader8mo ago
I reproduced your problem and it works for me.
i hate SQL so much its unreal
so do i need to have @ConfigurationProperties annotation?
Peter Rader
Peter Rader8mo 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)!
i hate SQL so much its unreal
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 Rader8mo ago
@EnableConfigurationProperties(BankAccountConfig.class) should go to the appconfig. Not the BankAccountConfig Do you have setter for BankAccounts in BankAccountConfig?
i hate SQL so much its unreal
idk its a good idea
No description
i hate SQL so much its unreal
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 Rader8mo 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.
Peter Rader
Peter Rader8mo ago
Ok, did you add @EnableConfigurationProperties(BankAccountConfig.class) to the application-config-java-class?
i hate SQL so much its unreal
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 Rader8mo ago
Hm, did you already have a @EnableConfigurationProperties in your application-config-java-class?
i hate SQL so much its unreal
nope. dont have it anywehere
Peter Rader
Peter Rader8mo ago
BankAccountConfig Is not a application-config-java-class by-the-way.
i hate SQL so much its unreal
i never said it is 😦
Peter Rader
Peter Rader8mo ago
Ah ok.
i hate SQL so much its unreal
np but i still dont get it
i hate SQL so much its unreal
why cant this work? why is Spring so against it?
i hate SQL so much its unreal
it IS a list of objects
No description
Peter Rader
Peter Rader8mo 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.
i hate SQL so much its unreal
ok, but why did the old config versin work?
No description
Peter Rader
Peter Rader8mo ago
This works great, do not change that!
i hate SQL so much its unreal
i know it works, but i need to change it
Peter Rader
Peter Rader8mo ago
You might consider to remove the @Component. And you might consider to remove the prefix=. Why do you need to change it?
i hate SQL so much its unreal
i need to separate bank accounts. i want to have two separate lists. smth like this:
No description
i hate SQL so much its unreal
i have two banks: bank1 and bank2. and i want to have a list of accounts for each of them
Peter Rader
Peter Rader8mo ago
oh, I think I got your question wrong the whole time.
i hate SQL so much its unreal
doesnt matter. its ok
Peter Rader
Peter Rader8mo ago
-_-
i hate SQL so much its unreal
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 Rader8mo 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
i hate SQL so much its unreal
ok. but u see, i have this config
No description
i hate SQL so much its unreal
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 Rader8mo ago
In line 43-45 the fields of bank1 do not match the fields in the class BankAccount
i hate SQL so much its unreal
ok, ill show u what i want to achieve
i hate SQL so much its unreal
i want to have this application.yml
No description
Peter Rader
Peter Rader8mo ago
ah, it is getting clearer.
i hate SQL so much its unreal
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 Rader8mo ago
Are you sure you could use @Value in a class that have the @ConfigurationProperties annotation?
i hate SQL so much its unreal
im just showing you pseudocode what i want to do 😄
Peter Rader
Peter Rader8mo ago
Can you give an example of two bankacounts in line 7? The configuration looks like there is only one bank-account possible.
i hate SQL so much its unreal
"LT516126515919194" for example. doesnt matter tbh. why are you asking?
Peter Rader
Peter Rader8mo 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>
i hate SQL so much its unreal
huh? you asked me about 7th line not 9th oh, wiat so the question is still there
Peter Rader
Peter Rader8mo ago
I give up, I dont get your question. Maybe someone else can help.
i hate SQL so much its unreal
😄 maybe someone else can help me out?
JavaBot
JavaBot8mo 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.

Did you find this page helpful?