路由器做网站服务器吗,瓯北网站制作公司,微信公众号里怎么做网站,你做网站群好朋友的作文在SpringBoot的大环境下#xff0c;基本上很少使用之前的xml配置Bean#xff0c;主要是因为这种方式不好维护而且也不够方便。 springboto注入bean主要采用下图几种方式#xff0c;分为本地服务工程注解声明的bean和外部依赖包中的bean。
一、 springboot装配本地服务工程…在SpringBoot的大环境下基本上很少使用之前的xml配置Bean主要是因为这种方式不好维护而且也不够方便。 springboto注入bean主要采用下图几种方式分为本地服务工程注解声明的bean和外部依赖包中的bean。
一、 springboot装配本地服务工程中的bean
1、注解装配Bean
1、使用ComponentServiceControllerRepository等派生注解 只要在类上加类上加 Component 注解即可,该注解只要被扫描到就会注入到spring的bean容器中。
Component
public class AnoDemoBean {
}
当然不只是Component注解可以声明Bean还有如:Repository、Service、Controller 等常用注解同样可以。
如果去看这些注解就发现这些注解上本身就有加 Component 注解
Target({ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
Documented
Component //可以看到Service注解上有添加Component Repository和Controller也一样。
public interface Service {AliasFor(annotation Component.class)String value() default ;
}这系列注解的出现给我们带来了极大的便利。我们不需要像以前那样在bean.xml文件中配置bean了现在只用在类上加上相关注解就能轻松完成bean的定义。
这四种注解在功能上其实没有特别的区别不过在业界有个不成文的约定
Controller 一般用在控制层Service 一般用在业务层Repository 一般用在数据层Component 一般用在公共组件上
2、Bean定义方式
这种方式主要是结合Configuration来定义bean首先是声明一个配置类而后再配置类中经过返回bean对象的方法形式来声明bean通常使用姿式以下
Data
public class ConfigDemoBean {
}Configuration
public class BeanLoadConfig {Beanpublic ConfigDemoBean configDemoBean() {return new ConfigDemoBean();}
}
须要说明的一点是BeanLoadConfig类自己也被Spring容器看为一个Bean。
3、Component注解 VS Bean注解
1作用对象不同Component 注解作用于类而 Bean 注解作用于方法。
Bean 方式更加灵活比如当我们引用第三方库中的类需要装配到 Spring 容器时只能通过 Bean 来实现。
比如
Configuration
public class WireThirdLibClass {Beanpublic ThirdLibClass getThirdLibClass() {//第三方的ThirdLibClass类return new ThirdLibClass();}
}再比如
Bean
public OneService getService(status) {case (status) {when 1:return new serviceImpl1();when 2:return new serviceImpl2();when 3:return new serviceImpl3();}
}这两点都是Component无法做到只能Bean实现所以说Bean更加灵活。
2Component通常是通过类路径扫描来自动装配到Spring容器中。而Bean通常我们会在该注解的方法中定义产生这个bean的逻辑。
我们可以加一些Conditional,ConditionalOnBean等等一些注解来控制是否声明该Bean不会一开始就自动装配到Spring容器中。
比如
public class MacCondition implements Condition {Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {Environment environment conditionContext.getEnvironment();String property environment.getProperty(os.name);if (property.contains(Mac)) {log.info(当前操作系统是Mac OS X);return true;}return false;}
}Configuration
public class ConditionalConfig {/*** 如果MacCondition的实现方法返回true则注入这个bean*/Bean(mac)Conditional({MacCondition.class})public SystemBean systemMac() {log.info(ConditionalConfig方法注入 mac实体);return new SystemBean(Mac ios系统,001);}
}上面的例子表示如果当前操作系统是Mac,才会注入当前Bean。这个也只能 Bean 注解才能实现。
总结Component和Bean都是用来注册Bean并装配到Spring容器中但是Bean比Component的自定义性更强。可以实现一些Component实现不了的自定义加载类。
二、springboot装配依赖包中的bean
当SpingBoot主类启动的时候SpringBootApplication注解会默认去扫描的自己所在包路径和它的子包路径下的所有需要装配的类自动装配到spring的bean容器中。 但是如果你提供了一个Jar包供第三方用户使用那么你这个jar包中的Bean能被第三方加载么 这就要看你当前项目的包名和你引用的第三方Jar包的包名是否一致了如果一致第三方依赖包中的bean可以直接加载如果不一致则无法加载第三方依赖中的bean。 例如如果你当前项目本包的地址是com.jincou而你引用的第三方Jar的本包也是 com.jincou那么第三方Jar的Bean就可以被扫描到并注入到spring的容器中。如果你当前项目本包的地址是com.jincou 而你引用的第三方Jar的本包是 com.third,那么也就是第三方Jar的Bean无法被扫描到所以也就无法注入到Spring容器中。
比如这里有个第三方的Bean。要如何做才能被扫描注入到Spring容器中呢。
package com.third.bean;import org.springframework.stereotype.Component;/*** Description: 这个bean作为第三方bean 给依赖该jar包的项目使用*/
Component
public class ThirdComponentBean {private String type 第三方ThirdComponent注解生成bean实体;
}1、ComponentScan注解
很简单既然SpringBootApplication注解默认扫描只是当前项目的本包和它的子包,那就想办法让它扫描第三方jar的包就好了。
/*** Description: Springboot 启动类*/
ComponentScan(basePackages {com.third.bean})
SpringBootApplication()
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class);}
}ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中。
需要装配的类也就是上面加了ControllerServiceRepositoryComponentConfiguration等等的注解的Bean到IOC容器中。
这里不一定要加在启动类上你可以加在加在装配的类上但建议加在启动类上比较直观后期如果要改动或者去除也比较好找。
2、Import注解
ComponentScan是扫描整个包,但其实你可能只需注入一个或者几个指定的Bean,那我们可以考虑用 Import 注解
Import(value com.third.bean.ThirdComponentBean.class)
SpringBootApplication()
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class);}
}这样做同样也会成功的将ThirdComponentBean对象注入到Spring的bean容器中。
3、spring.factories文件中配置bean
上面两种注入方式都有个很明显缺点,就是如果我需要引用外部jar包的Bean的时候,都需要在当前项目配置 ComponentScan 或者 Import 去扫描才能注入当前Bean这样显然不够友好。
可不可以当前项目什么都不做就可以直接引用第三方jar的Bean呢
当然可以。
我们只需要在将配置放在第三方jar指定的文件中即可使用者会自动加载从而避免的代码的侵入
在资源目录下新建目录 META-INF 在 META-INF 目录下新建文件 spring.factories 在文件中添加下面配置
org.springframework.boot.autoconfigure.EnableAutoConfigurationcom.third.bean.ConfigurationBean