找人做一下网站大概多少钱,网站标题关键词用什么隔开,visio网站建设流程图,wordpress侧边栏菜单Spring利用依赖注入#xff08;DI#xff09;#xff0c;完成对IOC容器中中各个组件的依赖关系赋值#xff1b;依赖注入是spring ioc的具体体现#xff0c;主要是通过各种注解进行属性的自动注入。 一、Autowired#xff1a;自动注入
一、注解介绍 1、默认优先按照类型去… Spring利用依赖注入DI完成对IOC容器中中各个组件的依赖关系赋值依赖注入是spring ioc的具体体现主要是通过各种注解进行属性的自动注入。 一、Autowired自动注入
一、注解介绍 1、默认优先按照类型去容器中找对应的组件 2、如果找到多个相同类型的组件再将属性的名称作为组件的id去容器中查找 3、Qualifier(bookDao)使用Qualifier指定需要装配的组件的id而不是使用属性名 4、自动装配默认一定要将属性赋值好没有就会报错 可以使用Autowired(requiredfalse); 5、Primary让Spring进行自动装配的时候默认使用首选的bean 也可以继续使用Qualifier指定需要装配的bean的名字 BookService{ Autowired BookDao bookDao; } 二、AutowiredAnnotationBeanPostProcessor Autowired主要是通过AutowiredAnnotationBeanPostProcessor这个类实现的 三、使用地方 1、[标注在方法位置]Bean方法参数参数从容器中获取;默认不写Autowired效果是一样的都能自动装配 2、[标在构造器上]如果组件只有一个有参构造器这个有参构造器的Autowired可以省略参数位置的组件还是可以自动从容器中获取 3、放在参数位置 /*** Bean标注的方法创建对象的时候方法参数的值从容器中获取* param car* return*/Beanpublic Color color(Car car){Color color new Color();color.setCar(car);return color;}
二、Resource(JSR250)和Inject(JSR330) Spring还支持使用Resource(JSR250)和Inject(JSR330)[java规范的注解] 一、Resource: 可以和Autowired一样实现自动装配功能默认是按照组件名称进行装配的 不支持Primary功能也没有Autowiredreqiuredfalse这种功能; 二、Inject: 需要导入javax.inject的包和Autowired的功能一样。没有requiredfalse的功能 注意Autowired:Spring定义的 Resource、Inject都是java规范
三、自定义组件
一、自定义组件想要使用Spring容器底层的一些组件 自定义组件想要使用Spring容器底层的一些组件例如想要获取底层ApplicationContext(IOC)组件BeanFactory等等。可以自定义实现xxxAware接口在创建对象的时候会调用接口规定的方法注入相关组件 把Spring底层一些组件注入到自定义的Bean中。一般xxxAware接口功能都是使用xxxProcessor实现 例如ApplicationContextAware》ApplicationContextAwareProcessor ApplicationContextAware接口只有一个方法 public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext var1) throws BeansException; } 而ApplicationContextAwareProcessor是实现BeanPostProcessor接口里面的方法后置处理器 public interface BeanPostProcessor {Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}然后ApplicationContextAwareProcessor会在对象初始化前调用postProcessBeforeInitialization将ApplicationContext设置进实现 ApplicationContextAware接口的对象的setApplicationContext方法中然后我们想要使用的时候只需要将ApplicationContext对象保存在当前对象中即可具体栗子如下 /**
实现对应的xxxAware然后实现对应的方法
例如ApplicationContextAware当red对象生成后ApplicationContextAwareProcessor还在初始化
前将ApplicationContext 传入到setApplicationContext这个方法中然后red方法会保存在当前属性中
在red对象中写对应的业务逻辑是如果需要获取容器中的对象可以通过ApplicationContext 进行获取。
**/
Component
public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware {private ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {// TODO Auto-generated method stubSystem.out.println(传入的iocapplicationContext);this.applicationContext applicationContext;}Overridepublic void setBeanName(String name) {// TODO Auto-generated method stubSystem.out.println(当前bean的名字name);}Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {// TODO Auto-generated method stubString resolveStringValue resolver.resolveStringValue(你好 ${os.name} 我是 #{20*18});System.out.println(解析的字符串resolveStringValue);}}二、ApplicationContextAware接口实现原理源码展示 class ApplicationContextAwareProcessor implements BeanPostProcessor {private final ConfigurableApplicationContext applicationContext;private final StringValueResolver embeddedValueResolver;public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {this.applicationContext applicationContext;this.embeddedValueResolver new EmbeddedValueResolver(applicationContext.getBeanFactory());}Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {AccessControlContext acc null;// 判断red实现的是哪一个接口if (System.getSecurityManager() ! null (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {acc this.applicationContext.getBeanFactory().getAccessControlContext();}if (acc ! null) {// 权限检查如判断能不能够访问对应的方法 AccessController.doPrivileged(() - {this.invokeAwareInterfaces(bean);return null;}, acc);} else {// 主要是调用这个方法this.invokeAwareInterfaces(bean);}return bean;}/**主要是匹配对应实现的接口例如匹配到ApplicationContextAware会调用setApplicationContext将applicationContext设置进实现接口的对象方法中**/private void invokeAwareInterfaces(Object bean) {if (bean instanceof Aware) {if (bean instanceof EnvironmentAware) {((EnvironmentAware)bean).setEnvironment(this.applicationContext.getEnvironment());}if (bean instanceof EmbeddedValueResolverAware) {((EmbeddedValueResolverAware)bean).setEmbeddedValueResolver(this.embeddedValueResolver);}if (bean instanceof ResourceLoaderAware) {((ResourceLoaderAware)bean).setResourceLoader(this.applicationContext);}if (bean instanceof ApplicationEventPublisherAware) {((ApplicationEventPublisherAware)bean).setApplicationEventPublisher(this.applicationContext);}if (bean instanceof MessageSourceAware) {((MessageSourceAware)bean).setMessageSource(this.applicationContext);}if (bean instanceof ApplicationContextAware) {((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);}}}public Object postProcessAfterInitialization(Object bean, String beanName) {return bean;}
}四、Profile Spring为我们提供的可以根据当前环境动态的激活和切换一系列组件的功能。 Profile指定组件在哪个环境的情况下才能被注册到容器中不指定任何环境下都能注册这个组件 1、加了环境标识的bean只有这个环境被激活的时候才能注册到容器中。默认是default环境 2、写在配置类上只有是指定的环境的时候整个配置类里面的所有配置才能开始生效 3、没有标注环境标识的bean在任何环境下都是加载的 栗子 Profile(prod) Profile(dev)