SpringBoot-从新建一个项目看起

官方文档-using-boot

pom.xml

spring-boot-starter-parent下声明了spring-boot-dependencies,即SpringBoot已配置的依赖。依赖里明确用properties声明依赖的默认导入版本。没在properties声明的依赖需要我们自己写依赖的
starter这个词叫启动器,不同场景下有不同启动器。在Spring Boot application starters中查看。

启动类

SpringBoot是一个web项目,使用注解@SpringBootApplication标识SpringBoot的主配置类。主配置类里有一个main(),用于启动程序。

注解

spring-core
配置文件声明,相当于bean.xml @Configuration
声明bean(方法内new Class) @Bean
@Component(通用)= @Repository(dao层) = @Service(service层) =@Controller(controller层)

@Component(通用)+@ComponentScan=@Configuration+@Bean
@Component注解用于类。使用类路径扫描自动配置bean。注释类和bean之间存在隐式的一对一映射(即每个类一个bean)
@Bean注解用于方法。方法返回值是对象,可以在方法中为对象设置属性。Spring的Starter机制,就是通过@Bean注解来定义bean。
classes annotated with @Component, @Controller, etc, or @Bean methods in Java-based @Configuration classes.

bean作用域 @Scope
注入(从容器中取出)bean @Autowired
属性不可空 @Nullable
属性值为外部值 @Value is typically used to inject externalized properties:@Value("${catalog.name}") String catalog @ImportResource("classpath:/com/acme/properties-config.xml")
@Import the @Import annotation allows for loading @Bean definitions from another configuration class。@Import(ConfigA.class) 只需要ClassB需要显示声明容器。ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);

切面
@Aspect
@Around("methodsToBeProfiled()")
@Pointcut("execution(public * foo...(..))")

控制器
@RestController=@Controller+@ResponseBody.

原文地址:https://www.cnblogs.com/laidianjilu/p/13661395.html