Jacob
Jacob

Reputation: 14731

<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

Upvotes: 3

Views: 3312

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

<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.

Upvotes: 3

NPKR
NPKR

Reputation: 5496

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.

Upvotes: 2

Related Questions