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

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

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

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

[web开发] - 一些注解的解释_Linux

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

基本的这样的架构参考demo

 

@Component

@Component
public 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处理自动注入

[web开发] - 一些注解的解释_JVM_02

[web开发] - 一些注解的解释_JVM_03 <这是一张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

 [web开发] - 一些注解的解释_JVM_04

再看一张关于bean的注入图示

其实了解bean的注入机理还是挺复杂的,像之前我了解过的一些编程,其实去创建一个东西远比去剖析一个东西要简单.

因为被剖析的肯定是别人写的,那么别人写的肯定是你不懂的,要了解,要读懂作者的意图和惯用的套路.

而创建一个东西虽然不是说随心所欲,但只要搭建起来后,从根上你肯定就知道它是怎样构成的.