2017-09-04 35 views
0

我对Java Servlets和JSP有一点经验,但我曾与Spring合作过。在Spring中我们有一个名为BeanPostProcessor的接口。我使用这个接口实现来创建自定义注释。代码示例Servlet 3.0中的BeanPostProcessor模拟

public class InjectRandomIntAnnotationBeanPostProcessor implements BeanPostProcessor { 

    @Override 
    public Object postProcessBeforeInitialization(Object bean, String string) throws BeansException { 
     Field[] fields = bean.getClass().getDeclaredFields(); 
     for (Field field : fields) { 
      InjectRandomInt annotation = field.getAnnotation(InjectRandomInt.class); 
      if (annotation != null) { 
       int min = annotation.min(); 
       int max = annotation.max(); 
       Random r = new Random(); 
       int i = min + r.nextInt(max - min); 
       field.setAccessible(true); 
       ReflectionUtils.setField(field, bean, i); 
      } 

     } 
     return bean; 
    } 

    @Override 
    public Object postProcessAfterInitialization(Object o, String string) throws BeansException { 
     return o; 
    } 

} 

当我开始在使用Servlet我提到这个注释@WebServlet(urlPatterns = "/user/login") 的问题是:是否有任何功能在Servlet中,类似于用的BeanPostProcessor春,这是哪里注释@WebServlet注入?

例子: 我的意思是注释春注射用的BeanPostProcessor,为examle注释@Autowired由AutoWiredAnnotationBeanPostProcessor类中声明,但在注释@WebServlet注入(或申报)

+0

这个注解用于声明一个servlet和配置其映射。正如javadoc(http://docs.oracle.com/javaee/7/api/javax/servlet/annotation/WebServlet.html)所述。注释不会被注入任何地方。开发人员使用注释来注释代码。你的问题到底是什么?你想达到什么目的?如果你想知道servlets是如何工作的,为什么不读取免费的规范? –

+0

对不清楚的问题,我添加更多的细节问题 –

回答