javascript
spring 构造函数注入_Spring构造函数依赖注入示例
spring 構(gòu)造函數(shù)注入
歡迎使用Spring構(gòu)造函數(shù)依賴注入示例指南。 基于構(gòu)造器的依賴注入是Spring 依賴注入的一種 。 依賴注入的另一種類型是Setter注入和字段注入。
有關(guān)Spring依賴注入的更多信息:
- Spring二傳手注射的例子
- Spring田間注入
- 依賴注入–構(gòu)造函數(shù)與現(xiàn)場注入
- 依賴注入和控制反轉(zhuǎn)
基于構(gòu)造函數(shù)的依賴注入
它是Spring Dependency Injection的一種 ,其中對象的構(gòu)造函數(shù)用于注入依賴項。 這種注入方式比較安全,因為如果不存在依賴項或無法解決依賴項,則不會創(chuàng)建對象。
要理解, 基于構(gòu)造函數(shù)的依賴注入在Spring中是如何工作的-顯然-我們需要一個Spring應(yīng)用程序。 考慮一下,我們有一個非常簡單的Spring應(yīng)用程序,稱為DogsService ,這是一個虛擬服務(wù)。
 不知道如何編寫Spring Boot Rest Service? 
 閱讀: Spring Boot Rest Service 
 想更多地了解Spring Framework? 
 讀這個: 
- Spring框架介紹
- Spring框架架構(gòu)
- Spring Bean–單例與原型
- Spring自動布線
狗狗
DAO類沒有任何依賴關(guān)系。 我們在print語句中添加了無參數(shù)構(gòu)造函數(shù)。
import com.amitph.spring.dogs.repo.Dog; import org.springframework.stereotype.Component; import java.util.List; @Component public class DogsDao { public DogsDao(){ System.out.println( "DogsDao no-arg constructor called" ); } public List<Dog> getAllDogs() { System.out.println( "DogsDao.getAllDogs called" ); return null ; } }狗服務(wù)
 服務(wù)HAS-A DogsDao 。 服務(wù)類具有setter方法, 無參數(shù)構(gòu)造函數(shù)和帶有相應(yīng)打印語句的參數(shù)化構(gòu)造函數(shù) 。 
 注意:參數(shù)化的構(gòu)造函數(shù)以@Autowrired注釋。 
狗的控制器
 控制器HAS-A DogsService 。 控制器類還具有一個設(shè)置程序,一個無參數(shù)構(gòu)造函數(shù)和一個帶有各自打印語句的參數(shù)化構(gòu)造函數(shù)。 
 注意:參數(shù)化的構(gòu)造函數(shù)以@Autowrired注釋。 
運行應(yīng)用程序
啟動應(yīng)用程序時,我們應(yīng)該在控制臺上看到以下日志。
2019 - 02 - 04 19 : 56 : 46.812 INFO 68906 --- [ main] com.amitph.spring.dogs.Application : Starting Application on Amitsofficemac.gateway with PID 68906 (/Users/aphaltankar/Workspace/personal/dog-service-jpa/out/production/classes started by aphaltankar in /Users/aphaltankar/Workspace/personal/dog-service-jpa) 2019 - 02 - 04 19 : 56 : 46.815 INFO 68906 --- [ main] com.amitph.spring.dogs.Application : No active profile set, falling back to default profiles: default 2019 - 02 - 04 19 : 56 : 47.379 INFO 68906 --- [ main] .sdrcRepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode. 2019 - 02 - 04 19 : 56 : 47.428 INFO 68906 --- [ main] .sdrcRepositoryConfigurationDelegate : Finished Spring Data repository scanning in 45ms. Found --- [ main] .sdrcRepositoryConfigurationDelegate : Finished Spring Data repository scanning in 45ms. Found 1 repository interfaces. 2019 - 02 - 04 19 : 56 : 47.682 INFO 68906 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$EnhancerBySpringCGLIB$86296a04] is not eligible for getting processed by all BeanPostProcessors ( for example: not eligible for auto-proxying) 2019 - 02 - 04 19 : 56 : 47.931 INFO 68906 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019 - 02 - 04 19 : 56 : 47.944 INFO 68906 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019 - 02 - 04 19 : 56 : 47.944 INFO 68906 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/ 9.0 . 12 2019 - 02 - 04 19 : 56 : 47.949 INFO 68906 --- [ main] oacatalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/aphaltankar/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 2019 - 02 - 04 19 : 56 : 48.021 INFO 68906 --- [ main] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019 - 02 - 04 19 : 56 : 48.021 INFO 68906 --- [ main] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 1158 ms 2019 - 02 - 04 19 : 56 : 48.042 INFO 68906 --- [ main] osbwservlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2019 - 02 - 04 19 : 56 : 48.045 INFO 68906 --- [ main] osbwservlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2019 - 02 - 04 19 : 56 : 48.046 INFO 68906 --- [ main] osbwservlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2019 - 02 - 04 19 : 56 : 48.046 INFO 68906 --- [ main] osbwservlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*] 2019 - 02 - 04 19 : 56 : 48.046 INFO 68906 --- [ main] osbwservlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2019 - 02 - 04 19 : 56 : 48.136 INFO 68906 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Starting... 2019 - 02 - 04 19 : 56 : 48.230 INFO 68906 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Start completed. 2019 - 02 - 04 19 : 56 : 48.322 INFO 68906 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2019 - 02 - 04 19 : 56 : 48.366 INFO 68906 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core { 5.3 . 7 .Final} 2019 - 02 - 04 19 : 56 : 48.366 INFO 68906 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2019 - 02 - 04 19 : 56 : 48.461 INFO 68906 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations { 5.0 . 4 .Final} 2019 - 02 - 04 19 : 56 : 48.546 INFO 68906 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect 2019 - 02 - 04 19 : 56 : 48.960 INFO 68906 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' DogsDao no-arg constructor called DogsService arg constructor called DogsController arg constructor called 2019 - 02 - 04 19 : 56 : 49.304 INFO 68906 --- [ main] ossconcurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019 - 02 - 04 19 : 56 : 49.330 WARN 68906 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2019 - 02 - 04 19 : 56 : 49.479 INFO 68906 --- [ main] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019 - 02 - 04 19 : 56 : 49.482 INFO 68906 --- [ main] com.amitph.spring.dogs.Application : Started Application in 3.003 seconds (JVM running for 3.521 )- 第27行:正如預(yù)期的那樣,調(diào)用了DAO的無參數(shù)構(gòu)造函數(shù)。
- 第28行:調(diào)用了DogsService的參數(shù)化構(gòu)造函數(shù), DogsService在第27行創(chuàng)建了DAO實例。
- 第29行:調(diào)用控制器的參數(shù)化構(gòu)造函數(shù),并在第28行創(chuàng)建一個服務(wù)實例。
請注意,Spring 不會調(diào)用設(shè)置器和無參數(shù)構(gòu)造器 。 依賴項是通過 構(gòu)造函數(shù) 純粹注入的 。 此方法優(yōu)于Spring中的Spring Setter注入和Field注入 。
摘要
在這個Spring構(gòu)造函數(shù)依賴注入示例指南中,您學(xué)習(xí)了基于構(gòu)造函數(shù)的依賴注入在Spring應(yīng)用程序中如何工作。 我們還使用構(gòu)造函數(shù)注入編寫了可執(zhí)行代碼。
當(dāng)構(gòu)造函數(shù)用于在對象上設(shè)置實例變量時,稱為構(gòu)造函數(shù)注入。 在深入使用Spring Framework之前,重要的是要了解Setter注入與字段注入與構(gòu)造函數(shù)注入之間的區(qū)別 。
快樂編碼!
翻譯自: https://www.javacodegeeks.com/2019/02/spring-constructor-dependency-injection-example.html
spring 構(gòu)造函數(shù)注入
總結(jié)
以上是生活随笔為你收集整理的spring 构造函数注入_Spring构造函数依赖注入示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 步步高平板电脑多少钱(步步高平板电脑哪个
- 下一篇: 苹果12预计售价(苹果12预计售价多少钱
