Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.sequence;
import org.springframework.stereotype.Component;
@Component
public class SequenceDaoImpl implements SequenceDao {
...
}
Also, you apply this stereotype annotation to the SequenceService class for Spring to detect it. In
addition, you apply the @Autowired annotation to the DAO field for Spring to auto-wire it by type. Note
that because you're using the annotation on a field, you don't need a setter method here.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SequenceService {
@Autowired
private SequenceDao sequenceDao;
...
}
With the stereotype annotations applied to your component classes, you can ask Spring to scan
them by declaring a single XML element: <context:component-scan> . In this element, you need to specify
the package for scanning your components. Then the specified package and all its subpackages will be
scanned. You can use commas to separate multiple packages for scanning.
The previous stereotype is enough to be able to use the bean. Spring will give the bean a name
created by lowercasing the first character of the class and using the rest of the camelcased name for the
bean name. Thus, the following works (assuming that you've instantiated an application context
containing the <context:component-scan> element).
SequenceService sequenceService =
(SequenceService) context.getBean(" sequenceService ");
Note that this element will also register an AutowiredAnnotationBeanPostProcessor instance that can
auto-wire properties with the @Autowired annotation.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xmlns:context=" http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package=
"com.apress.springenterpriserecipes.sequence" />
</beans>
Search WWH ::




Custom Search