文档
Spring Framework IoC 容器示例
目标
演示 Spring IoC 容器的基本用法:Bean 定义、依赖注入、ApplicationContext 启动。
完整代码
1. 服务接口和实现
// MessageService.java
package com.example.service;
public interface MessageService {
String getMessage();
}
// EmailService.java
package com.example.service.impl;
import com.example.service.MessageService;
public class EmailService implements MessageService {
private String sender;
// Setter 注入
public void setSender(String sender) {
this.sender = sender;
}
@Override
public String getMessage() {
return "Email from " + sender;
}
}
// SmsService.java
package com.example.service.impl;
import com.example.service.MessageService;
public class SmsService implements MessageService {
@Override
public String getMessage() {
return "SMS message";
}
}
2. 消费者类
package com.example.consumer;
import com.example.service.MessageService;
public class NotificationConsumer {
private MessageService messageService;
// 构造器注入(推荐)
public NotificationConsumer(MessageService messageService) {
this.messageService = messageService;
}
public void notifyUser() {
System.out.println(messageService.getMessage());
}
}
3. XML 配置 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="emailService" class="com.example.service.impl.EmailService">
<property name="sender" value="noreply@example.com"/>
</bean>
<bean id="smsService" class="com.example.service.impl.SmsService"/>
<bean id="notificationConsumer" class="com.example.consumer.NotificationConsumer">
<constructor-arg ref="emailService"/>
</bean>
</beans>
4. Java Config 配置(注解方式)
package com.example.config;
import com.example.service.MessageService;
import com.example.service.impl.EmailService;
import com.example.consumer.NotificationConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MessageService messageService() {
EmailService service = new EmailService();
service.setSender("noreply@example.com");
return service;
}
@Bean
public NotificationConsumer notificationConsumer() {
return new NotificationConsumer(messageService());
}
}
5. 主程序
package com.example;
import com.example.config.AppConfig;
import com.example.consumer.NotificationConsumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 方式一:XML 配置
ApplicationContext xmlCtx =
new ClassPathXmlApplicationContext("beans.xml");
NotificationConsumer consumer1 =
xmlCtx.getBean(NotificationConsumer.class);
consumer1.notifyUser();
// 方式二:Java 注解配置
ApplicationContext annoCtx =
new AnnotationConfigApplicationContext(AppConfig.class);
NotificationConsumer consumer2 =
annoCtx.getBean(NotificationConsumer.class);
consumer2.notifyUser();
}
}
运行
javac -cp "spring-context-6.1.0.jar:..." com/example/*.java com/example/**/*.java
java -cp ".:spring-context-6.1.0.jar:spring-core-6.1.0.jar:spring-beans-6.1.0.jar:spring-expression-6.1.0.jar:commons-logging-1.2.jar" com.example.Main
预期输出
Email from noreply@example.com
Email from noreply@example.com