pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.burns.spring.framework.test</groupId>
<artifactId>spring-framework-test-20220516</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
</project>
程序入口: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关键位置
?
|