Annotation-based IOC Configuration

XML Configuration

First, you need to scan the packages.

<!-- Add namespace -->
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan package -->
    <context:component-scan base-package="xyz.aiinirii"/>

Common Annotations

@Component

@Component("userService")
public class UserService {
    // ...
}

@Service

@Service
public class UserService {
    // ...
}

@Repository

@Repository
public class UserDao {
    // ...
}

@Controller

@Controller
public class UserController {
    // ...
}

@Autowired

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
}

@Qualifier

@Service
public class UserService {
    @Autowired
    @Qualifier("userDaoImpl")
    private UserDao userDao;
}

@Value

@Component
public class ValueDemo {
    @Value("test")
    private String str;
    
    @Value("${jdbc.url}")
    private String jdbcUrl;
}

Summary

Annotation-based configuration reduces XML files and makes development more convenient.