Springboot常用注解

(1)@SpringBootApplication:声明该类是一个SpringBoot引导类,标注SpringBoot的启动类,该注解具备多种功能,是封装了spring注解的复合注解,包含@ComponentScan,@SpringBootConfiguration,@EnableAutoConfiguration

(2)@RestController :等价于@Controller + @ResponseBody

(3) @RequestMapping(value = “/hello”, method = RequestMethod.GET)
@PostMapping("/hello") :只处理POST请求
@DeleteMapping("/hello") :只处理DELETE请求
@PutMapping("/hello") :只处理PUT请求
@GetMapping("/hello") :只处理GET请求

(4)1. @ComponentScan(包扫描)
• component是组件,scan是扫描,所以这个注解的含义就是用来扫描组件的,componentScan 扫描当前包及其子包下被 @Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理,所以这个注解会自动注入所有在主程序所在包下的组件。默认把当前启动类所在的包作为扫描包的起点。
• 相当于:

<context:component-scan base-package="com.xxx"></context:component-scan>
  1. @SpringBootConfiguration : 表示当前类具有配置类的作用
  2. @EnableAutoConfiguration:自动配置
    o 根据当前引入的依赖包,猜测需要创建的工程类型,以及工程中有可能创建的对象,根据猜测自动创建工程所需的相关实例bean程序启动,会自动加载扫描所有 classpath:/META-INF/spring.factories文件 ,并且创建对应实例

(5)注:Spring创建Bean三种声明方式

  1. 标签
<bean id=user” class=”com.atguigu.bean.User”></bean>
  1. 组件注解
    @Component @Controller @Service @Repository
  2. 自定义配置类
@Configuration
public class AppConfig {
    @Bean
    public User user(){
return new User();
    }
}

(6)@Mapper :标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中

(7)/**

  • @MapperScan(basePackages = “com.atguigu.dao”)
  • 扫描指定包下的所有Mapper接口,将动态代理的实现类对象注入Spring容器中
  • basePackages属性:指定扫描的包路径地址
  • 作用相当于:
* <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 *     <property name="basePackage" value="com.atguigu.dao"/>
 * </bean>
 */
@MapperScan("com.atguigu.dao")
@SpringBootApplication
public class SpringBoot03Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot03Application.class, args);
}
}

(8)@RunWith(SpringRunner.class)
@SpringBootTest :junit4测试

public class MapperTest {
 
    @Autowired
    private UserService userService;
 
    @Test
    public void testUser(){
        List<User> users = userService.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
 
}

(9)@ConfigurationProperties(prefix = “spring.datasource”) //将数据库连接信息直接封装到数据源对象中

@Configuration
public class AppConfig {
@ConfigurationProperties(prefix = "spring.datasource") //将数据库连接信息直接封装到数据源对象中
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("stat");
return dataSource;
}
}

(10)1. 扫描Dao接口,需要在主程序类中增加扫描注解 @MapperScan(“com.atguigu.**.dao”)及事务管理@EnableTransactionManagement
2. 传统的SSM架构中采用的是声明式事务,需要在配置文件中增加AOP事务配置,Spring Boot框架中简化了这种配置,可以在Service接口中增加注解@Transactional

(11)@EnableScheduling :在主程序类名上添加注解:定时任务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值