Spring and Spring Boot Notes
1. Creating Spring Project with Maven
- Use Spring Boot starter Maven archetype
- Add Spring context dependency in pom.xml
2. Creating IOC Container
- Use ApplicationContext interface
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
- To get managed class object:
context.getBean(Dev.class);
3. BeanFactory vs ApplicationContext
- BeanFactory is basic; ApplicationContext is advanced and pre-loads beans.
4. XML Configuration for Beans
<bean id="dev" class="com.package.Dev">
<property name="age" value="12"/>
</bean>
5. Setter Injection
- Uses <property> tag in XML
6. Constructor Injection
<bean>
<constructor-arg index="0" value="16"/>
</bean>
public Dev(int age) {
this.age = age;
}
7. Autowire and Primary
- Use autowire="byType"
- Use primary="true" to resolve ambiguity