Read a Map from properties file through applicationContext.xml in spring - spring

I have a map in my properties file as below:
property1=Hello_1
property2=Hello_2
list={a:'A', b:'B', c:'C'}
and I have a model object that contains:
public class Model {
String s1;
String s2;
Map<String, String> map;
}
and I want to read it from applicationContext.xml and fill it in my java Map like below:
<bean id="model" class="Model">
<property name="s1" value="${property1}" />
<property name="s2" value="${property2}" />
<property name="map" value="${list}" />
</bean>
I can read s1 and s2 successfully and fill it in the model, but I don't know how to fill the map collection, any idea?

You need to use Spring expression to handle that. Try <property name="map" value="#{${list}}" />

Related

spring new arrayList injection

how can i inject an empty (new) arraylist of "double/string/object" in spring ?
public class Pack{
private List<String> names;
private List<Course> courses;
private List<double> prices;
private int comp;
}
xml file
<bean id="pack" class="com.classes.Pack">
<property name="compt" value="0" />
<property name="courses" ref="courses" />
<property name="prices" ref="prices" />
<property name="names" ref="names" />
</bean>
You can inject an empty list like this, however this is probably unnecessary, unless you're trying to setup an example template spring XML config perhaps.
<property name="courses">
<list></list>
</property>

indexing document to a specific collection using spring data solr

I am trying to index a document to a specific collection in solr. The collection name is 'program'. I am using spring data solr.
I am getting the below error when trying to save the document:
HTTP ERROR 404
Problem accessing /solr/update.
Reason:Not Found
My assumption is that the annotation #SolrDocument is not recognized. spring-data-solr is trying to post the document to /solr/update whereas it should try to post it to /solr/program/update.However I am not sure how to prove it or fix it.
My schema is available on the link below:
http://<solr-host>/solr/program/schema
The update request handler is available in the link below:
http://<solr-host>/solr/program/update
spring-config-xml
<context:annotation-config />
<context:component-scan base-package="com.oostnet.controllers, com.oostnet.models, com.oostnet.services" />
<mvc:annotation-driven />
<solr:solr-server id="solrServer" url="http://<solr-host>/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
<solr:repositories base-package="com.oostnet.solr.repositories" />
Model definition:
package com.oostnet.models.documents;
#SolrDocument(solrCoreName="program")
public class Program {
#Id
#Indexed
private String id;
<more variables>
}
Repository definition:
package com.oostnet.solr.repositories;
public interface ProgramRepository extends SolrCrudRepository<Program, String> {
}
Controller:
package com.oostnet.controllers;
public class ProgramController{
private ProgramRepository programRepository;
#Autowired
public void setProgramRepository(ProgramRepository programRepository) {
this.programRepository = programRepository;
}
public void createProgram(Program program) {
programRepository.save(program);
}
}
Below are the versions used:
<spring.data.solr.version>1.3.2.RELEASE</spring.data.solr.version>
<spring.version>4.1.4.RELEASE</spring.version>
solr server version - solr-spec 4.10.2
To work on multiple collection you need to enable multiple repository under spring configuration as below :-
<context:annotation-config />
<context:component-scan base-package="com.test.solr" />
<solr:solr-server id="solrServer" url="http://localhost:8983/solr" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServerFactory" />
</bean>
<solr:repositories base-package="com.test.solr.repositories" multicore-support="true" />
<bean id="solrServerFactory"
class="org.springframework.data.solr.server.support.MulticoreSolrServerFactory">
<constructor-arg ref="solrServer" />
<constructor-arg name="cores">
<list>
<value>program</value>
</list>
</constructor-arg>
</bean>

Spring batch MongoItemReader to read as json

I am trying to write a Spring Batch job that reads documents from a mongo db and writes the documents to a CMS (for now I will attempt to test this first with WireMock). Can I set up the job and itemreader without specifying the exact structure of the document? I would just like to read each document as json and then sent that json through to the CMS. Is this even possible?
Since JSON is just a String, you should configure your MongoItemReader for String type and provide MongoTemplate with some custom simple converter:
public class DBObjectToStringConverter implements Converter<DBObject, String> {
public String convert(DBObject source) {
return source == null ? null : source.toString();
}
}
This one just return a String JSON representation of DBObject.
Then configuration:
<mongo:db-factory/>
<mongo:mapping-converter id="mappingConverter">
<mongo:custom-converters>
<mongo:converter>
<bean class="com.my.batch.mongo.DBObjectToStringConverter "/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory"/>
<constructor-arg ref="mappingConverter"/>
</bean>
<bean class="org.springframework.batch.item.data.MongoItemReader">
<property name="template" ref="mongoTemplate"/>
<property name="query" value="..."/>
<property name="targetType" value="java.lang.String"/>
</bean>
And voila! Each item returns as JSON String.

How load set of properties and then access it using spring framework

I have a spring web application. Let's say it is managing an animals zoo.
In my property file I am writing how much animals there are and for each animal its type and name as following :
animal.number = 2
animal.1.type= tall
animal.1.name= Simba
animal.2.type= small
animal.2.name= Pumba
Now i would like to use this property in my application using spring way. In my config.xml i could write something like following.
<bean id="animal" class="com.zoo.animalsManaging">
<property name="animalNumber" value="${animal.number}" />
<property name="animal1Name" value="${animal.1.name}" />
<property name="animal1Type" value="${ animal.1.type}" />
<property name="animal2Name" value="${animal.2.name}" />
<property name="animal2Type" value="${ animal.2.type}" />
</bean>
The problem is that animalNumber will always change. I am wondering if there is a spring way to get the entire animal property and then access to sub property by let's say animal.1.type.
As guided here you can use the util:properties
Animal properties
animal.1.type= tall
animal.1.name= Simba
animal.2.type= small
animal.2.name= Pumba
animal.number= 2
application.xml configuration
<context:property-placeholder location="classpath:animal.properties"/>
<bean name="zoo" class="com.stack.Zoo" init-method="init">
<property name="animalProperties">
<!-- not sure if location has to be customizable here; set it directly if needed -->
<util:properties location="${classpath:animal.properties}"/>
</property>
<property name="numberOfAnimals" value="${animal.number}" />
</bean>
Zoo is class Hold list of animals from Properties.
public class Zoo {
Properties animalProperties;
private Integer numberOfAnimals;
List<Animal> animals;
public void init() {
animals = new ArrayList<Animal>();
if (numberOfAnimals > 0) {
StringBuffer key = new StringBuffer();
for (int i = 1; i <= numberOfAnimals; i++) {
key.setLength(0);
key.append("animal.").append(i).append(".type");
String type = this.animalProperties.getProperty(key.toString());
key.setLength(0);
key.append("animal.").append(i).append(".name");
String name = this.animalProperties.getProperty(key.toString());
animals.add(new Animal(name,type));
System.out.println("Animals Added");
}
}
}
// Setter and getter Methods
}
In Post Construct , we are reading the properties and creating the Animal Instances.
numberOfAnimals value injected by Spring.

How to set map(master) values to bean in Spring? How to show in jsp?

I had stored in map for context-param values from web.xml at startup tomcat server time.
I want store map values to bean and populate jsp.
Thanks in Advance
Based on your comments, it looks as though you just need to expose the map on the model. Use a method annotated with #ModelAttribute in your controller, not #RequestMapping:
#Controller
public class YourController {
#Autowired
private ServletContext context;
#ModelAttribute("staticValues")
public Map<String, String> getStaticValues() {
Map<String, String> map = new HashMap<String, String>();
map.put("test1",context.getInitParameter("test1"));
map.put("test2",context.getInitParameter("test2"));
map.put("test3",context.getInitParameter("test3"));
map.put("test4",context.getInitParameter("test4"));
return map;
}
...
}
And then in the jsp you can do:
<c:out value="${staticValues.test1}" />
<c:out value="${staticValues.test2}" />
<c:out value="${staticValues.test3}" />
...etc...
One way of achieving this would be to set classA up in your Spring configuration with the appropriate property config:
<bean id="classA" class="some.package.ClassA">
<property name="propName" value="propValue"/>
...etc...
</bean>
You could then expose this in the view resolver configuration using the exposeContextBeansAsAttributes property:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
And then in any jsp, you could access the properties of the classA bean directly:
<c:out value="${classA.propName}"/>

Resources