GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Spring XML Bean Config vs Annotation-Driven | java.evolved
Enterprise
Intermediate
Spring XML Bean Config vs Annotation-DrivenReplace verbose Spring XML bean definitions with concise annotation-driven configuration in Spring Boot. Code Comparison
✕ Spring (XML)
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userRepository"
class="com.example.UserRepository">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userService"
class="com.example.UserService">
<property name="repository" ref="userRepository"/>
</bean>
</beans>
✓ Spring Boot 3+
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Repository
public class UserRepository {
private final JdbcTemplate jdbc;
public UserRepository(JdbcTemplate jdbc) {
this.jdbc = jdbc;
}
}
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
}
See a problem with this code? Let us know.
Why the modern way wins
🚫
No XML@SpringBootApplication triggers component scanning and auto-configuration, eliminating all XML wiring files. 💉
Constructor injectionSpring injects dependencies through constructors automatically, making beans easier to test and reason about. ⚡
Auto-configurationSpring Boot configures DataSource, JPA, and other infrastructure from the classpath with zero boilerplate. Old Approach
XML Bean Definitions
Modern Approach
Annotation-Driven Beans
Since JDK
17
Difficulty
Intermediate
JDK Support
Spring XML Bean Config vs Annotation-Driven
Available
Widely available since Spring Boot 1.0 (April 2014); Spring Boot 3 requires Java 17+ How it works
Traditional Spring applications wired beans through XML configuration files, declaring each class and its dependencies as verbose <bean> elements. While annotation support existed since Spring 2.5, XML remained the dominant approach until Spring Boot introduced auto-configuration. Spring Boot detects beans annotated with @Component, @Service, @Repository, and @Controller via classpath scanning, satisfies dependencies through constructor injection automatically, and configures infrastructure like DataSource from the classpath — eliminating all XML wiring files. Related Documentation
Proof
Related patterns
Enterprise
Intermediate
EJB versus CDIJava EE
@Stateless
public class OrderEJB {
@EJB
private InventoryEJB inventory;
public void placeOrder(Order order) {
// container-managed transaction
inventory.reserve(order.getItem());
}
}
Jakarta EE 8+
@ApplicationScoped
public class OrderService {
@Inject
private InventoryService inventory;
@Transactional
public void placeOrder(Order order) {
inventory.reserve(order.getItem());
}
}
JDK 11+
→
Enterprise
Intermediate
JNDI Lookup vs CDI InjectionJava EE
public class OrderService {
private DataSource ds;
public void init() throws NamingException {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup(
"java:comp/env/jdbc/OrderDB");
}
public List<Order> findAll()
throws SQLException {
try (Connection con = ds.getConnection()) {
// query orders
}
}
}
Jakarta EE 8+
@ApplicationScoped
public class OrderService {
@Inject
@Resource(name = "jdbc/OrderDB")
DataSource ds;
public List<Order> findAll()
throws SQLException {
try (Connection con = ds.getConnection()) {
// query orders
}
}
}
JDK 11+
→
Enterprise
Intermediate
Manual JPA Transaction vs Declarative @TransactionalJava EE
@PersistenceContext
EntityManager em;
public void transferFunds(Long from, Long to,
BigDecimal amount) {
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Account src = em.find(Account.class, from);
Account dst = em.find(Account.class, to);
src.debit(amount);
dst.credit(amount);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw e;
}
}
Jakarta EE 8+
@ApplicationScoped
public class AccountService {
@PersistenceContext
EntityManager em;
@Transactional
public void transferFunds(Long from, Long to,
BigDecimal amount) {
var src = em.find(Account.class, from);
var dst = em.find(Account.class, to);
src.debit(amount);
dst.credit(amount);
}
}
JDK 11+
→
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |