[web开发] – 一些注解的解释

@WebServlet替代了原本web.xml中配置的url拦截

可以直接在servlet上添加该注解,加入("/hello")类似的路径

但在controller层(SpringBoot)加入后,该注解使用率降低

基础的web端=>controller层=>service层=>dao层结构已经满足基本的web应用组成.

dao层进行数据库的连接,在SpringBoot中可以基于映射文件进行查询(基于MyBatis)

基本的这样的架构参考demo

 

@Component

@Componentpublic class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory..."); int count = beanFactory.getBeanDefinitionCount(); String[] names = beanFactory.getBeanDefinitionNames(); System.out.println("µ±Ç°BeanFactoryÖÐÓÐ"+count+" ¸öBean"); System.out.println(Arrays.asList(names)); }}

这个注解一般用于除了Service和controller,实体bean之外的没有特别指定的组件.

一般springboot注入bean的注解还有一些: @Service @Bean ...
Spring容器会合适的时机创建这些bean

 

每一个bean创建完成都会使用各种后置处理器进行处理,增强bean的功能:

如@Autowired 是通过AutowiredAnnotationBeanPostProcessor处理自动注入

 <这是一张BeanFactoryPostProcessor装载继承及实现图

而在AutowiredAnnotationBeanPostProcessor中,

/** * Create a new AutowiredAnnotationBeanPostProcessor * for Spring‘s standard {@link Autowired} annotation. * <p>Also supports JSR-330‘s {@link javax.inject.Inject} annotation, if available. */ @SuppressWarnings("unchecked") public AutowiredAnnotationBeanPostProcessor() { this.autowiredAnnotationTypes.add(Autowired.class); this.autowiredAnnotationTypes.add(Value.class); try { this.autowiredAnnotationTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); logger.trace("JSR-330 ‘javax.inject.Inject‘ annotation found and supported for autowiring"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } }

这个构造器可以看到通过了

private final Set<Class<? extends Annotation>> autowiredAnnotationTypes = new LinkedHashSet<>(4);

这个链表Set进行装载,将Autowired.clss以及Value.class装载了进去

并通过ClassUtils.forName的这种反射机制,将javax.inject.Inject类,以及获取了本类的类加载器,而且这个地方会有可能抛出异常:

ClassNotFoundException

 

相关文章