沈阳网站seo排名优化,.net网站空间,东方城乡与住房建设部网站,上海做外贸网站的公司第一步需要在pom.xml文件指定需要导入的坐标 要是没有自动提示需要检查maven有没有 实现代码
/*springboot第三方自动配置实现方法
* 什么是自动配置 自动配置就是springboot启动自动加载的类不需要在手动的控制反转自动的加入bean中
*
* *//*第一种方案包扫描 不推荐因为繁琐…第一步需要在pom.xml文件指定需要导入的坐标 要是没有自动提示需要检查maven有没有 实现代码
/*springboot第三方自动配置实现方法
* 什么是自动配置 自动配置就是springboot启动自动加载的类不需要在手动的控制反转自动的加入bean中
*
* *//*第一种方案包扫描 不推荐因为繁琐自己也没有试成功过加在启动类上
ComponentScan({com.example,com.example.comtihmabc2}*//*
第二种方案Import注解导入加在启动类上
Import({HeaderParser.class}) 导入普通类交给I0C容器管理Import({HeaderConfig.class})//导入配置类交给I0C容器管理
示例代码
Configuration
public class HeaderConfig {Beanpublic HeaderParser headerParser(){ return new HeaderParser();}Beanpublic HeaderGenerator headerGenerator(){ return new HeaderGenerator(); }
}
Import({MyImportSelector.class})//导入Importselector接口实现类
第三方所实现的接口ImportSelector 里面清楚的写明白了需要导入的类位置
示例代码
public class MyImportSelector implements ImportSelector {public String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{com.example.HeaderConfig};}
*//*第三种方案自定义注解EnableHeaderConfig加在启动类上
第三方自定义注解示例代码
Retention(RetentionPolicy.RUNTIME)
Target(ElementType.TYPE)
Import(MyImportSelector.class)//MyImportSelector.class表示所要导入的类可以是配置类
public interface EnableHeaderConfig {
}
* */SpringBootApplication
public class ComTihmaBc2Application {public static void main(String[] args) {SpringApplication.run(ComTihmaBc2Application.class, args);}}执行代码示例
SpringBootTest
class ComTihmaBc2ApplicationTests {// //1.获取I0c容器对象
// Autowired
// ApplicationContext applicationContext;Autowired //手动依赖注入ApplicationContext applicationContext ;// Test
// void contextLoads() {
// applicationContext.getBean(HeaderParser.class).parse();
// }Testvoid contextLoads1() {applicationContext.getBean(HeaderGenerator.class).generate();}}自动装配条件判断
/*springboot自动配置条件判断注解满足条件才会执行 通常声名在类上或者方法上
* springboot底层自动配置就是用此方法
* */
Configuration
public class HeaderConfig {//注解1/*判断环境中是否有对应字节码文件才注册bean到I0C容器。可通过类型value或者名字name来进行判断示例1 ConditionalOnClass(name io.jsonwebtoken.Jwts) 名字name示例2 ConditionalOnClass(value HeaderParser.class ) 通过类型value*///注解2/* ConditionalOnMissingBean 当不存在当前类型的bean时才声明该bean才加入ioc容器 ---也可以指定类型(value属性)或 名称(name属性)没有就是当前 比如ConditionalOnMissingBeanpublic HeaderParser headerParser(){ return new HeaderParser();}*/
/* ConditionalOnProperty(name name,havingValue itheima)
配置文件中存在对应的属性和值才注册bean到I0C容器比如application.properties 配置文件中
# 应用服务 WEB 访问端口
server.port8080
nameitheima
有就可以执行*/BeanConditionalOnProperty(name name,havingValue itheima)public HeaderParser headerParser(){ return new HeaderParser();}Beanpublic HeaderGenerator headerGenerator(){ return new HeaderGenerator(); }
}