Java Reference
In-Depth Information
public void setSequenceDao(SequenceDao sequenceDao) {
this.sequenceDao = sequenceDao;
}
public String generate(String sequenceId) {
Sequence sequence = sequenceDao.getSequence(sequenceId);
int value = sequenceDao.getNextValue(sequenceId);
return sequence.getPrefix() + value + sequence.getSuffix();
}
}
Finally, you have to configure these components in the bean configuration file to make the
sequence generator application work. You can auto-wire your components to reduce the amount of
configurations.
<beans ...>
<bean id="sequenceService"
class="com.apress.springenterpriserecipes.sequence.SequenceService"
autowire="byType" />
<bean id="sequenceDao"
class="com.apress.springenterpriserecipes.sequence.SequenceDaoImpl" />
</beans>
Then you can test the preceding components with the following Main class:
package com.apress.springenterpriserecipes.sequence;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
SequenceService sequenceService =
(SequenceService) context.getBean("sequenceService");
System.out.println(sequenceService.generate("IT"));
System.out.println(sequenceService.generate("IT"));
}
}
Scanning Components Automatically
The component scanning feature provided by Spring since version 2.5 can automatically scan, detect,
and instantiate your components from the classpath. By default, Spring can detect all components
with a stereotype annotation. The basic annotation type that denotes a Spring-managed component is
@Component . You can apply it to your SequenceDaoImpl class.
Search WWH ::




Custom Search