配置文件配置好 了之后可以进行第一个例子的编写了!
@Controller
@EnableAutoConfiguration()
public class SampleController
{
private Hello hello;
@Autowired
public SampleController(Hello hello)
{
this.hello = hello;
}
@RequestMapping("/")
@ResponseBody
String home(String type)
{
System.out.println("login:" + hello);
return "Hello World";
}
public static void main(String[] args) throws Exception
{
SpringApplication app = new SpringApplication(SampleController.class);
app.setWebEnvironment(true);
app.setShowBanner(false);
Set<Object> set = new HashSet<Object>();
set.add("classpath:applicationContext.xml");
app.setSources(set);
app.run(args);
}
}
右键以java application方式运行,在浏览器上输入:localhost:8080/
显示
说明配置成功了,那么下面对程序简单的分析下:
@Controller 是springMVC使用的注解,标识这个类为控制层类
@EnableAutoConfiguration() springboot使用的注解,必须
@RequestMapping("/") 请求的url地址,缺省为方法名称
@ResponseBody 以json格式返回数据
Set<Object> set = new HashSet<Object>();
set.add("classpath:applicationContext.xml");
自定义spring配置文件的位置,因为springboot使用的是默认的位置,这里可以根据实际情况修改!