Overview
Why Spring Configuration with Annotations?
- XML configuration can be verbose
- Easy to configure your Spring beans with Annotations
- Annotations minimize the XML configuration
Spring Annotation Based Configuration Basics
How to enable annotation-based wiring?
<?xml version = "1.0" encoding = "UTF-8"?>
<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:annotation-config/>
<!-- bean definitions go here -->
</beans>
- @Required
- @Autowired
- @Qualifier
- @Primary
- @Resource
- @PostConstruct and @PreDestroy (These are JSR-250 annotations)
@Required
This annotation simply indicates that the setter method must be configured to be dependency-injected with a value at configuration time.
@Autowired
Constructor injection
class Car {
private Engine engine;
@Autowired
Car(Engine engine) {
this.engine = engine;
}
}
Setter injection
class Car {
private Engine engine;
@Autowired
void setEngine(Engine engine) {
this.engine = engine;
}
}
Field injection
class Car {
@Autowired
private Engine engine;
}
@Primary Annotation
@Configuration
public class Config {
@Bean
public Connection mySQLConnection() {
return new MySQLConnection();
}
@Bean
public Connection oracleConnection() {
return new OracleConnection();
}
}
@Qualifier
- This annotation helps fine-tune annotation-based autowiring. There may be scenarios when we create more than one bean of the same type and want to wire only one of them with a property. This can be controlled using @Qualifier annotation along with the @Autowired annotation.
- The @Qualifier is used to resolve ambiguous dependencies i.e, it helps @Autowired annotations to choose one of the dependency.
@Autowired
@Qualifier("datasource")
private DataSource datasource;
@Autowired
@Qualifier("datasource1")
private DataSource datasource;
@Resource
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
@PostConstruct and @PreDestroy
Consider DatabaseInitiaizer bean, whose init() and destroy() methods are annotated with @PostConstruct and @PreDestroy annotations respectively.
DatabaseInitiaizer.java
package net.javaguides.spring;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
@Component
public class DatabaseInitiaizer {
private List < User > listOfUsers = new ArrayList < > ();
@PostConstruct
public void init() {
User user = new User(1, "User");
User user1 = new User(2, "Admin");
User user2 = new User(3, "SuperAdmin");
listOfUsers.add(user);
listOfUsers.add(user1);
listOfUsers.add(user2);
System.out.println("-----------List of users added in init() method ------------");
for (Iterator < User > iterator = listOfUsers.iterator(); iterator.hasNext();) {
User user3 = (User) iterator.next();
System.out.println(user3.toString());
}
// save to database
}
@PreDestroy
public void destroy() {
// Delete from database
listOfUsers.clear();
System.out.println("-----------After of users removed from List in destroy() method ------------");
for (Iterator < User > iterator = listOfUsers.iterator(); iterator.hasNext();) {
User user3 = (User) iterator.next();
System.out.println(user3.toString());
}
}
}
Are annotations better than XML for configuring Spring?
Conclusion
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment