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

购物网站功能模块图百度浏览器app

购物网站功能模块图,百度浏览器app,手机网站建设讯息,随州广水疫情最新消息目录 1.什么是DI? 2.依赖注入的三种⽅式 2.1属性注⼊ 2.2构造⽅法注⼊ 2.3Setter 注⼊ 2.4三种注⼊优缺点分析 3.Autowired存在问题 1.什么是DI? DI: 依赖注⼊ 依赖注⼊是⼀个过程,是指IoC容器在创建Bean时, 去提供运⾏时所依赖的资源,⽽资源指的…

目录

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.hkea.cn/news/349743/

相关文章:

  • 网站总是跳转dede58seo对网络推广的作用是
  • seo排名怎么提高seo排名优化软件有用
  • 江门论坛建站模板黑帽seo联系方式
  • 政府网站信息内容建设专项检查搜索引擎排名优化seo课后题
  • 个人做的好的淘宝客网站软文营销推广
  • 城乡建设委员会网站河北seo推广公司
  • 某网站栏目策划2022十大热点事件及评析
  • 德清网站建设中心优化大师官方免费下载
  • 生日网页制作免费网站制作代做网页设计平台
  • 学校类网站特点游戏优化大师官网
  • 手机电视网站大全河南网站建设定制
  • zblog做的商城网站上海有实力的seo推广咨询
  • 免费网站模板psd网络营销的整体概念
  • 网站模板下载破解版环球军事新闻最新消息
  • 徐汇苏州网站建设东莞免费建站公司
  • 厦门网站建设哪家强深圳网站维护
  • 政府网站新媒体平台建设关键词权重查询
  • 重庆网站建设制作公司百度客服人工在线咨询电话
  • 微信公众号平台入口官网奶盘seo伪原创工具
  • 泉州网站建设公司推荐宁德市地图
  • 大厂县住房和城乡建设局网站刷百度指数
  • 低代码开发平台优缺点昆山seo网站优化软件
  • 网站开发年终总结网络营销战略的内容
  • 建立门户网站的意义营销推广网
  • 网站建设网站软件有哪些百度推广开户费用标准
  • 找家装修公司家装吉林seo外包
  • 保定医疗网站建设公司会计培训班初级费用
  • 最好的销售管理系统seo发帖网站
  • 德州乐陵德州seo公司seo批量建站
  • 贵州省建设监理协会官方网站seo代运营