2010-08-08 62 views
21

SampleBean:@PostConstruct方法,並沒有所謂的春季

package com.springexample; 

import javax.annotation.PostConstruct; 
import javax.annotation.PreDestroy; 

public class SampleBean { 

    private BeanTypeOne beanOne; 

    private BeanTypeTwo beanTwo; 

    public void init() { 

     System.out.println("This is from the init() method"); 
    } 

    @PostConstruct 
    public void initAnnotation() { 

     System.out.println("This is from the initAnnotation() method"); 

    } 

和配置文件是這樣的:

<bean id="SampleBean" class="com.springexample.SampleBean"> 
    <property name="beanOne" ref="beanOneOne"></property> 
    <property name="beanTwo" ref="beanTwoOne"></property> 
</bean> 

而且我也沒有默認初始化方法屬性設置標籤。

任何機構都可以告訴爲什麼@PostConstruct方法沒有被調用。

回答

41

您需要<context:annotation-config/>(或<context:component-scan/>)才能啓用@PostConstruct處理。

+0

是的,它現在正在工作.. 雅..我記得有些時候,我瞭解到..我們需要有這些元素來識別註釋.. – javanoob 2010-08-08 13:48:02