The following app-context.xml can be replaced with the following SpringConfig.java The MainApp.java code needs to be modified as follows: You will also need the CGLib jar This can be added in the Pom as follows: or downloaded from cglib.sourceforge.net
Spring Annotations @Qualifier or @Resource
What Doesn't Change app-context.xml Student.java What Changes Profile - using @Qualifier Profile - using @Resource
Spring Annotations
Annotations include: @Required @Autowired @Qualifier @Resource @PostConstruct @PreDestroy Autowired Annotations @AutoWired @AutoWired(required=false) @AutoWired @Qualifier("student1") @Resource(name="student1") AutoWired = required by default
Spring Bean Life Cycle
Initialization Callbacks Destruction Callbacks Initialization Callbacks public class Example1 implements InitializingBean { public void afterPropertiesSet() { } } <bean id="exampleBean" class="Example2" init-method="init"/> public class Example2 { public void init() { } } Destruction Callbacks public class Example1 implements DisposableBean { public void destroy() { } } <bean id="exampleBean" destroy-method="destroy"/> public class Example2 { public void... Continue Reading →
Spring Bean Scopes
singleton (default) only one instance per Spring Container prototype any number of instances request scoped to an HTTP request (web-aware Spring ApplicationContext) session scoped to an HTTP session (web-aware Spring ApplicationContext) global-session scoped to a global HTTP session (web-aware Spring ApplicationContext) <bean id="helloWorld" class="Hello" scope="singleton"> </bean> <bean id="helloWorld" scope="prototype"> </bean>
Spring Configuration Methods
XML based config file Annotation-based configuration Java-based configuration XML based config file
Spring Bean Attributes
<bean id="my bean" class="MyClass"/> class name scope constructor-arg properties auto wiring mode lazy-initialization mode initialisation method destruction method
Jars Required for Core Spring
antlr-runtime commons-logging org.springframework.aop org.springframework.aop org.springframework.asm org.springframework.aspects org.springframework.beans org.springframework.context org.springframework.context.support org.springframework.core org.springframework.expression