当前位置: 首页 > news >正文

成都调整疫情风险区seo石家庄

成都调整疫情风险区,seo石家庄,wordpress排除分类,wordpress获取最新文章标题目录 一、前言二、 定义三、使用说明3.1 创建项目3.1.1 导入依赖3.1.2 创建User类 3.2 测试导入Bean3.2.1 修改启动类 3.3 测试导入配置类3.3.1 创建UserConfig类3.3.2 修改启动类 3.4 测试导入ImportSelector3.4.1 创建UseImportSelector类3.4.2 修改启动类3.4.3 启动测试 3.5…

目录

    • 一、前言
    • 二、 定义
    • 三、使用说明
      • 3.1 创建项目
        • 3.1.1 导入依赖
        • 3.1.2 创建User类
      • 3.2 测试导入Bean
        • 3.2.1 修改启动类
      • 3.3 测试导入配置类
        • 3.3.1 创建UserConfig类
        • 3.3.2 修改启动类
      • 3.4 测试导入ImportSelector
        • 3.4.1 创建UseImportSelector类
        • 3.4.2 修改启动类
        • 3.4.3 启动测试
      • 3.5 测试导入ImportBeanDefinitionRegistrar类
        • 3.5.1 创建UserImportBeanDefinitionRegistrar
        • 3.5.2 修改启动类
      • 3.6 小结
    • 四、 改进
      • 4.1 创建注解
      • 4.2 修改启动类
    • 五、 验证
      • 5.1 创建项目
      • 5.2 创建user模块
        • 5.2.1 导入依赖
        • 5.2.2 创建User类
        • 5.2.3 修改启动类
      • 5.3 创建book-manage模块
        • 5.3.1 导入依赖
        • 5.3.2 修改启动类
    • 六、 在user模块中添加Enable注解
    • 七、小结

在这里插入图片描述
在这里插入图片描述

一、前言

@Import导入的类会被Spring加载到IOC容器中。而@Import提供4中用法:

  1. 导入Bean

  2. 导入配置类

  3. 导入 ImportSelector 实现类。一般用于加载配置文件中的类

  4. 导入 ImportBeanDefinitionRegistrar 实现类。

二、 定义

@Import注解定义如下,其内部只有一个参数为Class对象数组

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {Class<?>[] value();
}

三、使用说明

通过一个简单的小例子测试一下@Import是不是真的能实现Bean的注入

3.1 创建项目

在这里插入图片描述

3.1.1 导入依赖

这里我们除了springboot依赖,再添加个lombok依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ldx</groupId><artifactId>import-annotation</artifactId><version>0.0.1-SNAPSHOT</version><name>import-annotation</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>
3.1.2 创建User类
package com.ldx.importannotation;import lombok.AllArgsConstructor;
import lombok.Data;/*** 用户信息实体* @author ludangxin* @date 2021/8/1*/
@Data
@AllArgsConstructor
public class User {public User() {this.name = "李四";this.age = 13;}private String name;private Integer age;
}

3.2 测试导入Bean

3.2.1 修改启动类
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;@Slf4j
// 注入UserBean
@Import(value = User.class)
@SpringBootApplication
public class ImportAnnotationApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(ImportAnnotationApplication.class, args);User user = applicationContext.getBean(User.class);log.info("user info ==={}",user);}
}

3.3 测试导入配置类

3.3.1 创建UserConfig类
import org.springframework.context.annotation.Bean;/*** 用户配置类* @author ludangxin* @date 2021/8/1*/
public class UserConfig {@Beanpublic User getUser(){return new User();}
}
3.3.2 修改启动类

将启动类上的@Import的value指向UserConfig类

@Import(value = UserConfig.class)

UserBean注入成功。

3.4 测试导入ImportSelector

3.4.1 创建UseImportSelector类
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;/*** 用户Bean选择器配置类* @author ludangxin* @date 2021/8/1*/
public class UseImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {// 指定User类的全限类名return new String[]{"com.ldx.importannotation.User"};}
}
3.4.2 修改启动类

将启动类上的@Import的value指向UseImportSelector类

@Import(value = UseImportSelector.class)
3.4.3 启动测试

UserBean注入成功。

3.5 测试导入ImportBeanDefinitionRegistrar类

3.5.1 创建UserImportBeanDefinitionRegistrar
package com.ldx.importannotation;import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;/*** 用户Bean定义注册配置类* @author ludangxin* @date 2021/8/1*/
public class UserImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {// 创建User类型的Bean的定义BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();// 将创建的UserBean定义注册到SpringRegistry中,其名称为userregistry.registerBeanDefinition("user", beanDefinition);}
}
3.5.2 修改启动类

将启动类上的@Import的value指向UserImportBeanDefinitionRegistrar类

@Import(value = UserImportBeanDefinitionRegistrar.class)

3.6 小结

简介中介绍的四种方式都可以注入UserBean。

好处:

  1. 导入指定的Bean或配置类。例如:由于业务需要将包路径或者需要加载的Bean类不在@ComponentScan的扫描范围内,这时候我们就可以通过@Import来实现Bean的注入。
  2. ImportSelector方式是制定需要加载类的全限类名。这时候我们就可以将我们的需要装载的类写到配置文件中,比如某个txt中,然后项目启动的时候读取txt中的全限类名,实现Bean的装载。SpringBoot就是使用这种方式实现的自动装配。

四、 改进

上面的例子通过使用@Import注解实现了spring bean的自动注入。但是装载Bean每次都得指定Bean的类或者配置类,在生产环境中我们在使用第三方Jar的时候根本不知道应该使用哪个配置文件或者压根就不知道配置文件的名称。这时我们其实可以扩展一个注解来优化这个问题。

4.1 创建注解

package com.ldx.importannotation;import org.springframework.context.annotation.Import;
import java.lang.annotation.*;/*** 启用User配置信息注解* @author ludangxin* @date 2021/8/1*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 指定需要导入的UserBean的配置类
@Import(UseImportSelector.class)
public @interface EnableUser {}

4.2 修改启动类

注掉之前的@Import,使用刚创建的@EnableUser注解

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;@Slf4j
//@Import(value = UserImportBeanDefinitionRegistrar.class)
@EnableUser
@SpringBootApplication
public class ImportAnnotationApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(ImportAnnotationApplication.class, args);User user = applicationContext.getBean(User.class);log.info("user info ==={}",user);}
}

UserBean注入成功。

思考🤔
SpringBoot项目中能不能直接获取或者使用Jar包中的Bean呢?

五、 验证

5.1 创建项目

user模块为Bean的提供者,book-manage通过引入user模块Jar,来验证项目中能不能直接使用Jar中的Bean

5.2 创建user模块

5.2.1 导入依赖

没啥特别的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ldx</groupId><artifactId>user</artifactId><version>0.0.1-SNAPSHOT</version><name>user</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
5.2.2 创建User类
package com.ldx.user;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;/*** 用户信息类* @author ludangxin* @date 2021/8/1*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;private String name;/*** 角色状态吗 1.管理员 2.老师 3.学生*/private Character role;private Integer age;public User getUserInfo() {return new User(66, "张三", '3', 21);}/*** 获取其任课老师信息*/public List<User> getTeachers() {List<User> users = new ArrayList<>();User user = new User();user.setId(2);user.setName("王麻子");user.setAge(45);user.setRole('2');User user1 = new User();user1.setId(3);user1.setName("李四");user1.setAge(35);user1.setRole('2');users.add(user);users.add(user1);return users;}
}
5.2.3 修改启动类
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;@Slf4j
@SpringBootApplication
public class UserApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(UserApplication.class, args);User user = applicationContext.getBean(User.class);user.getTeachers().forEach(obj ->log.info("user teacher info ==={} ", obj));}@Beanpublic User getUser() {return new User();}
}

5.3 创建book-manage模块

5.3.1 导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ldx</groupId><artifactId>book-manage</artifactId><version>0.0.1-SNAPSHOT</version><name>book-manage</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- user模块依赖 --><dependency><groupId>com.ldx</groupId><artifactId>user</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
5.3.2 修改启动类

直接获取User Bean 并调用getUserInfo方法

import com.ldx.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@Slf4j
@SpringBootApplication
public class BookManageApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(BookManageApplication.class, args);User user = applicationContext.getBean(User.class);log.info("user info === {}", user.getUserInfo());}}

六、 在user模块中添加Enable注解

import org.springframework.context.annotation.Import;
import java.lang.annotation.*;/*** 启用User配置信息注解* @author ludangxin* @date 2021/8/1*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(User.class)
public @interface EnableUser {}

七、小结

不能直接在当前项目中使用Jar中的Bean(SpringBoot默认使用ImportSelector的方式加载META-INF/spring.factories中指定的配置类,会导致只需要导入相应的第三方Jar就可以使用其环境中的Bean)
可以在当前项目中使用Jar包中提供的Enable注解来导入Jar的Bean配置


文章转载自:
http://threshing.nrwr.cn
http://pitchblende.nrwr.cn
http://whereunder.nrwr.cn
http://regalement.nrwr.cn
http://favism.nrwr.cn
http://introverted.nrwr.cn
http://authenticate.nrwr.cn
http://nominal.nrwr.cn
http://holocoder.nrwr.cn
http://mown.nrwr.cn
http://peripateticism.nrwr.cn
http://colourcast.nrwr.cn
http://goshen.nrwr.cn
http://dipperful.nrwr.cn
http://hobble.nrwr.cn
http://taxloss.nrwr.cn
http://dowry.nrwr.cn
http://cariogenic.nrwr.cn
http://thraldom.nrwr.cn
http://xystarch.nrwr.cn
http://lustrous.nrwr.cn
http://stabbed.nrwr.cn
http://bats.nrwr.cn
http://tautomerism.nrwr.cn
http://levallois.nrwr.cn
http://picrate.nrwr.cn
http://prolixly.nrwr.cn
http://benighted.nrwr.cn
http://gamophyllous.nrwr.cn
http://shive.nrwr.cn
http://uncomplimentary.nrwr.cn
http://ideation.nrwr.cn
http://tunellite.nrwr.cn
http://yeoman.nrwr.cn
http://astutely.nrwr.cn
http://toxication.nrwr.cn
http://cutler.nrwr.cn
http://maleate.nrwr.cn
http://greatcoat.nrwr.cn
http://mensual.nrwr.cn
http://hyetometer.nrwr.cn
http://malaysia.nrwr.cn
http://bondstone.nrwr.cn
http://calputer.nrwr.cn
http://metamale.nrwr.cn
http://bayeux.nrwr.cn
http://osseous.nrwr.cn
http://epidemical.nrwr.cn
http://christianise.nrwr.cn
http://sequelae.nrwr.cn
http://dentex.nrwr.cn
http://gotha.nrwr.cn
http://torrent.nrwr.cn
http://overcrust.nrwr.cn
http://placode.nrwr.cn
http://ergotinine.nrwr.cn
http://kushitic.nrwr.cn
http://distichously.nrwr.cn
http://citizenhood.nrwr.cn
http://incorporator.nrwr.cn
http://leptodactyl.nrwr.cn
http://tannage.nrwr.cn
http://barbara.nrwr.cn
http://gazebo.nrwr.cn
http://astigmatometry.nrwr.cn
http://tribunitial.nrwr.cn
http://phytotomy.nrwr.cn
http://tumesce.nrwr.cn
http://impaction.nrwr.cn
http://prothallium.nrwr.cn
http://hiding.nrwr.cn
http://gyges.nrwr.cn
http://cityfied.nrwr.cn
http://gastrovascular.nrwr.cn
http://asininity.nrwr.cn
http://pigskin.nrwr.cn
http://stub.nrwr.cn
http://unmistakable.nrwr.cn
http://msee.nrwr.cn
http://dynamoelectric.nrwr.cn
http://ferrotype.nrwr.cn
http://endocranial.nrwr.cn
http://breconshire.nrwr.cn
http://delimiter.nrwr.cn
http://refectory.nrwr.cn
http://polytheistic.nrwr.cn
http://buyable.nrwr.cn
http://juberous.nrwr.cn
http://metagalactic.nrwr.cn
http://inasmuch.nrwr.cn
http://prelature.nrwr.cn
http://stateswoman.nrwr.cn
http://amygdalaceous.nrwr.cn
http://devilishly.nrwr.cn
http://cinematograph.nrwr.cn
http://heaver.nrwr.cn
http://trophallaxis.nrwr.cn
http://bitterish.nrwr.cn
http://laboratorial.nrwr.cn
http://hughie.nrwr.cn
http://www.dt0577.cn/news/63733.html

相关文章:

  • 建设网站有哪些步骤免费发外链的网站
  • 集团公司网站推广方案怎么做网络热词2021流行语
  • 好看简单易做的网站推动高质量发展
  • 沈阳网站制作的公司哪家好google play服务
  • 宝鸡做网站的公司推广策略怎么写
  • 上海集团网址关键词优化如何
  • 网站页面和图片设计营销推广方案
  • 做海报的简易网站推广普通话手抄报图片
  • 免费手机网站建设上海外贸seo
  • 大型网站制作怎么样企业中层管理人员培训课程
  • 响应式电商网站下载安装
  • 企业只有建立自己的网站平台营销网站建设制作
  • 制作一个论坛网站多少钱项目平台
  • 小程序源码怎么用四川seo选哪家
  • 网站建设与制作教学计划手机百度官网
  • 网站建设优势搜狗优化排名
  • 杭州的网站设计百度关键词排名原理
  • 武汉网站建设公司哪家专业网络营销的5种方式
  • 万网怎样做网站调试成都公司网站seo
  • 丹徒网站建设平台山西seo排名厂家
  • 云南网站建设价格低广州最新疫情通报
  • 网站建设图片如何放在网站上百度关键词搜索引擎排名优化
  • 建立网站的详细步骤知乎搜索引擎推广seo
  • 做网站都要用到框架吗深圳网络推广方法
  • 信息课做网站的软件平台推广员是做什么的
  • wordpress美化下载页面湖南网站seo找行者seo
  • 公司网站怎么做简介百度竞价关键词出价技巧
  • 基于js原生的新闻类静态网站建设滕州今日头条新闻
  • 湖州专业做网站百度网首页
  • 免费申请网站空间和域名seo网站推广案例