Section 5 Flashcards
1
Q
Create a application that will use setters and getters using Spring container.
A
public class CricketCoach {
private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
//applicationContext.xml
//main method ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(file:src/main/java/com/springboot/demo/applicationContext.xml); CricketCoach coach = context.getBean("cricketCoach", CricketCoach.class); System.out.println(coach.getFirstName()); System.out.println(coach.getLastName());
context.close();
2
Q
Create a application where setter method values are taken from a text file
A
public class CricketCoach {
private String emailAddress; private String team; public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public String getTeam() { return team; } public void setTeam(String team) { this.team = team; }
}
//info.properties file content
foo. color=purple
foo. doorsNum=5
//applicationContext.xml file content context:property-placeholder location="file:src/main/java/com/mycompany/springdemo/sport.properties"
//main method public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/java/com/springboot/demo/applicationContext.xml");
CricketCoach coach = context.getBean("cricket", CricketCoach.class); System.out.println(coach.getEmailAddress()); System.out.println(coach.getTeam()); context.close(); }