基于o2o的旅游网站建设,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 测试导入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 测试导入ImportBeanDefinitionRegistrar类3.5.1 创建UserImportBeanDefinitionRegistrar3.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中用法 导入Bean 导入配置类 导入 ImportSelector 实现类。一般用于加载配置文件中的类 导入 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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.3/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.ldx/groupIdartifactIdimport-annotation/artifactIdversion0.0.1-SNAPSHOT/versionnameimport-annotation/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build
/project3.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。
好处
导入指定的Bean或配置类。例如由于业务需要将包路径或者需要加载的Bean类不在ComponentScan的扫描范围内这时候我们就可以通过Import来实现Bean的注入。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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.3/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.ldx/groupIdartifactIduser/artifactIdversion0.0.1-SNAPSHOT/versionnameuser/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project5.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 ListUser getTeachers() {ListUser 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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.3/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.ldx/groupIdartifactIdbook-manage/artifactIdversion0.0.1-SNAPSHOT/versionnamebook-manage/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency!-- user模块依赖 --dependencygroupIdcom.ldx/groupIdartifactIduser/artifactIdversion0.0.1-SNAPSHOT/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project5.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中的BeanSpringBoot默认使用ImportSelector的方式加载META-INF/spring.factories中指定的配置类会导致只需要导入相应的第三方Jar就可以使用其环境中的Bean 可以在当前项目中使用Jar包中提供的Enable注解来导入Jar的Bean配置 文章转载自: http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.zydr.cn.gov.cn.zydr.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn