Spring Controller中的Init方法(注释版本)
我将控制器转换为新的注释版本。 在旧版本中,我使用以下方法在springmvc-servlet.xml中指定init方法:
<beans> <bean id="myBean" class="..." init-method="init"/> </beans> 我如何使用注释版本来指定init方法?
您可以使用
 @PostConstruct public void init() { // ... } 
 或者你可以让你的类实现InitializingBean接口来提供一个callback函数( afterPropertiesSet() ),当bean被构造时,ApplicationContext将调用它。 
 public class InitHelloWorld implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("BeforeInitialization : " + beanName); return bean; // you can return any other object as well } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("AfterInitialization : " + beanName); return bean; // you can return any other object as well } }