pom文件
4.0.0
com.burns.spring.framework.test
spring-framework-test-20220516
1.0-SNAPSHOT
8
8
org.springframework.boot
spring-boot-starter-parent
2.5.13
import
pom
org.springframework
spring-context
程序入口:Application
package com.burns.spring.framework.test;
import com.burns.spring.framework.test.component.MessagePrinter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;
@ComponentScan
public class Application {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.pringMessage();
}
}
MessagePrinter
package com.burns.spring.framework.test.component;
import com.burns.spring.framework.test.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
final private MessageService messageService;
@Autowired
public MessagePrinter(MessageService messageService) {
this.messageService = messageService;
}
public void pringMessage() {
System.out.println(this.messageService.getMessage());
}
}
服务接口:MessageService
package com.burns.spring.framework.test.service;
public interface MessageService {
String getMessage();
}
服务实现类:MessageServiceImpl
package com.burns.spring.framework.test.service.impl;
import com.burns.spring.framework.test.service.MessageService;
import org.springframework.stereotype.Service;
@Service
public class MessageServiceImpl implements MessageService {
@Override
public String getMessage() {
return "Hello World!";
}
}
spring注册bean关键位置