在Spring框架中,配置初始化是构建应用上下文的关键步骤,涉及多种配置方式,以下是核心概念和详细实现示例:

XML配置初始化(传统方式)
步骤:
- 创建
applicationContext.xml - 定义Bean及其依赖
- 通过
ClassPathXmlApplicationContext加载配置
<!-- 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="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db"/>
<property name="username" value="root"/>
</bean>
<bean id="userService" class="com.example.UserService">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Java初始化代码:
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService userService = context.getBean(UserService.class);
注解配置(Java Config)
步骤:
- 创建配置类(
@Configuration) - 使用
@Bean定义组件 - 通过
AnnotationConfigApplicationContext加载配置类
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/db");
ds.setUsername("root");
return ds;
}
@Bean
public UserService userService(DataSource dataSource) {
return new UserService(dataSource);
}
}
初始化代码:
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean(UserService.class);
组件扫描(自动装配)
步骤:

- 在配置类启用扫描(
@ComponentScan) - 使用注解标记组件(
@Component,@Service等) - 通过
@Autowired自动注入依赖
@Configuration
@ComponentScan("com.example") // 扫描包路径
public class AppConfig {
@Bean
public DataSource dataSource() { ... }
}
@Service
public class UserService {
private final DataSource dataSource;
@Autowired // 自动注入DataSource
public UserService(DataSource dataSource) {
this.dataSource = dataSource;
}
}
混合配置(XML + 注解)
-
XML引入Java配置:
<beans> <bean class="com.example.AppConfig"/> <!-- 引入Java配置类 --> <context:component-scan base-package="com.example"/> </beans> -
Java配置引入XML:
@Configuration @ImportResource("classpath:legacy-config.xml") // 引入XML文件 public class AppConfig { ... }
Spring Boot自动配置
Spring Boot简化初始化流程:
- 主类使用
@SpringBootApplication(包含@Configuration+@ComponentScan) - 通过
application.properties/application.yml配置参数 - 自动装配条件化Bean(如
@ConditionalOnClass)
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args); // 自动初始化上下文
}
}
# application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/db
username: root
初始化回调方法
-
Bean初始化后执行:
@Bean(initMethod = "init") public DataSource dataSource() { ... } public class HikariDataSource { public void init() { System.out.println("DataSource初始化完成"); } }或使用注解:

@PostConstruct public void init() { ... } -
销毁回调:
@PreDestroy public void cleanup() { ... }
属性外部化配置
使用@PropertySource加载属性文件:
@Configuration
@PropertySource("classpath:app.properties")
public class AppConfig {
@Value("${db.url}")
private String dbUrl;
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(dbUrl);
return ds;
}
}
| 配置方式 | 适用场景 | 核心注解/标签 |
|---|---|---|
| XML配置 | 遗留系统、复杂Bean依赖 | <bean>, <property> |
| Java Config | 现代Spring应用、类型安全配置 | @Configuration, @Bean |
| 组件扫描 | 快速开发、自动装配 | @ComponentScan, @Autowired |
| Spring Boot | 快速启动、约定优于配置 | @SpringBootApplication |
根据项目需求灵活选择或组合配置方式,Spring Boot推荐使用Java Config + 自动装配,大幅减少显式配置。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/286752.html

