I have a text file which contain comma separated data which is the attribute of our bean.
e.g. name,age,gender,city,zipcode
We read the text file and we have a list which contain all the attribute. Here we need to create a dynamic Bean which contain the attribute based on that list which we get after reading text file, but we have different text files with different fields. So how should I create a dynamic bean which can contain the attributes according to the list which we will get after reading test file? Please give me some solution on this issue.
It isn't a dynamic Bean,
but I would use a HashMap:
HashMap<String, String> values = new HashMap<String, String>();
values.put("name", "Sebastian Blablabla");
values.put("city", "MyTown");
System.out.println(values.get("name"));
System.out.println(values.containsKey("city"));
System.out.println(values.containsKey("zipcode"));
Dynamic beans by Oracle use Maps too, look here:
http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0210dynamicbeans01.html
I would just Use a Super- Class; Just like, i dont know..
public class Item
Related
I have a property that looks like:
#Value(...)
Map<String, List<String>> classToMethods;
I want to populate this map via my application.yml in a Spring Boot application via the #Value("...") annotation. Some sample values are:
"java.lang.Double" => ["parseDouble", "toString"]
"java.lang.String" => ["toUpperCase"]
How do I represent this in my application.yml file? Also, I would like to put placeholder for environmentally injected values from our deployment tool. So for example, I currently have:
some:
property: ${ENV_VALUE:default_value_if_no_env}
Basically, this is the syntax I use for injecting values for the specific environment(value for ENV_VALUE varies for each environment). I am looking for a way to format the YML map such that this kind of environment replacements are also possible.
Alternative I am thinking of is just creating a String and manually parsing it during constructor injection but would like to avoid if possible.
I'm creating a microservice with Spring Boot that uses some properties loaded both with #Value and #ConfigurationProperties. Those properties will have default values defined in the application.yaml but I want to override them with environment variables. I've manage to do it with almost every basic property type but I'm unable with maps.
This is the code that retrieves the property, so the map is found in configuration.map:
#Data
#ConfigurationProperties(prefix = "configuration")
public class MapConfig{
private Map<String, Object> map;
}
The default values I have in the application.yaml are:
configuration:
map:
a: "A"
b: 2
c: true
This works fine and I get a map containing those key-value entries. Problem comes when I try to initialize the map with the environment variables. I've tried with multiples variants of CONFIGURATION_MAP='{aa:"aa", bb:12, cc:true}' and every time I start the application I get the default values defined in application.yaml but no trace of the environment map.
I also tried adding variables like CONFIGURATION_MAP_AA='HELLO' and I was able to put new values in my map. However all I can do is adding information but the default values I wrote in the yaml are still there. In this case I'm getting:
{
"aa": "HELLO",
"a": "A",
"b": 2,
"c": true
}
This isn't the behavior I'm looking for. I want to completely override the default map instead of adding new info. It also has the problem that keys added this way are always transformed to lowercase so I can't override keys with camelCase. And values are also casted to strings even when my map is <String,Object>.
Can anyone tell me how to properly initialize maps with environment variables or point me in the right direction? Thanks a lot!
The problem is the order in which the properties are been loaded.
Have a look at this doc that explains the loading order. I don't know if it will be
useful for your scenario but you can try creating a profile (for example application-other.yml) and load this. It will load after the application.yml
The cast is coming from the most general type in your list. In the end all three "name, 1, true" can be Strings. I can't think of a way of loading Object but you can recast your String on what you like ( if its needed. ) Else break it into multiple maps that are type specific ( Map<String,String> , Map<String,Boolean>, Map<String,Integer> )
I have a "details.yml" file, considering all the setting for getting all values from "yml.file" is done. But I am unable to store Map values into
"Map"
Here is my "details.yml" file below
details:
company:XYZ
values:
name: Manish
last: Raut
And in my class file i am able to get the values of "company" from yml file using #Value("${company}")
#Component
#EnableConfigurationProperties
#ConfigurationProperties(prefix = "details")
public class abcd() {
#Value("${company}")
String company;
#Value("${values}")
Map<String, String> values =new HashMap<String, String>();
...............................
}
i am not able to get those values in Mao which i created in this class, but i am getting values for "Company".
Help me with this?
Im the past, I added getter/setter for MAP type and it was work.
Did you try it? (getter/setter for 'values')
I'm not really sure what marshalling framework Spring uses behind the scenes and how it configures it (you could probably find out with some debugging and maybe make it work for your case), but you could always add an extra layer to your application and configure your own.
For instance you could use Jackson with yaml dataformat - https://dzone.com/articles/read-yaml-in-java-with-jackson.
I have properties file which has a key company.id like this
company.id=key1=value1,key2=value2,key3=value4
Now, I want to store key-value pair directly in map using #Value of spring
//For example,
//for key in properties file
//company.list=comp1,comp2
#Value("#{'${proposal.provience}'.split(',')}")
List<String> companyList;
the above will directly converts to List.
Please tell me what to write here without using other libraries like Guava
#value(some logic here)
HashMap<String,String> map;
I'm a jstl newbie so probably this question will sound to you funny.
Anyway, I have a model with a List as property and I'd like to fill this with a list of values (chosen from a list of checkboxes). I'm using the useBean tag in the form-processing jstl page, but doing this:
<jsp:useBean id='subscription' class='Subscription'>
<c:set target='${subscription}' property='priviledge' value='${param.priviledge}'/>
Where the property priviledge is a List and $param.priviledge the values of a series of checkboxes, I get a
javax.servlet.jsp.el.ELException: Attempt to convert String "ads" to type "[Ljava.lang.String;", but there is no PropertyEditor for that type
"ads" is one of the values I have selected. I thought the values of the priviledge field was already a List but it seems it works in a different way. I tried to iterate through the $param.priviledge object and I get all the values with no problem.
How can I use this list to fill a List?
Thanks for any help.
Roberto
Attempt to convert String "ads" to type "[Ljava.lang.String;"
This error suggests that the setter is setPriviledge(String[] arr), not a java.util.List.
The values in the param map are Strings; to get all the values as an array, use the paramValues map.
${paramValues.priviledge}
The property on the Subscription bean would need to be a String array:
private String[] priviledge;
public String[] getPriviledge() {
return priviledge;
}
public void setPriviledge(String[] priviledge) {
this.priviledge = priviledge;
}
(I don't know if you've simplified the code to post here, but you should not use the default package - many web servers won't like it and won't be able to instantiate your Subscription class.)