自动装配+maven私服

springboot自动装配:

方案一:@ComponentScan 组件扫描
@ComponentScan({"com.example","com.itheima"})
@SpringBootApplication
public class SpringbootWebConfig2Application{}
方案二:@Import导入。使用@Import导入的类会被Spring加载到IOC容器中,导入形式主要有以下几种:
    ------------------------------------------------------
    HeaderConfig.java
    @Configuration
    public class HeaderConfig {
        @Bean
        public HeaderParser headerParser(){
            return new HeaderParser();
        }
        @Bean
        public HeaderGenerator headerGenerator(){
            return new HeaderGenerator();
        }
    }       
    ------------------------------------------------------
    TokenParser.java
    @Component
    public class TokenParser {
        public void parse(){
            System.out.println("TokenParser ... parse ...");
        }
    }
    ------------------------------------------------------
    MyImportSelector.java
    public class MyImportSelector implements ImportSelector {
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[]{"com.example.HeaderConfig"};
        }
    }
    ------------------------------------------------------

    1、导入 普通类
        在启动类中加了@Import 注解之后,TokenParser类中加不加@Component 注解都可以将这个类直接交给IOC容器管理 
        ------------------------------------------------------
        @Import({TokenParser.class})
        @SpringBootApplication
        public class SpringbootWebConfig2Application{}
        ------------------------------------------------------
    2、导入 配置类
        配置类导入进来之后,配置类中所有的bean对象都会加载到IOC容器当中,
        如HeaderConfig类中声明的 bean 的 HeaderParser 和 HeaderGenerator 也会交给IOC管理
        ------------------------------------------------------
        @Import({HeaderConfig.java})
        @SpringBootApplication
        public class SpringbootWebConfig2Application{}
        ------------------------------------------------------
    3、导入 ImportSelector 接口的实现类
        ImportSelector是Spring框架中的一个接口,它用于实现在@Configuration配置类中选择性地导入需要注册到IoC容器中的bean。
        在一些复杂的用例中,我们可能需要根据某些条件来动态地决定使用哪些Bean。此时,就可以使用ImportSelector接口来注册所需的Bean定义,
        并将其返回给IoC容器进行统一管理。具体而言,使用@Import注解加上实现ImportSelector接口的类,就可以实现动态注册Bean的目的。
        HeaderConfig类中声明的 bean 的 HeaderParser 和 HeaderGenerator 也会交给IOC管理
        ------------------------------------------------------
        @Import({MyImportSelector.java})
        @SpringBootApplication
        public class SpringbootWebConfig2Application{}
        ------------------------------------------------------
    4、EnableXxxx注解,封装@Import 注解,然后@Import 后面再来指定要导入哪些 bean 或者哪些配置类
        ------------------------------------------------------
        @Retention(RetentionPolicy.RUNTIME)
        @Target(ElementType.TYPE)
        @Import(MyImportSelector.class)
        public @interface EnableHeaderConfig {
        }
        ------------------------------------------------------
        @Import中导入了 ImportSelector 接口的实现类 MyImportSelector.class,
        而在 MyImportSelector.class 中就指定了要导入哪些配置类和bean
        第三方依赖当中提供了这个注解之后,如果要开启第三方依赖的自动配置功能,我们就不用去记要导入哪些bean和哪些配置类了,
        直接加 @EnableHeaderConfig 就可以了

Springboot在启动的时候会自动加载 META-INF/spring.factories 
和 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件当中所配置的信息,
当信息加载出来之后,会封装到 List<String> configurations 当中,
而List集合中的内容又会封装到 String[] selectImports 当中,而String数组中封装的数据,最终就是加载到IOC容器当中的 bean 或 配置类

自动配置原理:

@SpringBootApplication 中封装了
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan
        而@EnableAutoConfiguration 中,封装了
            @AutoConfigurationPackage
            @Import(AutoConfigurationImportSelector.class)
                AutoConfigurationImportSelector.class 是 ImportSelector 接口的实现类,
                    这个实现类中实现了ImportSelector 接口中的一个方法 String[] selectImports(...)
                        此方法的返回值是String类型的数组,数组中封装的是要导入到SpringIOC容器的类的全类名,
                        此方法中加载了两个文件
                            1、META-INF/spring.factories     这时springboot早期自动配置加载的文件
                            /*而在2.7.X版本中提供了一份全新的配置文件 AutoConfiguration.imports,
                            上面的 spring.factories 会在3.X.X中会被彻底移除*/
                            2、META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
                            所以我们现在要导入的配置类就直接定义在此文件中,此文件中定义的就是配置类的全类名,
                            在这些配置类当中,就可以通过 @Bean 注解来声明一个一个的bean对象,
                            最终springboot项目在启动时就会加载 AutoConfiguration.imports 配置文件中所配置的配置类,
                            然后将配置类的信息封装到String[] selectImports(...)返回的String数组中,
                            最终通过@Import 注解将这些配置类全部加载到springIOC容器当中。

maven私服:

私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,用来代理位于外部的中央仓库,用于解决团队内部的资源共享与资源同步问题

依赖查找顺序:本地方库 -> 私服 -> 中央仓库

私服里有: central仓库、release仓库、snapshot仓库

central仓库存放的是从中央仓库下载下来的资源,

而 release仓库 和 snapshot仓库 则都是项目组内部共享的资源

项目版本:
  • RELEASE(发行版本):功能趋于稳定、当前更新停止,可以用于发行的版本,存储在私服的RELEASE仓库中
  • SNAPSHOT(快照版本):功能不稳定、尚处于开发中的版本,即快照版本,存储在私服的SNAPSHOT仓库中
        <artifactId>tlias-utils</artifactId>
        <version>1.0-SNASHOT</version>
        此为快照版本,如果<version>1.0-RELEASE</version>,就上传至发行版本仓库,
        即使不加-RELEASE,默认也是上传至发行版本仓库
    

资源上传与下载:

1、设置私服的访问名/密码(settings.xml 中 servers 中配置)
-------------------------------------
<server>
    <id>maven-releases</id>
    <username>admin</username>
    <password>admin</password>
</server>

<server>
    <id>maven-snapshots</id>
    <username>admin</username>
    <password>admin</password>
</server>
-------------------------------------
2、IDEA的maven工程的pom文件中配置上传(发布)地址
-------------------------------------
<distributionManagenment>
    //RELEASE版本
    <repository>
        <id>maven-releases</id>
        <url>http://192.168.150.101:8081/repository/maven-releases/</url>
    </repository>
    //SNAPSHOT版本
    <snapshotRepository>
        <id>maven-snapshots</id>
        <url>http://192.168.150.101:8081/repository/maven-releases/</url>
    </snapshotRepository>
</distributionManagenment>
-------------------------------------
3、设置私服依赖下载的仓库组地址(settings.xml 中 mirrors、profiles 中配置)
-------------------------------------
<mirror>
    <id>maven-public</id>
    <mirrorOf>*</mirrorOf>
    <url>http://192.168.150.101:8081/repository/maven-public/</url>
</mirror>
-------------------------------------
<profile>
    <id>allow-snapshots</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    <repositories>
        <repository>
            <id>maven-public</id>
            <url>http://192.168.150.101:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>
-------------------------------------
4、如果需要上传自己的项目到私服上,需要在项目的pom.xml文件中,增加如下配置,来配置项目发布的地址(也就是私服的地址)
-------------------------------------
<distributionManagement>
    <!-- release版本的发布地址 -->
    <repository>
        <id>maven-releases</id>
        <url>http://192.168.150.101:8081/repository/maven-releases/</url>
    </repository>

    <!-- snapshot版本的发布地址 -->
    <snapshotRepository>
        <id>maven-snapshots</id>
        <url>http://192.168.150.101:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>   
-------------------------------------
5、发布项目,直接运行 deploy 生命周期即可 (发布时,建议跳过单元测试)
创作不易!转载请注明作者及文章链接或作者博客链接——
- 作者:pidanxia
- 链接:https://pidanxia.ink
(链接可为:**文章链接**或者**作者博客链接**)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇