Sunday, 6 September 2015

Inject a java.util.Properties into a Spring Bean


Hard Coded

<bean id="adminUser" class="com.spring.InjectProperties">
       <!-- java.util.Properties -->
       <property name="emails">
              <props>
                     <prop key="admin">admin@nospam.com</prop>
                     <prop key="support">support@nospam.com</prop>
              </props>
       </property>
</bean>


From properties file

You can use “util:” namespace as well to create properties bean from properties file, and use bean reference for setter injection.

<util:properties id="emails" location="classpath:com/foo/emails.properties" />

Inject a property value into a Spring Bean using XML configurations

<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/>
<util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/>

Inject a property value into a Spring Bean using annotations

@Autowired+@Qualifier can double as by-name autowiring, but it's really meant for by-type autowiring with the ability to fine-tune the type.

Typically, @Autowired is used for by-type autowiring in Spring, and @Resource is used for by-name.

package com.abusecore.controller;
@Autowired
@Qualifier("serverProperties")
private Properties serverProperties;
@Autowired
@Qualifier("someConfig")
private Properties otherProperties;


OR

@Resource(name = "serverProperties")
private Properties serverProperties;
@Resource(name = "someConfig")
private Properties otherProperties;

Saturday, 5 September 2015

XML response with @ResponseBody annotation

@ResponseBody annotation is front of controller and annotates the object that we want in xml form.

Return xml response of Java object controller

To return an java object in xml form from an spring objects requires two simple configurations:
1) Annotate Object Class
2) Add @ResponseBody annotation to the controller's method


1) Annotate Object Class

This is a simple Java class (Student) whose object will be returned in xml form, from the controller.

@XmlRootElement defines the root element of xml structure; by default it takes class name as root element.

Optionally we can give its "name" parameter if we want some other name as xml root element.

@XmlElement defines internal xml nodes, 'name' attribute applies here as well, as same as with @XmlRootElement.

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;

@XmlRootElement(name = "student")
public class Student {
       private int id;
       private String firstName;
       private String lastName;
       private String email;
       private String phone;

       protected Student() {
       }

       public Student(int id, String firstName, String lastName, String email,
                     String phone) {
              this.id = id;
              this.firstName = firstName;
              this.lastName = lastName;
              this.email = email;
              this.phone = phone;
       }

       public int getId() {
              return id;
       }

       @XmlElement
       public void setId(int id) {
              this.id = id;
       }

       public String getFirstName() {
              return firstName;
       }

       @XmlElement
       public void setFirstName(String firstName) {
              this.firstName = firstName;
       }

       public String getLastName() {
              return lastName;
       }

       @XmlElement
       public void setLastName(String lastName) {
              this.lastName = lastName;
       }

       public String getEmail() {
              return email;
       }

       @XmlElement
       public void setEmail(String email) {
              this.email = email;
       }

       public String getPhone() {
              return phone;
       }

       @XmlElement
       public void setPhone(String phone) {
              this.phone = phone;
       }
}

2) Add @ResponseBody annotation to the controller's method

Second thing we need to do is to use '@ResponseBody' annotation against the controller's method.

This will make spring understand that method return value should be bound to the web response body.

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DataController {

       @RequestMapping("student")
       public @ResponseBody Student getStudent() {
              return new Student(1, "Rajesh", "Kumar", "rk@gmail.com","9528716691");
       }

       @RequestMapping("studentlist")
       public @ResponseBody StudentList getStudentList() {
              List<student> studentList = new ArrayList<student>();
              studentList.add(new Student(2,"awdh","dixit", "awdh@gmail.com","8287713093"));
              studentList.add(new Student(1,"Rajesh","Kumar", "rk@gmail.com","9528716691"));

              return new StudentList(studentList);
       }
}


Return Java object List as xml response from controller

Retrun the simple xml form of single java object is quite simple because there is full control to 'Student' class and hence we could annotate the class easily.

When we want to return a list of objects say 'ArrayList', but we don’t have ArrayList class's code to annotate it.
So it’s a bit tricky; we have to make another class having a list of objects and the object of that annotated class will represent the whole list and will be returned as a list of xml nodes.

import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student-list")
public class StudentList {

       private List<student> studentList;

       protected StudentList() {
       }

       public StudentList(List<student> studentList) {
              this.studentList = studentList;
       }

       @javax.xml.bind.annotation.XmlElement(name = "student")
       public List<student> getStudentList() {
              return studentList;
       }
}



Inject Java Collection in Spring


<list> : This helps in wiring i.e. injecting a list of values, allowing duplicates.

<set> : This helps in wiring a set of values but without any duplicates.

<map> : This can be used to inject a collection of name-value pairs where name and value can be of any type.

<props> : This can be used to inject a collection of name-value pairs where the name and value are both Strings.

<beans>
       <!-- Definition for javaCollection -->
       <bean id="myCollection" class="com.MyCollection">
             
             <!-- java.util.List -->
              <property name="stdList">
                     <list>
                           <value>INDIA</value>
                           <value>Pakistan</value>
                           <value>USA</value>
                           <value>UK</value>
                     </list>
              </property>

             <!-- java.util.Set -->
              <property name="dataSet">
                     <set>
                           <value>INDIA</value>
                           <value>Pakistan</value>
                           <value>USA</value>
                           <value>UK</value>
                     </set>
              </property>

             <!-- java.util.Map -->
              <property name="stdMap">
                     <map>
                           <entry key="1" value="INDIA" />
                           <entry key="2" value="Pakistan" />
                           <entry key="3" value="USA" />
                           <entry key="4" value="UK" />
                     </map>
              </property>

             <!-- java.util.Properties -->
              <property name="loginProperies">
                     <props>
                           <prop key="admin">admin@gmail.com</prop>
                           <prop key="support">support@gmail.com</prop>
                     </props>
              </property>
       </bean>

</beans>


Related Posts Plugin for WordPress, Blogger...