application.properties //默认为application.properties
#Server server.port=8090 #需要加的URL前缀 server.context-path=/v1 #LOGGING logging.pattern.level=INFO Test.id=8090 Test.name=xing
package com.cesmart; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import com.cesmart.entity.TestBean; //@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan //@Configuration // @EnableAutoConfiguration //@ComponentScan(basePackages = "com.cesmart.config") //扫描那些包得到bean @ComponentScan(basePackages = "com.cesmart") //扫描那些包得到bean,@ComponentScan({"com.teradata.notification","com.teradata.dal"}) public class Application { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(Application.class, args); TestBean testBean = (TestBean) applicationContext.getBean("testBean"); System.out.println("TestBean == " + testBean.toString()); } }
package com.cesmart.entity; public class TestBean { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "TestBean [id=" + id + ", name=" + name + "]"; } public TestBean(String id, String name) { super(); this.id = id; this.name = name; } }
package com.cesmart.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.cesmart.entity.TestBean; @Configuration // 相当于定义XML文件 @ConfigurationProperties(prefix = "Test") // 所需字段以什么开头的 public class TestConfig { private String id; // 需要它的set方法才可以进行Properties文件内容的引入 private String name; @Bean(name = "testBean") // 相当于XML里配置bean public TestBean testBean() { TestBean client = new TestBean(id, name); return client; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.cesmart.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController //返回的是body的数据,不是视图名称,//相当于@ResponseBody + @Controller合在一起的作用。 public class WebTest { @RequestMapping("/webTest") //http://localhost:8090/webTest访问到它 public String webTest(){ System.out.println("webTest"); return "webTest"; } }