@Component
public class MyBean {
public String getMessage () {
return "a message from MyBean";
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyWebController {
@Autowired
private MyBean myBean;
@RequestMapping("/")
@ResponseBody
public String theHandler () {
return myBean.getMessage();
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class EnabledAutoConfigExample {
public static void main (String[] args) {
SpringApplication.run(EnabledAutoConfigExample.class, args);
}
}
Output in browser
Just like @ComponentScan elements: basePackageClasses and basePackages, @SpringBootApplication annotation provides scanBasePackageClasses and scanBasePackages to be specified if the scan base path is not same as of this configuration class.
Example Project
Dependencies and Technologies Used:
Spring Boot 1.4.2.RELEASE Corresponding Spring Version 4.3.4.RELEASE
Spring Boot Web Starter : Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container.