β 1. What is Spring Framework?
Spring is a powerful, lightweight framework for building Java enterprise applications. It promotes loose coupling through Dependency Injection (DI) and offers powerful integrations for web apps, data access, security, AOP, and more.
π Core Features:
- IoC Container (Dependency Injection)
- AOP (Aspect-Oriented Programming)
- Spring MVC (Web framework)
- Spring JDBC/ORM
- Transaction Management
- Spring Security
- Spring Testing
β 2. Dependency Injection (DI) in Spring
β€ What is Dependency Injection?
DI is a design pattern that allows an object to receive its dependencies from an external source rather than creating them itself.
β¨ Benefits:
- Loose coupling
- Better testability
- More maintainable code
β€ DI with XML Configuration
π beans.xml
:
Explanation:
- Declares 2 beans.
employeeService
depends onemployeeDao
.<property>
injectsemployeeDao
intoemployeeService
using its setter method.
<bean id="employeeDao" class="com.app.EmployeeDao" />
<bean id="employeeService" class="com.app.EmployeeService">
<property name="employeeDao" ref="employeeDao" />
</bean>
π EmployeeService.java
:
Explanation:
setEmployeeDao()
is a setter.- Spring calls this and injects the dependency defined in
beans.xml
.
public class EmployeeService {
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao dao) {
this.employeeDao = dao;
}
}
πΉ Annotation-Based DI
π EmployeeDao.java
@Component
public class EmployeeDao {}
π EmployeeService.java
@Service
public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;
}
π AppConfig.java
@Configuration
@ComponentScan("com.app")
public class AppConfig {}
Explanation:
@Component
/@Service
: Marks the class as a Spring-managed bean.@Autowired
: Automatically wiresEmployeeDao
intoEmployeeService
.@ComponentScan
: Scans the package for Spring annotations.
β 3. Bean Scopes in Spring
Scope | Description |
---|---|
singleton | One shared instance per Spring container |
prototype | New instance each time it’s requested |
request | One per HTTP request (Web only) |
session | One per session (Web only) |
π Example:
Explanation:
@Component
: Declares the class as a bean.@Scope("prototype")
: Spring will create a new instance every time this bean is requested (instead of sharing the same one).
@Component
@Scope("prototype")
public class MyBean {}
β 4. Aspect-Oriented Programming (AOP)
β€ What is AOP?
AOP allows you to define cross-cutting concerns (like logging, security, transactions) separately from business logic.
π Example: Logging Aspect
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.app.service.*.*(..))")
public void log(JoinPoint jp) {
System.out.println("Method called: " + jp.getSignature());
}
}
Key Annotations:
@Aspect
@Before
,@After
,@Around
@EnableAspectJAutoProxy
Explanation:
@Aspect
: Declares this class as an AOP aspect.@Before(...)
: Executes before any method incom.app.service.*
package.JoinPoint
: Gives method metadata at runtime.- This is commonly used for logging, auditing, security.
β 5. Spring JDBC (with JdbcTemplate)
β€ What is JdbcTemplate?
A helper class that simplifies database operations.
π UserDao.java
:
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbc;
public void save(User u) {
jdbc.update("INSERT INTO users(name,email) VALUES (?,?)", u.getName(), u.getEmail());
}
}
π DataSource Config:
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setUrl("jdbc:mysql://localhost/test");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
Explanation:
JdbcTemplate
handles low-level JDBC tasks (connection, statement, exception).jdbc.update(...)
: Runs a SQL update/insert.?
: Placeholder for values.DataSource
: Provides database connection details.
β 6. Spring MVC β Web Framework
β€ DispatcherServlet Flow:
Request β DispatcherServlet β Controller β ViewResolver β JSP
π HomeController.java
:
@Controller
public class HomeController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("msg", "Hello from Spring!");
return "hello";
}
}
π hello.jsp
:
<h2>${msg}</h2>
Explanation:
@Controller
: Marks the class as a Spring MVC controller.@RequestMapping("/hello")
: Maps URL/hello
to this method.Model model
: Sends data to the view.model.addAttribute(...)
: Adds data to be shown in JSP.- JSP renders the
msg
value from the controller.
β 7. Form Handling in Spring MVC
π User.java
:
public class User {
private String name;
private String email;
// Getters and setters
}
π UserController.java
:
@GetMapping("/register")
public String showForm(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/save")
public String save(@ModelAttribute("user") User user) {
System.out.println(user.getName() + " - " + user.getEmail());
return "result";
}
π register.jsp
:
<form action="save" method="post">
Name: <input name="name" type="text"/><br/>
Email: <input name="email" type="text"/><br/>
<input type="submit"/>
</form>
Explanation:
@ModelAttribute("user")
: Binds form fields toUser
object.GET /register
: Shows form with an emptyUser
object.POST /save
: Submits form, Spring populates theUser
object.
β 8. Spring Transaction Management
π BankService.java
:
@Service
public class BankService {
@Autowired
private AccountDao dao;
@Transactional
public void transfer(String fromAcc, String toAcc, double amount) {
dao.withdraw(fromAcc, amount);
dao.deposit(toAcc, amount);
}
}
π‘ If any method fails, the transaction is rolled back.
Explanation:
@Transactional
: Spring wraps this method in a transaction.- If
withdraw
succeeds butdeposit
fails, everything rolls back. - Ensures atomicity and consistency.
β 9. Spring Security (In-Memory Auth)
π SecurityConfig.java
:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("{noop}1234").roles("ADMIN");
}
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().formLogin();
}
}
π Add spring-security-web
and spring-security-config
to your dependencies.
Explanation:
@EnableWebSecurity
: Enables Spring Security.inMemoryAuthentication()
: Hardcoded login (admin/1234
).{noop}
: Tells Spring the password is plaintext (for demo only).http.authorizeRequests()
: Protects all endpoints β forces login.
β 10. Spring Profiles (For Environment Configs)
π DevConfig.java
:
@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public DataSource devDataSource() { ... }
}
π ProdConfig.java
:
@Configuration
@Profile("prod")
public class ProdConfig {
@Bean
public DataSource prodDataSource() { ... }
}
π application.properties:
propertiesCopyEditspring.profiles.active=dev
Explanation:
@Profile("dev")
: Used only whendev
profile is active.- Switch between environments without changing code.
β 11. Spring Testing with JUnit
π UserServiceTest.java
:
Explanation:
@RunWith
: Integrates Spring context with JUnit.@ContextConfiguration
: Loads beans fromAppConfig
.@Autowired
: Injects the bean under test.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testSave() {
User u = new User("Test", "[email protected]");
userService.save(u);
}
}
β 12. Spring Best Practices
Best Practice | Why? |
---|---|
Use constructor injection | Promotes immutability and testing |
Keep controller thin, service rich | Follows separation of concerns |
Avoid field injection if possible | Harder to test |
Externalize configuration | Easier environment management |
Use @Service , @Repository properly | Helps AOP and Spring’s stereotypes |
Use @Transactional at service layer | Controls transaction scope cleanly |
π§ Spring Interview Q&A Summary
Question | Answer |
---|---|
What is Spring? | A modular Java framework for enterprise applications |
What is DI? | Injecting dependencies instead of creating them manually |
Singleton vs Prototype? | Singleton = 1 bean instance, Prototype = new bean every time |
What is AOP? | Separates cross-cutting concerns |
Purpose of @Transactional ? | Rollback changes if something fails |
Difference: @Component vs @Service ? | @Service is semantic for business layer |
What is Spring MVC flow? | DispatcherServlet β Controller β ViewResolver β JSP |
JdbcTemplate benefits? | Simplifies DB access |
XML vs Java Config? | Java Config is type-safe and more modern |
Profiles use case? | Environment-specific beans (dev/test/prod) |
β Additional Concepts of Spring
πΉ 1. Bean Lifecycle Callbacks
- init-method / destroy-method in XML
@PostConstruct
and@PreDestroy
in annotation config
π Example:
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
@PreDestroy
public void destroy() {
System.out.println("Bean destroyed");
}
}
π Useful for initializing resources or cleanup.
πΉ 2. FactoryBean Interface
Allows more complex logic when creating beans.
π Example:
public class CarFactory implements FactoryBean<Car> {
public Car getObject() {
return new Car(); // Complex creation logic
}
public Class<?> getObjectType() {
return Car.class;
}
}
Used when bean creation needs dynamic logic.
πΉ 3. ApplicationContext vs BeanFactory
Feature | BeanFactory | ApplicationContext |
---|---|---|
Lazy loading | Yes | No (eager by default) |
Event handling | No | Yes |
Internationalization | No | Yes |
Use
ApplicationContext
in most cases.
πΉ 4. Spring Events
Spring lets beans communicate via events.
π Example:
public class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
@Component
public class MyListener implements ApplicationListener<MyEvent> {
public void onApplicationEvent(MyEvent event) {
System.out.println("Event received!");
}
}
π Good for decoupled communication within app.
πΉ 5. Custom Qualifiers
When multiple beans of the same type exist.
π Example:
@Qualifier("mysqlRepo")
@Autowired
private UserRepository repo;
Or define a custom qualifier:
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
public @interface MyRepo {}
πΉ 6. BeanPostProcessor
Custom logic before and after bean initialization.
π Example:
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String name) {
System.out.println("Before Init: " + name);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String name) {
System.out.println("After Init: " + name);
return bean;
}
}
πΉ 7. Property Placeholder / External Properties
Reading values from .properties
files.
π Example:
db.url=jdbc:mysql://localhost/test
π Java Config:
@PropertySource("classpath:db.properties")
public class AppConfig {
@Value("${db.url}")
private String url;
}
πΉ 8. @Required Annotation
Forcing Spring to inject a property (setter-based injection).
@Required
public void setName(String name) {
this.name = name;
}
π‘ Deprecated in later Spring versions β use constructor injection.
πΉ 9. Spring Integration with Hibernate
Using HibernateTemplate
, LocalSessionFactoryBean
, or Spring ORM.
π XML example:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
...
</bean>
10. π§ SpEL (Spring Expression Language)
. @value
@Value("#{2 * 2}")
private int value;
- @Scheduled
Scheduled(fixedRate = 5000)
public void poll() {
System.out.println("Polling...");
}
- @Cacheable
@Cacheable("users")
public User findById(String id) {
...
}