2012-12-19 33 views
3

在Spring 3中,我是否需要定義<context:annotation-config><context:component-scan>以啓用@PostConstruct?<context:annotation-config>和<context:component-scan>

的applicationContext.xml

<context:component-scan base-package="net.test" /> 

........ 
... 

<context:annotation-config /> 

我有一個方法getLazyLoad在我ManagedBean,我想用@PostConstruct此Bean。

感謝

回答

2

在你的bean並不具有一流水平有關@Component

<context:component-scan base-package="net.test" /> 

任何註釋不是必需的。

要使用@PostConstruct只有<context:annotation-config />就足夠了。

EDIT1:

suspose如果你的bean是有以下任何註釋

@Component, @Repository, @Service, or @Controller. 

然後<context:component-scan />將掃描指定的包下的豆。

+0

爲了掃描軟件包,我還是需要''? – user75ponic

+0

看我的編輯anwser – NPKR

+0

非常感謝解釋。 – user75ponic

3

<context:annotation-config>就夠了,之後你可以從context.xml中和@PostConstruct,@Autowired,@Resource和Spring支持將處理一些其他註釋實例化你的豆。請注意,如果您使用組件掃描註釋 - 配置模式默認情況下處於啓用狀態。

+0

爲了掃描軟件包,我還是需要''? – user75ponic

+0

是的,你可以。這兩個'命令'是分開的。 –

+0

感謝和讚賞。 – user75ponic