SpringBoot的常用注解的讲解

SpringBoot 的注解的简单讲解

Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。

简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题—习惯大于约定。
我们从入口类开始讲解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication// 这个注解表示 SpringBoot的启动类 
public class Demo {
	public static void main(String[] args) {
		SpringApplication.run(Demo.class, args);//springBoot的核心启动方法
	}
}

接下来我们,就说一下SpringBoot 的常用注解及其注解之间的区别
@Configuration注解可以用java代码的形式实现spring中xml配置文件配置的效果。
下面我们用代码表示一下

@Configuration//声明这是一个配置类,当然这个注解的使用是跟随使用者来不同的使用的
public class ConfigTest {

    @Bean//@Bean 注解就表示 之前SpringXML的bean 的标签
    public HelloService getHello(){
        HelloService helloService = new HelloServiceImpl();
        return helloService;
    }
    
	@PostConstruct
    public void execute(){
        System.out.println("bbb");
    }
}

这个注解可以配合Java提供给我们的@PostConstruct 注解来实现在SpringBoot全部启动之前来执行一些方法
当然@Component这个注解配合@PostConstruct 也是可以的
@Component
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用来标注这个类

@Component
public class ComponentTest {
    @PostConstruct
    public void aaa(){
        System.out.println("tou");
    }
}

自我感觉的使用上@Component与@Configuration的使用在宏观上是一样的
为什么,这么说呢?原因就在于在@Component中使用的@Bean注解也是可以创建出对象的
那么他们的真正区别到底在哪呢?
在Spring官方有这样一段话
The @Bean methods in a Spring component are processed differently than their counterparts inside a Spring @Configuration class. The difference is that @Component classes are not enhanced with CGLIB to intercept the invocation of methods and fields. CGLIB proxying is the means by which invoking methods or fields within @Bean methods in @Configuration classes creates bean metadata references to collaborating objects; such methods are not invoked with normal Java semantics but rather go through the container in order to provide the usual lifecycle management and proxying of Spring beans even when referring to other beans via programmatic calls to @Bean methods. In contrast, invoking a method or field in an @Bean method within a plain @Component class has standard Java semantics, with no special CGLIB processing or other constraints applying.

大概是什么意思呢?
个人理解:大概是说如果使用了非@Configuration的注解,类似于@Service、@Repository、@Component,他在创建对象的时候不会触发CGLb 的动态创建一个增强对象,也就是说在使用非@Configuration注解的时候,使用@Bean注解是可以的,但是呢这样创建出来的类并不是一个被增强过后的类
这个增强啊大概是个怎么回事我也不太清楚,但是我知道一个如果使用两个不同的注解来注入@Bean的时候会发生一个怎样的事情
如果在@Component中使用@Bean的话,它的对象创建流程大概是就是
创建父类对象,
再去创建一遍创建父类对象,然后去创建子类对象创建子类对象
而在@Configuration中使用这个注解的话那么它的对象创建流程大概是
创建父类对象
从工厂中拿出父类对象,然后根据拿出来的对象,去创建子类对象
熟悉JAVA的人应该大概已经看明白了这其中的差别了吧!
那就是IOC的时候对象的创建的不同导致父类的重复创建问题!
@Value注解
@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码。

@Value("${text.aa}")
private Boolean timerEnabled;

上面那段代码可以读取SpringBoot的配置文件中的text.aa的key所对应的值!
说到了@Value就不得不说一下
@ConfigurationProperties(prefix = “text”)
@PropertySource 加载指定配置文件

@ConfigurationProperties(prefix = "text", ignoreUnknownFields=false)
@PropertySource(value= {"classpath:config/jdbc-bainuo-dev.properties"},
        ignoreResourceNotFound=false,encoding="UTF-8",
        name="jdbc-bainuo-dev.properties")
public class TestConfigurationProperties {
}

@ConfigurationProperties
上例中我们用@ConfigurationProperties注解就可以绑定属性了。ignoreUnknownFields = false告诉Spring Boot在有属性不能匹配到声明的域的时候抛出异常。开发的时候很方便! prefix 用来选择哪个属性的prefix名字来绑定。
请注意setters 和 getters 需要在@ConfigurationProperties bean中创建! 与@Value注解相反, 这带来了代码中的一些困扰 (特别是简单的业务中,个人观点).
OK,但是我们需要用属性来配置 application. 有至少两种方式来创建@ConfigurationProperties。即可以搭配@Configuration 注解来提供 @Beans 也可以单独使用并注入 @Configuration bean。
注意属性字段名要与抛去前缀的配置文件的key一致
@PropertySource
上述的代码目的是加载classpath路径中config文件中的jdbc-bainuo-dev.properties。其中encoding
用于指定读取属性文件所使用的编码,我们通常使用的是UTF-8;ignoreResourceNotFound含义是当指定的配置文件不存在是否报错,默认是false;比如上文中指定的加载属性文件是jdbc-bainuo-dev.properties。如果该文件不存在,则ignoreResourceNotFound为true的时候,程序不会报错,如果ignoreResourceNotFound为false的时候,程序直接报错。实际项目开发中,最好设置ignoreResourceNotFound为false。该参数默认值为false。
value值是设置需要加载的属性文件,可以一次性加载多个。name的值我们设置的是jdbc-bainuo-dev.properties。这个值在Springboot的环境中必须是唯一的,如果不设置,则值为:“class path resource [config/jdbc-bainuo-dev.properties]“。
可能很多人比较纳闷,为什么是“class path resource [config/jdbc-bainuo-dev.properties]“呢?这个就涉及到了Spring中对资源文件的封装类Resource。上文我们配置的value值为"classpath:config/jdbc-bainuo-dev.properties",因此Spring发现是classpath开头的,因此最终使用的是Resource的子类ClassPathResource。如果是file开头的,则最终使用的类是FileSystemResource。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值