当前位置: 首页 > news >正文

广州手机网站建设联系电话微信crm系统软件

广州手机网站建设联系电话,微信crm系统软件,网站工程前端,采集站seo赚钱辅导班学习如何编写Spring Setter依赖注入示例 。 Setter注入是Spring依赖注入的一种 。 Spring支持字段注入#xff0c;Setter注入以及构造函数注入#xff0c;以将依赖项注入Spring托管的bean中。 本教程的范围仅限于Setter注入。 有关Spring依赖注入的更多信息#xff1a; Sp… 学习如何编写Spring Setter依赖注入示例 。 Setter注入是Spring依赖注入的一种 。 Spring支持字段注入Setter注入以及构造函数注入以将依赖项注入Spring托管的bean中。 本教程的范围仅限于Setter注入。 有关Spring依赖注入的更多信息 Spring构造函数依赖注入示例 Spring字段依赖注入示例 Spring依赖注入–字段vs设置器vs构造函数注入 Spring依赖注入和控制反转 考虑一下我们有一个DogsService 它是一个基于Spring的REST服务。 我们将编写一个DogsController DogsService和DogsDao 。 这是一个不执行任何操作的虚拟服务。 这里的目的是了解Spring Dependency Injection如何与Setter方法一起使用。 不知道如何编写Spring Boot Rest Service 阅读 Spring Boot Rest Service 想更多地了解Spring Framework 读这个 Spring框架介绍 Spring框架架构 Spring依赖注入和控制反转 Spring靴休息服务 DogsDao.java 没有字段因此没有依赖性。 我们添加了带有打印消息的无参数构造函数。 package com.amitph.spring.dogs.dao;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 ListDog getAllDogs() {System.out.println(DogsDao.getAllDogs called);return null;} }DogsService.java DogsService取决于DogsDao 。 在下面的类中 setter方法用Autowired注释。 为了查看Setter注入的工作方式我们在setter方法中添加了一条print语句。 除了使用setter方法外我们还添加了无参数构造函数和带有相应打印消息的参数化构造函数 。 package com.amitph.spring.dogs.service;import com.amitph.spring.dogs.dao.DogsDao; import com.amitph.spring.dogs.repo.Dog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.util.List;Component public class DogsService {private DogsDao dao;public ListDog getDogs() {System.out.println(DogsService.getDogs called);return dao.getAllDogs();}Autowiredpublic void setDao(DogsDao dao) {System.out.println(DogsService setter called);this.dao dao;}public DogsService(){System.out.println(DogsService no-arg constructor called);}public DogsService(DogsDao dao) {System.out.println(DogsService arg constructor called);this.dao dao;} }DogsController.java DogsController依赖于DogsService 。 设置器带有Autowired注释并具有打印语句。 除了setter方法外我们还添加了无参数和参数化构造函数以及相应的打印消息。 package com.amitph.spring.dogs.web;import com.amitph.spring.dogs.repo.Dog; import com.amitph.spring.dogs.service.DogsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;RestController RequestMapping(/dogs) public class DogsController {private DogsService service;GetMappingpublic ListDog getDogs() {return service.getDogs();}Autowiredpublic void setService(DogsService service) {System.out.println(DogsController setter called);this.service service;} }应用启动 . ____ _ __ _ _/\\ / ____ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | _ | _| | _ \/ _ | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) ) |____| .__|_| |_|_| |_\__, | / / / /|_||___//_/_/_/:: Spring Boot :: (v2.1.0.RELEASE)2019-02-04 19:06:17.058 INFO 68545 --- [ main] com.amitph.spring.dogs.Application : Starting Application on Amitsofficemac.gateway with PID 68545 (/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:06:17.061 INFO 68545 --- [ main] com.amitph.spring.dogs.Application : No active profile set, falling back to default profiles: default 2019-02-04 19:06:17.670 INFO 68545 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode. 2019-02-04 19:06:17.724 INFO 68545 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 48ms. Found 1 repository interfaces. 2019-02-04 19:06:17.992 INFO 68545 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$4a5366ed] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-02-04 19:06:18.225 INFO 68545 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-02-04 19:06:18.237 INFO 68545 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-02-04 19:06:18.237 INFO 68545 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12 2019-02-04 19:06:18.242 INFO 68545 --- [ main] o.a.catalina.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:06:18.315 INFO 68545 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-02-04 19:06:18.315 INFO 68545 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1205 ms 2019-02-04 19:06:18.339 INFO 68545 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2019-02-04 19:06:18.342 INFO 68545 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: characterEncodingFilter to: [/*] 2019-02-04 19:06:18.342 INFO 68545 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: hiddenHttpMethodFilter to: [/*] 2019-02-04 19:06:18.342 INFO 68545 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: formContentFilter to: [/*] 2019-02-04 19:06:18.342 INFO 68545 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: requestContextFilter to: [/*] 2019-02-04 19:06:18.434 INFO 68545 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2019-02-04 19:06:18.524 INFO 68545 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2019-02-04 19:06:18.645 INFO 68545 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default...] 2019-02-04 19:06:18.690 INFO 68545 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.7.Final} 2019-02-04 19:06:18.691 INFO 68545 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2019-02-04 19:06:18.779 INFO 68545 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final} 2019-02-04 19:06:18.868 INFO 68545 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect 2019-02-04 19:06:19.279 INFO 68545 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit default DogsDao no-arg constructor called DogsService no-arg constructor called DogsService setter called DogsController no-arg constructor called DogsController setter called 2019-02-04 19:06:19.650 INFO 68545 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService applicationTaskExecutor 2019-02-04 19:06:19.681 WARN 68545 --- [ 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 this warning 2019-02-04 19:06:19.856 INFO 68545 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path 2019-02-04 19:06:19.859 INFO 68545 --- [ main] com.amitph.spring.dogs.Application : Started Application in 3.138 seconds (JVM running for 3.647) 春天发生的事情是Spring首先尝试实例化DogsController并发现了对DogsService的依赖。 因此它实例化了DogsService 而后者又依赖于DogsDao 。 因此 DogsDao是第一个实例化的然后是DogsService 最后是DogsController 。 让我们详细看看发生了什么。 第36行调用DogsDao无参数构造函数。 第37行调用DogsService无参数构造函数。 注意参数化的构造函数永远不会被调用 。 第38行调用DogsService Setter。 注入DogsDao实例在第36行创建的位置。 第39行调用DogsController无参数构造函数。 第40行调用DogsController Setter方法。 DogsService实例在第37行中创建。 摘要 您学习了如何在Spring应用程序中编写Setter注入 。 如果使用Setter注入 则使用Autowired注释setter方法。 Spring将首先使用无参数构造函数实例化Bean然后调用setter方法来注入依赖项。 在接下来的教程中我们将了解基于构造函数的注入是如何工作的。 翻译自: https://www.javacodegeeks.com/2019/02/spring-setter-dependency-injection-example.html
http://www.zqtcl.cn/news/983288/

相关文章:

  • 网站开发移动端网络系统软件应用与维护
  • 浙江网站建设营销网站后台管理系统一般用户名是什么
  • 网站 空间 租用wordpress搬家需要修改
  • 做网站推广怎么找客户网站换空间 seo
  • ipad网站开发seo哪家强
  • 昆明网站建设猫咪科技公司资料模板
  • 网站系统开发做网站需要填什么
  • 网站的数据库丢失建筑素材网
  • 个人网站做短视频pathon能做网站开发吗
  • 客户网站制作管理系统网站程序 wap pc 同步
  • 天津手动网站建设调试百度医院网站建设
  • ppt网站源码今天哈尔滨最新通告
  • asp网站乱码广州制作网页设计
  • 调用别人网站的数据库如何开网店卖自己的东西
  • 个人网站做影视网站开发学什么专业
  • 企业名称注册查询官网入口免费seo网站推广
  • 浙江门户网站建设公司个体工商户查询
  • 做网站的注意点赛事竞猜网站开发
  • 现在流行用什么语言做网站ppt设计教程网
  • 高端网站哪种好培训机构不退钱最怕什么举报
  • 青岛个人建站模板wordpress没有链接
  • 网上学习网站有哪些厦门城乡建设局网站
  • 怎样创建网站快捷方式个人制作一个网站的费用
  • 恒信在线做彩票的是什么样的网站软件开发流程管理
  • 网站服务器地址在哪里看艺术学校网站模板
  • 郑州中心站网站建设价格标准新闻
  • 电子商务网站管理互联网营销师主要做什么
  • 门户网站指的是什么凯里网络公司建设网站
  • 网站接入服务商查询0建设营销型网站步骤
  • 长沙如何做百度的网站小型网站建设实训教程