源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

零基础入门学习——Spring Boot注解(一)

  • 时间:2021-10-17 17:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:零基础入门学习——Spring Boot注解(一)
[b]声明bean的注解:[/b] @Component组件,没有明确角色的bean @Service,在业务逻辑层(service)中使用 @Repository,在数据访问层(dao)中使用 @Controller,在展现层中使用 @Configuration声明配置类 实体类无需添加注解,因为并不需要“注入”实体类 [b]指定Bean的作用域的注解:[/b] @Scope("prototype") 默认值为singleton 可选值prototype、request、session、globalSession [b]声明生成Bean的方法的注解:[/b] @Bean 用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean 使用AnnotationApplicationContext对象的getBean方法获取Bean [b]注入Bean的注解:[/b] @Autowired,自动注入(默认为byType型的注入),可以用在属性或者方法上,可以通过设置required = "false"说明不要求一定要注入有多个同样的接口的实现时,通过@qualifier区分 当注入的变量为List后者Map时,会把所有的接口实现都注入进来,key为Bean的名字,value为实现类对象。可以通过在实现类上添加@order=1来指定加载顺序,数越小越优先加载 @Lazy启动延迟注入 [b]配置类注解:[/b] @Configuration声明当前类是一个配置类,相当于Spring配置的一个xml文件 @ComponentScan,自动扫描配置类所在包名下的所有bean @EnableAutoConfiguration,启动自动配置 在spring boot中这三个注解可以用一个@SpringBootApplication替代 @EnableTransactionManagement,开启事务支持 [b]事务管理:[/b] @EnableTransactionManagement,加在配置类中,开启事务支持 @Transactional,加在Service的方法上,标注需要事务支持 [b]AOP注解:[/b] @AspectJ [b]任务调度:[/b]
@Scheduled用在需要定时执行的方法上
@EnableScheduling用在需要使用的入口类上
[b]Spring MVC集成:[/b] 首先需要对Application类进行修改
@SpringBootApplication
@EnableTransactionManagement
//1、添加继承SpringBootServletInitializer
public class Application extends SpringBootServletInitializer{
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
  @Override
  //2、重写configure方法
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return super.configure(builder);
  }
}
  Spring MVC的注解:
@Controller,在展现层使用
@ResponseBody
@RestController
以上所述是小编给大家介绍的Spring Boot注解学习(一),希望对大家有所帮助!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部