Spring Boot Example.
Create Spring Boot Example.
There are three way creating spring boot
1. Create project from spring.io
Create Spring Boot Example.
There are three way creating spring boot
1. Create project from spring.io
2. Install Spring from the command line
c:\> spring init -d=web my-app
3. Project Snapshot
4. DemoApplication Class
package com.example.springBootExample;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import com.abc.foo.User;
@ComponentScan({"com.abc.foo","com.example.springBootExample"})
@SpringBootApplication
public class DemoApplication {
@Bean
public User user(){
return new User("Chandra","Gauro");
}
public static void main(String[] args) {
ApplicationContext ctx=SpringApplication.run(DemoApplication.class, args);
String []names=ctx.getBeanDefinitionNames();
Arrays.sort(names);
System.out.println("===============");
for(String name:names){
System.out.println(name);
}
System.out.println("=========******======");
System.out.println(ctx.getBean("user").toString());
}
}
5. PageController Class
package com.example.springBootExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.abc.foo.NotificationService;
@RestController
public class PageController {
//using default
//@Autowired
private NotificationService notificationService;
//using setter
// @Autowired
// public void setNotificationService(NotificationService notificationService) {
// this.notificationService = notificationService;
// }
//using Constructor
@Autowired
public PageController(NotificationService notificationService) {
this.notificationService = notificationService;
}
@RequestMapping("/")
public String home(){
return notificationService.toString();
//return "Hello, Spring Boot !";
}
}
6.NotificationService class
package com.abc.foo;
import org.springframework.stereotype.Service;
@Service
public class NotificationService {
public NotificationService() {
// TODO Auto-generated constructor stub
}
public void send(){
}
public void sendAsync(){
}
@Override
public String toString(){
return "NotificationService from CKG from constructor()";
}
}
7. User Class
package com.abc.foo;
public class User {
private String firstName;
private String lastNmae;
private String emailAddress;
public User(String firstName, String lastNmae) {
this.firstName = firstName;
this.lastNmae = lastNmae;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastNmae() {
return lastNmae;
}
public void setLastNmae(String lastNmae) {
this.lastNmae = lastNmae;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@Override
public String toString() {
return "User [firstName=" + firstName + ", lastNmae=" + lastNmae + ", emailAddress=" + emailAddress + "]";
}
}
8. Change port
server.port = 9009
Comments
Post a Comment