学习笔记051——SpringBoot学习2

Spring Boot 原理

Spring Boot 可以自动读取配置文件,将项目所需要的组件全部自动加载到 IoC 容器中,包括两部分

  • 开发者自定义组件(Controller、Service、Repository)
  • 框架自带的组件(DispatcherServlet、SqlSessionFactory)

Spring Boot 启动类注解 @SpringBootApplication 是由 3 个注解组成

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

1、@SpringBootConfiguration

@SpringBootConfiguration 本质上就是一个 @Configuration

@Configuration 的作用是标注一个配置类,将一个 Java 类标注成为一个配置类,用来取代 XML 的,向 IoC 容器中注入对象的。

基于 XML 的配置方式

package com.htl.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    private String username;
    private String password;
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置一个对象 -->
    <bean id="account" class="com.htl.entity.Account">
        <property name="username" value="tom"></property>
        <property name="password" value="123"></property>
    </bean>

</beans>
package com.htl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        System.out.println(applicationContext.getBean("account"));
    }
}

@Configuration

配置类,Java 类相当于 XML 文件

package com.htl.configure;

import com.htl.entity.Account;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AccountConfiguration {

    @Bean
    public Account account(){
        return new Account("cat","456");
    }
}

2、@ConfigurationProperties

可以直接读取 YAML 文件中的数据,并封装到 bean 中,给 bean 的属性赋值。

package com.htl.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "people")
public class People {
    private Integer id;
    private String name;
    private String tel;
}
package com.htl.configure;

import com.htl.entity.People;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(People.class)
public class PeopleConfiguration {
}
people:
  id: 1
  name: 张三
  tel: 123456789

3、@ComponentScan

将开发者自定义的组件进行扫描注入

4、@EnableAutoConfiguration

将框架自带的组件进行扫描注入

@AutoConfigurationPackage

@Import

@Import 注入 bean,结合一个选择器来注入,选择器提供要注入的 bean 的信息,@Import 实现注入

package com.htl.entity;

import lombok.Data;

@Data
public class User {
    private String cardId;
    private Double score;
}
package com.htl.selector;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class UserSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        String[] names = {"com.htl.entity.User"};
        return names;
    }
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.htl.configure.ImportConfig

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值