SpringBoot:三、基础知识点总结

版权声明:转载请注明出处 https://blog.csdn.net/chenmingxu438521/article/details/89338123

一、读取配置文件的方式

1.方式一:通过@Value

application.properties

test.name=cmx
test.age=18
    @Value("${test.name}")
    private String name;
 
    @Value("${test.age}")
    private String age;

2.方式二:通过注入环境变量获取

application.properties

test.name=cmx
test.age=18
    @Autowired
    private Environment environment;
    environment.getProperty("test.name");
    environment.getProperty("test.age");

二、@SpringBootApplication注解

1.@SpringBootApplication =  @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan。一句话就是通过@SpringBootApplication替代了applicationContext.xml

1.1.@SpringBootConfiguration继承自@Configuration注解,二者功能也是一样的,标注当前类是配置类,并将当前类内声明的一个或者多个以@Bean注解标记的方法的实例装配到spring容器中,并且实例名就是方法名。

1.2.@EnableAutoConfiguration的作用启动自动的配置,它的意思就是Springboot根据你添加的jar包来配置你的项目默认配置,比如根据spring-boot-starter-web,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。

1.3.@ComponentScan,扫描当前包及其子包下被@Component@Controller@Service@Repository注解标记的类并纳入到spring容器中进行管理,是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。所以本demo中的User为何会被spring容器管理。常用的参数如下:

1.3.1.basePackages:用于指定扫包路径,basePackages={" "," "," "}

         includeFilters:包含的过滤条件,includeFilters={@Filter(type=xxx,classes={xxx.class,xxx.class})};

         excludeFilters:排除的过滤条件,excludeFilters={@Filter(type=FilterType.ANNOTATION,classes={Repository.class})};

         1.type=FilterType.ANNOTATION: 是根据注解的规则
         2.type=FilterType.ASSIGNABLE_TYPE:按照给定的类型
         @Filter(type=FilterType.ASSIGNABLE_TYPE,classes=CustomerService.class)
         3.type=FilterType.ASPECTJ:使用aspectj表达式
         4.type=FilterType.REGEX:使用正则表达式
         5.type=FilterTyoe.CUSTOM:自定义规则

三、@Configuration和@Bean

1.@Configuration作用在类上,表示该类是一个配置类,其实就相当于一个xml配置文件,可理解为用spring的时候xml里面的<beans>标签,@Bean可理解为用spring的时候xml里面的<bean>标签

例子:

@Configuration
public class TestConfig {

    @Bean
    public Service service() {
        return new ServiceImpl();
    }

}

就相当于在 XML 文件中配置

<beans>
    <bean id="service" class="com.cmx.services.ServiceImpl"/>
</beans>

2.@Configuration 注解的类中,所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。

四、@ConfigurationProperties的讲解

1.有时候是这样的场景,我们想把配置文件的信息,读取并且封装成实体类,这样子我们在实体类中操作就方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类。

实例:

user.username=root
user.password=123456
user.remoteAddress=127.0.0.1

封装到实体类中:

@Component
@ConfigurationProperties(prefix="user")
public class Test {

    private String username;
    private String remoteAddress;
    private String password ;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getRemoteAddress() {
        return remoteAddress;
    }
    public void setRemoteAddress(String remoteAddress) {
        this.remoteAddress = remoteAddress;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

或者我们还可以把@ConfigurationProperties还可以直接定义在@bean的注解上,这是bean实体类就不用@Component和@ConfigurationProperties了

@SpringBootApplication
public class Demotest{

    @Bean
    @ConfigurationProperties(prefix = "user")
    public Test connectionSettings(){
        return new Test();

    }

    public static void main(String[] args) {
        SpringApplication.run(Demotest.class, args);
    }
}

然后我们需要使用的时候就直接这样子注入

@RestController
@RequestMapping("/test")
public class DemoController {

@Autowired Test test;

@RequestMapping(value = {"/",""})
public String hellTask(){
    String userName = test.getUsername();     
    return "hello task !!";
}

}

这就是@ConfigurationProperties的用法。

五、@Sl4j的解释

1.@Sl4j的用法

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

然后在类上加入@Slf4j,就可以直接使用log.info()来输出日志

六、@Inherited解释

1.元注解:就是定义注解的注解

Inherited作用是,使用此注解声明出来的自定义注解,在使用此自定义注解时,如果注解在类上面时,子类会自动继承此注解,否则的话,子类不会继承此注解。这里一定要记住,使用Inherited声明出来的注解,只有在类上使用时才会有效,对方法,属性等其他无效。(不是太理解这个东西有何实际的意义,希望你们看到的能给我评论下,大家互相学习,谢谢)

七、结束

暂时先介绍到这里吧,以后会添加的!!!

猜你喜欢

转载自blog.csdn.net/chenmingxu438521/article/details/89338123