Spring启动后加载类或启动类方法
使用背景: 有两个简单的方法,都是在applicationContext.xml中配置beanbeanid="templateAnnotationInit"class="com.cnc.apm.handler.ParseTemplateAnnotationHandler"/>其中,ParseTemplateAnnotationHandler是JavaBean。Spring容器启
使用背景:
这两天开发一个自定义注解,根据注解生成说明的菜单树,用于前端编辑。博主缺乏经验,所以觉得这个开发学了不少知识。准备一一拆解,留下来做备忘。
首先,讲解一下最后一步,就是在Spring启动后自动加载或者调用类方法。如果不使用Spring,我能想到的是使用Listener或者Servlet,让它们之一在web容器启动的时候去调用服务类或处理类的方法。但是,既然使用强大的Spring,使用IoC就是再自然不过的事了。
具体实现:
有两个简单的方法,都是在applicationContext.xml中配置bean
<beanid="templateAnnotationInit"class="com.cnc.apm.handler.ParseTemplateAnnotationHandler"/>
其中,ParseTemplateAnnotationHandler是JavaBean。Spring容器启动后会加载该类,没有指定其他参数,则Spring容器默认通过JavaBean的无参构造函数加载该类。类比,也可以通过指定构造函数的参数,调用特定的含参构造函数。
那如果是指定某个方法呢,则使用<bean>配置init-method属性,如下:
<beanid="templateAnnotationInit"class="com.cnc.apm.handler.ParseTemplateAnnotationHandler"init-method="init"/>
其中init即为初始方法。
小结:
在使用Spring的情况下,应该尽量考虑使用这个优秀的框架提供的方法。这个理念会在这次模块的开发中在后续博文中体现。
更多推荐
所有评论(0)