Announcement Announcement Module
Collapse
No announcement yet.
@Value and ArrayList Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Value and ArrayList

    Hi,

    I want to create an array list from the properties object. I have set up my configuration as follows:

    <beans>

    ......
    <utilroperties id="testProperties" location="database.properties"></utilroperties>
    ......

    </beans>

    I have then tried to create a ArrayList by doing the following please let me know if this is the correct way:

    Code:
    package test;
    
    import java.util.ArrayList;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DatabaseProperties {
    	
    	private @Value("#{testProperties['dog']}")String url;
    	private @Value("#{testProperties['jdbc.username']}")String username;
    	private @Value("#{testProperties['adnan*']}")ArrayList<String> values;
    	
    	@Bean(name="databaseParameters")
    	public DatabaseParameters databaseParameters(){
    		
    		System.out.println(values.toString() + "DOG");
    		
    		DatabaseParameters dp = new DatabaseParameters();
    		dp.setUrl(url);
    		dp.setUsername(username);
    		dp.setArrayList(values);
    		
    		
    		ArrayList<String> al = dp.getArrayList();
    		
    		for(String value: al){
    			System.out.println(value);
    		}
    		
    		return dp;
    		
    	}
    	
    	class DatabaseParameters{
    		
    		private String url;
    		private String username;
    		private ArrayList<String> arrayList;
    		
    		public String getUrl() {
    			return url;
    		}
    		public void setUrl(String url) {
    			this.url = url;
    		}
    		public String getUsername() {
    			return username;
    		}
    		public void setUsername(String username) {
    			this.username = username;
    		}
    		public ArrayList<String> getArrayList() {
    			return arrayList;
    		}
    		public void setArrayList(ArrayList<String> arrayList) {
    			this.arrayList = arrayList;
    		}
    		
    	}
    }

  • #2
    Hello,

    Not sure that you can use wild card like that to match property names.

    You could have one property where you have multiple vales as a comma separated list.
    Code:
    extra=AAA,BBBB,CCC
    and then use
    Code:
    	private @Value("#{testProperties['extra'].split(',')}")List<String> extraValues;
    Observe that there is no type converter from String[] to ArrayList so you will have to use java.util.List

    Hope this helps you.
    // Pontus

    Comment

    Working...
    X