简体   繁体   中英

<context:annotation-config> and <context:component-scan>

In Spring 3 do I need to have <context:annotation-config> and <context:component-scan> defined in order to enable @PostConstruct?

applicationContext.xml

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

........
...

<context:annotation-config />

I have a method getLazyLoad in my ManagedBean and I would like to use @PostConstruct for this bean.

Thanks

<context:annotation-config> is enough, after that you can instantiate your beans from context.xml and @PostConstruct, @Autowired, @Resource and some other annotations that Spring supports will be processed. Note if you use component-scan annotation-config mode is enabled by default.

In your bean is not having any Annotations at class level related to @Component

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

is not required.

To work with @PostConstruct only <context:annotation-config /> is enough.

EDIT1:

suspose If your bean is having any below annotation

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

then <context:component-scan /> will scan those bean under specified package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM