SpringBoot鸡汤(注解集合)

1.(ConfigBean.java :是一个带有属性的bean类)

@Configuration
@ConfigurationProperties(prefix = “com.md”)
@PropertySource(“classpath:test.properties”)
public class ConfigTestBean {
private String name;
private String want;
// 省略getter和setter
} 

 有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个ConfigBean.java类,顶部需要使用注解:
@ConfigurationProperties(prefix = “com.dudu”):来指明使用哪个

添加@Configuration和@PropertySource(“classpath:test.properties”)后才可以读取test.properties文件里面的属性值。
@PropertySource:标注的属性源

2.Chapter2Application.java:是一个带有main()方法的类,用于启动应用程序(关键)。

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Chapter2Application {
public static void main(String[] args) {
    SpringApplication.run(Chapter2Application.class, args);
}
} 

@EnableConfigurationProperties:并指明要加载哪个bean,如果不写ConfigBean.class,在bean类那边添加
@SpringBootApplication:是Sprnig Boot项目的核心注解,主要目的是开启自动配置。
@RestController:注解等价于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出。

3.Controller类

@RestController
public class UserController {
    @Autowired
    ConfigBean configBean;
    @RequestMapping("/")
    public String hexo(){
        return configBean.getName()+configBean.getWant();
    }
}

@RequestMapping :注解提供路由信息。它告诉Spring任何来自”/”路径的HTTP请求都应该被映射到 home 方法。
@RestController: 注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者json格式数据。
@ImportResource :注解加载XML配置文件。
@Configuration:注解该类,等价 与XML中配置beans;
@Bean:标注方法等价于XML中配置bean
@Import: 注解可以用来导入其他配置类

@ComponentScan: 注解自动收集所有的Spring组件,包括 @Configuration 类。
例子:
@ComponentScan(basePackages = “com.hyxt”,includeFilters = {@ComponentScan.Filter(Aspect.class)})

原文地址:https://www.cnblogs.com/MaxElephant/p/8086494.html