Posts

Showing posts from February, 2017

Spring Boot @ConfigurationProperties

application.yml prefix:     stringProp1: propValue1     stringProp2: propValue2     intProp1: 10     listProp:         - listValue1         - listValue2     mapProp:         key1: mapValue1         key2: mapValue2 application.properties prefix.stringProp1=propValue1 prefix.stringProp2=propValue2 prefix.intProp1=10 prefix.listProp[0]=listValue1 prefix.listProp[1]=listValue2 prefix.mapProp.key1=mapValue1 prefix.mapProp.key2=mapValue2 public class SamplePropertyLoadingTest {     @Value("${prefix.stringProp1}")     private String stringProp1; @ConfigurationProperties(prefix = "prefix") @Component public class SampleProperty {     private String stringProp1;     private String stringProp2;     @Max(99)     @Min(0)     private Integer intProp1;     private List listProp;     ...

Spring Advice Annotation Examples

package beans.annotation; import org.springframework.stereotype.Component; @Component public class Arithmatic { public int add(int a, int b){ return a+b; } public int sub(int a, int b){ return a-b; } public int mul(int a, int b){ return a*b; } public int div(int a, int b){ return a/b; } } package beans.annotation; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Required; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; @Component("cours") public class Course { @Value(value = "100") private int courseId; private String courseName; public int getCourseId() { return courseId; } public void setCourseId(int courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; }     @Value(value = "JAVA") @Required public void setCour...

Spring Core Examples

package main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import beans.annotation.Student; public class MainApplication { public static void main(String[] args) { AbstractApplicationContext  ac = new ClassPathXmlApplicationContext("config/hello.xml");   //setEmployeeName example /* Employee emp = (Employee)ac.getBean("employeeProp"); System.out.println(emp); emp.setEmpId(emp.getEmpId()+50); Employee employeeProp1 = (Employee)ac.getBean("employeeProp"); System.out.println(employeeProp1); Employee empCons = (Employee)ac.getBean("employeeCons"); System.out.println(empCons); Employee empCons1 = (Employee)ac.getBean("employeeCons1"); System.out.println(empCons1); //Constructor injection use index attribute to avoid constructor args ambiguity Employee empCons2 = (Employ...

Jersy Restful Example

package com; import java.util.ArrayList; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import pojo.Address; @Path("/aa") public class AllDemo { @GET @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) @Path("/bb") public Address demoOne() { return new Address(100,"Chennai","TN"); } @POST @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.TEXT_PLAIN}) @Path("/cc") public String demoTwo(Address address) { System.out.println(address.getState()); return " Welcome to " + address.getCity(); } @POST @Produces({MediaType.APPLICATION_JSON}) @Path("/dd") public List demoThree() { List list = new ArrayList(Address)(); Address address1 = new Address(10,"Chennai","TN"); Address address2 = new Address(20,"Ban...

Spring Boot MVC + Free Marker

package com.example; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class FTLHelloWorld { public static void main(String[] args) { //Freemarker configuration object Configuration cfg = new Configuration(); try { //Load template from source folder Template template = cfg.getTemplate("src/helloworld.ftl"); // Build the data-model Map(String, Object) data = new HashMap(String, Object)(); data.put("message", "Hello World!"); //List parsing List(String) countries = new ArrayList(String)(); countries.add("India"); countries.add("United States"); countries.add("...