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

什么样的网站空间做电影网站不卡上海网站建设企业建站

什么样的网站空间做电影网站不卡,上海网站建设企业建站,网站建设外包被骗,广州高端品牌网站建设目录 1.什么是DI? 2.依赖注入的三种⽅式 2.1属性注⼊ 2.2构造⽅法注⼊ 2.3Setter 注⼊ 2.4三种注⼊优缺点分析 3.Autowired存在问题 1.什么是DI? DI: 依赖注⼊ 依赖注⼊是⼀个过程#xff0c;是指IoC容器在创建Bean时, 去提供运⾏时所依赖的资源#xff0c;⽽资源指的…目录 1.什么是DI? 2.依赖注入的三种⽅式 2.1属性注⼊  2.2构造⽅法注⼊ 2.3Setter 注⼊ 2.4三种注⼊优缺点分析 3.Autowired存在问题  1.什么是DI? DI: 依赖注⼊ 依赖注⼊是⼀个过程是指IoC容器在创建Bean时, 去提供运⾏时所依赖的资源⽽资源指的就是对象 简单来说, 就是把对象取出来放到某个类的属性中. 关于依赖注⼊, Spring也给我们提供了三种⽅式: 1.属性注⼊(Field Injection) 2.构造⽅法注⼊(Constructor Injection) 3.Setter 注⼊(Setter Injection) 都是使用Autowired实现的。  接下来我们分别来看. 2.依赖注入的三种⽅式 都是使用Autowired实现的  我们接下来会频繁使用到UserService类和UserController类以及获取 UserController 中的 sayHi⽅法。 1.UserService代码 import org.springframework.stereotype.Service;Service public class UserService {public void doService(){System.out.println(doService~~~~);} } 2.UserController类依照我们选择的注入⽅式决定。 3.获取 UserController 中的 sayHi⽅法 ​ import com.wh.ioc.Controller.UserController; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;//ComponentScan(basePackages ~~~~~) SpringBootApplication public class SpringIocApplication {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(SpringIocApplication.class, args);UserController bean context.getBean(UserController.class);bean.sayHi();} }​ 2.1属性注⼊  UserController类的实现代码如下 import com.wh.ioc.Service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {Autowiredprivate UserService userService;public void sayHi(){userService.doService();System.out.println(UserController Hi);} } 2.2构造⽅法注⼊ import com.wh.ioc.Service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {private UserService userService;public UserController() {}Autowiredpublic UserController(UserService userService) {this.userService userService;}public void sayHi(){userService.doService();System.out.println(UserController Hi);} } 注意   如果类只有⼀个构造⽅法那么 Autowired 注解可以省略如果类中有多个构造⽅法 那么需要添加上 Autowired 来明确指定到底使⽤哪个构造⽅法。 补充 关于构造函数规范 1.当我们添加有参构造函数时一定也要把无参构造函数也添加上。 2.依赖注入时就算只有一个构造函数也要添加Autowired注解 2.3Setter 注⼊ 注解一定不能少 import com.wh.ioc.Service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {private UserService userService;Autowiredpublic void setUserService(UserService userService) {this.userService userService;}public void sayHi(){userService.doService();System.out.println(UserController Hi);} } 2.4三种注⼊优缺点分析 1.属性注⼊ 优点: 简洁使⽤⽅便 缺点 1 只能⽤于 IoC 容器 如果是⾮ IoC 容器不可⽤并且只有在使⽤的时候才会出现 NPE空指 针异常 2 不能注⼊⼀个Final修饰的属性 2.构造函数注⼊(Spring 4.X推荐) 优点: 1注⼊的对象不会被修改 2可以注⼊final修饰的属性 3依赖对象在使⽤前⼀定会被完全初始化因为依赖是在类的构造⽅法中执⾏的⽽构造⽅法 是在类加载阶段就会执⾏的⽅法 4通⽤性好, 构造⽅法是JDK⽀持的, 所以更换任何框架,他都是适⽤的 缺点 注⼊多个对象时, 代码会⽐较繁琐  3.Setter注⼊(Spring 3.X推荐)   优点: ⽅便在类实例之后, 重新对该对象进⾏配置或者注⼊ 缺点: 1.不能注⼊⼀个Final修饰的属性 2.注⼊对象可能会被改变, 因为setter⽅法可能会被多次调⽤, 就有被修改的⻛险  3.Autowired存在问题  我们接下来用打印学生信息的例子来说明 Student类 import lombok.Data;Data public class Student {private String name;private Integer id;public Student() {}public Student(String name) {this.name name;}public Student(String name, Integer id) {this.name name;this.id id;} } BeanConfig类import com.wh.ioc.Model.Student; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class BeanConfig {Beanpublic Student StudentInfo() {return new Student(wh,01);}Beanpublic Student StudentInfo2() {return new Student(Bob,02);} }DomeController类 import com.wh.ioc.Model.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class DomeController {Autowiredprivate Student student;public void say(){System.out.println(student);} }启动类 import com.wh.ioc.Controller.DomeController; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;//ComponentScan(basePackages ~~~~~) SpringBootApplication public class SpringIocApplication {public static void main(String[] args) {ConfigurableApplicationContext context SpringApplication.run(SpringIocApplication.class, args);DomeController bean context.getBean(DomeController.class);bean.say();} } 运行 报错的原因是⾮唯⼀的 Bean 对象  解释 Autowired是先按照类型去注入匹配到多个对象时再按照名称去注入。 如果我们明确注入对象的名称则可以正确打印该学生信息。 DomeController类代码 import com.wh.ioc.Model.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class DomeController {Autowiredprivate Student StudentInfo2;public void say(){System.out.println(StudentInfo2);} }结果 那当我们没有明确注入对象的名称又想得到正确结果我们可以怎么做 有以下⼏种解决⽅案 1.Primary 2.Qualifier 3.Resource 1. Primary 使⽤Primary注解当存在多个相同类型的Bean注⼊时加上Primary注解来确定默认的实现。 直接加到Bean注⼊的方法上。 Primary Bean public Student StudentInfo() {return new Student(wh, 01); } 2.Qualifier 使⽤Qualifier注解指定当前要注⼊的bean对象。 在Qualifier的value属性中指定注⼊的bean 的名称。 Qualifier注解不能单独使⽤必须配合Autowired使⽤ Controller public class DomeController {Qualifier(StudentInfo2)Autowiredprivate Student student;public void say(){System.out.println(student);} } 3.Resource注解 本身就是依赖注入注解是按照bean的名称进⾏注⼊。 Controller public class DomeController {Resource(name StudentInfo)private Student student;public void say(){System.out.println(student);} }那Autowird 与 Resource的区别是 1.Autowired 是spring框架提供的注解⽽Resource是JDK提供的注解  2.Autowired 默认是按照类型注⼊⽽Resource是按照名称注⼊. 相⽐于 Autowired 来说 Resource ⽀持更多的参数设置例如 name 设置根据名称获取 Bean 以上为我个人的小分享如有问题欢迎讨论  都看到这了不如关注一下给个免费的赞
http://www.tj-hxxt.cn/news/132801.html

相关文章:

  • 公司建设网站费用有哪些学校的网站做的好处
  • 容桂做pc端网站做网站效果图
  • 柳州建设网官方网站强化 门户网站建设
  • 网站暂停怎么做网站建设及制作
  • 北京鑫创网站建设网上书城网站开发的结论和不足
  • 安徽网站推广营销设计商城源码购买
  • 池州建设网站58同城网站建设排名
  • 怎么查看网站备案商宜宾微信网站建设
  • 网站怎么做企业只有做推广才能搜索到网站吗
  • 网站后台添加文章后怎么不显示网站设计客户需求
  • 自建免费网站瀑布流资源网站模板
  • 自助网站建设 网易免费发布信息网站大全
  • 有没有免费的手机网站建设九尾狐建站网址
  • 教我做网站网商之家
  • 电子商务网站建设需要什么沧州网站建设设计
  • 中国建设门户网站网络营销相关理论有哪些
  • 佛山制作网站公司哪家好培训计划模板
  • asp.net建立网站泰国做网站网站要判几年
  • 赣州网站建设优化服务什么网站做贸易好
  • 如何在凡科上做网站做示意图的网站
  • 广州网站建设找新际不会做网站如何做seo
  • 佛山微信网站开发合肥市网站建设
  • 网站站长统计怎么做pv3d 优秀网站
  • 陕西省建设监理协会查询官方网站wordpress媒体库不显示图片
  • 服务周到的上海网站建设公司微信小程序支付功能开发
  • 有哪些公司建设网站wordpress 显示多媒体
  • 网站的优势和劣势潍坊企化网站建设
  • 网站前端开发得会什么软件旅游网站经营模式
  • 网站正在建设中威海网站优化
  • 淄博张店网站建设全网搜索