springboot[2]_常用注解

介绍一些常用注解。

springBoot因为很少用到xml来配置bean文件,所以大部分都是用注解来创建和管理相关bean。

@Component、 @Service、 @Repository 和 @Controller这几个注解效果都是差不多的,都是可以用来创建bean实例的。只是规定使用的方法不同而已。

1. @Component

自动被component扫描

2. @Controller

用于web层

3. @Service

用于service层

4. @Repository

用于数据存储层

5. @Bean

bean注解用于告诉方法,产生一个Bean对象,然后交由Spring容器管理。 多用于@Configuration注解修饰的配置类中使用。

6. @Configuration

告诉SpringBoot修饰的类是一个配置类。@Bean修饰的animal即bean名称。

public class Animal {
    private String name;

    public Animal() {
    }
    
    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

配置类:

@Configuration
public class MyConfig {
    @Bean
    public Animal animal(){
        return new Animal("cat");
    }
}

启动:

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);

        Animal animal1 = context.getBean(Animal.class);
        System.out.println(animal1.getName());
        Animal animal2 = (Animal) context.getBean("animal");
        System.out.println(animal2.getName());
        System.out.println(animal1 == animal2);
    }
}

输出:

cat
cat
true

@Bean和@Configuration配合使用,@Bean修饰的animal方法,将Animal也添加到spring的管理中。重复调用getBean(),获得的也都是同一个对象。

7. @Import

可以手动导入组件。默认调用无参构造器创建类。

@Import(Animal.class)
@Configuration
public class MyConfig {
}

8. @Conditional

条件装配。可以根据各种情况来装配组件。

注解

例如 ConditionalOnBean ,就是在bean存在的情况下再来装配所修饰的组件。

我建了一个和Animal类似的类,Plant。

@ConditionalOnBean:

@Configuration
public class MyConfig {
    @Bean
    public Plant plant() {
        return new Plant("flower");
    }

    @Bean
    @ConditionalOnBean(Plant.class)
    public Animal animal() {
        return new Animal("cat");
    }
}
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);

        Animal animal = context.getBean(Animal.class);
        System.out.println(animal.getName());
    }
}

输出:

cat

如果把这plant注册的代码删掉,在获取Animal的时候则会报bean不存在的错

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.yt.myboot.entity.Animal' available

9. @ImportResource

导入xml配置,相当于利用以前的xml来导入bean组件。

@Configuration
@ImportResource("classpath:xxx.xml")
public class MyConfig {
}

10. @ConfigurationProperties

将properties配置的内容绑定到javaBean的属性中。

首先,我们先将之前写的@Configuration中自动注册Animal的方法去掉。

然后,给Animal类再添加一个属性age。在使用ConfigurationProperties之前要保证对象是spring容器中的组件,所以要加一个@Component注解。prefix是Properties配置文件中的前缀。

@Component
@ConfigurationProperties(prefix = "animal")
public class Animal {
    private String name;

    private Integer age;

    public Animal() {
    }

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

application.properties:

animal.name=cat
animal.age=3

测试:

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);

        Animal animal = context.getBean(Animal.class);
        System.out.println(animal.getName() + ":" + animal.getAge());
    }

输出:

cat:3

11. @EnableConfigurationProperties

启动配置注解。作用是使得用 @ConfigurationProperties 注解的类生效。

前面说过,使用@ConfigurationProperties注解,得要保证对象是spring容器中的组件,所以要加上@Component注解。

但是,如果不想要加@Component注解也可以,把@Component去掉,然后再配置类上加上@EnableConfigurationProperties,标明是哪一个类即可。

@ConfigurationProperties(prefix = "animal")
public class Animal {
	......
}

配置类:

@Configuration
@EnableConfigurationProperties(Animal.class)
public class MyConfig {
}

输出一样是

cat:3
posted @   Aeons  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示