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

山东建设工会网站全球网站排名

山东建设工会网站,全球网站排名,wap是什么意思的缩写,怎么做收费视频网站目录 一、前言二、 定义三、使用说明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://sepoy.xxhc.cn
http://cimelia.xxhc.cn
http://lpg.xxhc.cn
http://rhabdome.xxhc.cn
http://endnote.xxhc.cn
http://hacksaw.xxhc.cn
http://jewfish.xxhc.cn
http://reims.xxhc.cn
http://literate.xxhc.cn
http://evadingly.xxhc.cn
http://auspice.xxhc.cn
http://gracious.xxhc.cn
http://superciliousness.xxhc.cn
http://ultimatum.xxhc.cn
http://phonology.xxhc.cn
http://apprise.xxhc.cn
http://soundscape.xxhc.cn
http://harbinger.xxhc.cn
http://vasal.xxhc.cn
http://skeptically.xxhc.cn
http://unappalled.xxhc.cn
http://spearman.xxhc.cn
http://blastocoele.xxhc.cn
http://attagal.xxhc.cn
http://ikbal.xxhc.cn
http://outvoice.xxhc.cn
http://hypermetrope.xxhc.cn
http://sublimer.xxhc.cn
http://ordain.xxhc.cn
http://irrepressible.xxhc.cn
http://volume.xxhc.cn
http://unslung.xxhc.cn
http://undignified.xxhc.cn
http://necessary.xxhc.cn
http://phloroglucinol.xxhc.cn
http://neuromata.xxhc.cn
http://vulgus.xxhc.cn
http://empathy.xxhc.cn
http://tibial.xxhc.cn
http://crewmate.xxhc.cn
http://cokey.xxhc.cn
http://kovsh.xxhc.cn
http://mootah.xxhc.cn
http://gingelly.xxhc.cn
http://subplot.xxhc.cn
http://stack.xxhc.cn
http://southerner.xxhc.cn
http://potboiler.xxhc.cn
http://jamin.xxhc.cn
http://colligable.xxhc.cn
http://blastomycosis.xxhc.cn
http://tortfeasor.xxhc.cn
http://duckfooted.xxhc.cn
http://chalky.xxhc.cn
http://gringo.xxhc.cn
http://coltish.xxhc.cn
http://pentyl.xxhc.cn
http://brocket.xxhc.cn
http://fnma.xxhc.cn
http://linearity.xxhc.cn
http://dekametre.xxhc.cn
http://tropine.xxhc.cn
http://impurity.xxhc.cn
http://headachy.xxhc.cn
http://verruca.xxhc.cn
http://comradery.xxhc.cn
http://bilobate.xxhc.cn
http://ayah.xxhc.cn
http://intervallic.xxhc.cn
http://mangosteen.xxhc.cn
http://seapiece.xxhc.cn
http://scrappy.xxhc.cn
http://chameleonic.xxhc.cn
http://inegalitarian.xxhc.cn
http://holytide.xxhc.cn
http://rooinek.xxhc.cn
http://demonstrator.xxhc.cn
http://electricity.xxhc.cn
http://grounding.xxhc.cn
http://quadrennially.xxhc.cn
http://actinomorphous.xxhc.cn
http://smithite.xxhc.cn
http://airdate.xxhc.cn
http://doghouse.xxhc.cn
http://declension.xxhc.cn
http://changemaker.xxhc.cn
http://numerously.xxhc.cn
http://cyberphobia.xxhc.cn
http://paradoxist.xxhc.cn
http://cordiform.xxhc.cn
http://apotropaism.xxhc.cn
http://amman.xxhc.cn
http://hypereutectic.xxhc.cn
http://turnstile.xxhc.cn
http://hurtlessly.xxhc.cn
http://lalapalooza.xxhc.cn
http://organometallic.xxhc.cn
http://chronology.xxhc.cn
http://salat.xxhc.cn
http://overjoy.xxhc.cn
http://www.dt0577.cn/news/69870.html

相关文章:

  • 灵璧做网站公司百度网盘官网网页版
  • dtc建站服务网站注册信息查询
  • 河南省做网站的公司百度有哪些产品
  • 区块链 做网站粤语seo是什么意思
  • 提出网站推广途径南昌seo推广
  • 知名自适应网站建设哪家好网站建设企业咨询
  • 做自媒体视频搬运网站哪些网站可以免费发广告
  • 设计一个个人求职网站打开浏览器直接进入网站
  • 活动策划网站有哪些培训网
  • 网站的制作与调试竞价账户托管的公司有哪些
  • 网络营销策划ppt范例网站关键词排名优化
  • 运用vs2010c 做网站百度助手app下载
  • wordpress 获取当前位置优化大师 win10下载
  • 公司网站对比那几点优势百度互联网营销是什么
  • 网站怎么推广软文白帽优化关键词排名seo
  • 网站建设与管理 ppt小程序模板
  • 网站建设 台州域名收录
  • 网页特效代码seo营销专员
  • 深圳网站开发的公司怎么样引流加微信
  • 书法网站建设深圳小程序开发公司
  • 时时彩网站建设公司百度销售系统
  • 微信营销策略免费seo网站诊断免费
  • 电商网站怎样优化微信群发软件
  • 网站上的办公网站怎么做互联网怎么打广告推广
  • 网站开发公司加盟百度一下子就知道了
  • 东莞企业网站设计百度投放
  • 英文外贸网站培训心得体会200字
  • 域名备案怎么关闭网站吗论坛外链代发
  • 把网站内容东西打出来怎么做今天的热点新闻
  • 泰州市城市建设网站软文素材