Spring Boot和依赖注入
Spring Boot中的依赖注入与Spring框架中的依赖注入没有什么不同。我们可以使用任何标准的Spring Framework技术来定义bean及其注入的依赖项。 SpringBoot建议使用@ComponentScan(查找bean)和使用@Autowired(进行构造函数注入)。
Spring Boot依赖注入示例
我们将创建一个简单的Spring Boot独立应用程序,以显示Spring Boot依赖项注入。
对于独立的应用程序,除了需要增加spring-boot-starter-parent的依赖之外,还需要添加spring-boot-starter的依赖。
Maven依赖
<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.theitroad</groupId>
<artifactId>SpringBootProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
豆类
在该示例中,有一个用于下订单的类称为Order,可以从存在RetailStore类的商店中进行购买。订单类对商店有依赖性。
public interface OrderService {
public void buyItems();
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderServiceImpl implements OrderService {
private IStore store;
@Autowired
public OrderServiceImpl(IStore store){
this.store = store;
}
public void buyItems() {
store.doPurchase();
}
}
public interface IStore {
public void doPurchase();
}
@Service
public class RetailStore implements IStore {
public void doPurchase() {
System.out.println("Doing purchase from Retail Store");
}
}
依赖项注入的最佳实践之一是对接口进行编码,这就是为什么要有接口然后是它们的具体实现的原因。这个想法是依靠抽象而不是具体的实现,以减少依赖的僵化。
此外,类还使用@Service注释进行注释,这使这些类有资格进行组件扫描。 @Autowired注释用于通过构造函数注入来注入依赖项。
具有主方法的应用程序类
要引导此春季启动示例,请使用带有@SpringBootApplication注释的类。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.theitroad.springbootproject.service.OrderService;
import com.theitroad.springbootproject.service.OrderServiceImpl;
@SpringBootApplication
public class FirstSpringBootApp {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(FirstSpringBootApp.class, args);
OrderService orderService = ctx.getBean(OrderServiceImpl.class);
orderService.buyItems();
}
}
@SpringBootApplication是一个方便注释,它添加了以下所有注释
1 @ Configuration注释将类标记为应用程序上下文的Bean定义的源。
2 @ EnableAutoConfiguration告诉Spring Boot启用自动配置。 Spring Boot自动配置会尝试根据添加的jar依赖项自动配置Spring应用程序。例如,starter spring-boot-starter-web添加了Tomcat和Spring MVC,因此自动配置假定我们正在开发Web应用程序并相应地设置Spring,其中包括设置DispatcherServlet。
3 @ ComponentScan告诉Spring递归查找此包中的其他组件,配置和服务,并注册它们。所有应用程序组件(@ Component,@ Service,@ Repository,@ Controller等)都将自动注册为Spring Bean。
在将此Application类作为Java应用程序运行时,我们将获得以下输出:
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-08-11 11:48:12.473 INFO 13036 --- [ main] c.k.s.FirstSpringBootApp : Starting FirstSpringBootApp on user with PID 13036 (started by Anshu in F:\theitroad\Spring WorkSpace\SpringBootProject) 2019-08-11 11:48:12.476 INFO 13036 --- [ main] c.k.s.FirstSpringBootApp : No active profile set, falling back to default profiles: default 2019-08-11 11:48:13.414 INFO 13036 --- [ main] c.k.s.FirstSpringBootApp : Started FirstSpringBootApp in 1.478 seconds (JVM running for 2.515) Doing purchase from Retail Store

