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

个人网站主办者名称网推接单平台有哪些

个人网站主办者名称,网推接单平台有哪些,网站工作有哪些内容,做推广便宜的网站有哪些DI依赖注入 声明了一个成员变量(对象)之后,在该对象上面加上注解AutoWired注解,那么在程序运行时,该对象自动在IOC容器中寻找对应的bean对象,并且将其赋值给成员变量,完成依赖注入。 AutoWire…

DI依赖注入

声明了一个成员变量(对象)之后,在该对象上面加上注解@AutoWired注解,那么在程序运行时,该对象自动在IOC容器中寻找对应的bean对象,并且将其赋值给成员变量,完成依赖注入。

@AutoWired依赖注入的常见方式

1.属性注入

直接使用@AutoWired进行属性注入:

@Autowired
private UserService userService;

这是最简单的依赖注入方式,其优点是:代码简洁,可以方便快速的开发。其缺点是:直接使用属性注入,隐藏了各类之间的依赖关系:Controller是依赖了Service的,但是在类的结构层面无法看出二者的关联。还有可能会破坏类的封装性:按照封装性的解释来看,我们需要将成员属性设置为私有,并对外提供对应的set/get方法;但是直接使用属性注入,没有对外提供set方法,直接对其赋值,在底层是通过反射对其进行赋值的,实际上是破坏了面向对象的封装性原则的。

2.构造函数注入

通过构造函数的方式完成对成员变量的依赖注入:

private final UserService userService;
@Autowired
public UserController(UserService userService) {this.userService = userService;
}

相对于属性注入而言,构造函数注入就能够清晰的看到各类之间的依赖关系,并且基于构造函数注入,可以将userService设置为final,更加安全。但是假如是该类依赖了多个其他的类,都交给IOC容器管理,那么在书写构造方法注入的时候,构造方法的参数将十分多,构造方法将十分臃肿。

注意:如果该类只有一个构造函数,那么该构造函数上的@Autowired注解可以省略

3.setter注入

通过set方法完成对成员变量的依赖注入:

private UserService userService;
@Autowired
public void setUserService(UserService userService) {this.userService = userService;
}

优点和构造方法注入类似(但是不能将成员变量设置为final),缺点是需要额外提供set方法,编码繁琐。这种setter注入在开发中基本上不会使用。

在项目开发中,Spring官方推荐构造函数注入,因为其很好的保证了面向对象的特性,并且安全性得到很好的保障;但是在开发中大部分项目都喜欢使用属性注入,因为其简洁、方便开发。所以说要根据项目的具体要求而判断,在简洁性和规范性之间进行取舍。(setter注入基本不用)

使用@AutoWired注解的注意事项

类型注入

@Autowired注解在进行依赖注入时,默认是依据类型进行注入操作。然而,倘若存在需要注入的对象有多个对应的 bean实例时,就会引发错误:

这是第一个UserService的bean

package com.wzb.service.impl;import com.wzb.dao.UserDao;
import com.wzb.pojo.User;
import com.wzb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;/*** Service层实现类**/
@Component("newName")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;// 获取用户数据的代码不能写在此处,类的成员变量初始化是在类的实例化阶段进行的,此时可能@AutoWired注入还未完成,导致Null// private final List<String> lines = userDao.findUser();public List<User> findUser() {List<String> lines = userDao.findUser();List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]);String username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, username, password, name, age, updateTime);}).collect(Collectors.toList());return userList;}
}

这是第二个UserService对象的bean

package com.wzb.service.impl;import com.wzb.dao.UserDao;
import com.wzb.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;/*** 用于测试用的bean* */
@Component
public class UserServiceImpl2 implements UserService{@Autowiredprivate UserDao userDao;public List<User> findUser() {List<String> lines = userDao.findUser();List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]) + 200;String username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, username, password, name, age, updateTime);}).collect(Collectors.toList());return userList;}
}

此时Controller中需要依赖UserService这个类,但是IOC容器中有两个这个对象的bean,此时如果直接启动,程序将会直接报错:

 报错信息:

根据报错信息的描述来看,是因为UserController需要一个bean(也就是UserService的实现类),但是在IOC容器中找到了两个该对象的bean实例,不知道应该注入哪一个,所以说就报错了。并且还在Action中提供了一个解决方法:使用@Primary、@Qualifier注解。这证明了不能直接将同一个对象的多个bean加入IOC容器管理,如果一个对象有多个bean都需要给IOC容器管理,那么就需要使用其他注解,来成功找到需要的bean并注入。

解决方法

@Primary

假如需要注入的对象在IOC容器中存在多个bean实例,那么就可以在想要被注入的bean上添加注解@Primary:

使用@Primary注解,指定注入的UserService bean实例是UserServiceImpl2,将服务启动结合前端页面查看结果:

 

 

发现用户的id都加了200,说明此时注入的UserService实例bean是UserServiceImpl2,@Primary注解成功指定了注入的bean实例。

@Qualifier

@Qualifier注解需要配合@AutoWired注解使用,@Qualifier注解是在声明对象时使用,可以通过注解名(默认是类名小写)指定需要注入的bean实例对象。

通过@Qualifier注解和@AutoWired注解结合使用,指定需要注入UserService的bean实例是UserServiceImpl(注意,bean名字是类名小写):

 

 

发现通过@Qualifier注解和@AutoWired注解结合使用,成功将UserService需要注入的bean实例指定为了UserServiceImpl。

重要一点

此时UserServiceImpl2上面的@Primary注解还在,但是注入的是UserServiceImpl,说明@Qualifiler的优先级高于@Primary。

@Resource

@Resource注解是在声明对象时使用,单独使用,通过注解名指定需要注入的bean实例:

@Resource注解不是Spring提供的,是JavaEE规范中提供的,使用时需要指定name = "bean名",同样,此时UserServiceImpl2的@Primary注解还在,但是注入的是@Resource注解指定的bean实例,所以说@Resource注解的优先级也高于@Primary注解。

但是假如将@Resource和@Qualifier注解一起用:

 

 

其注入的bean是@Resource注解指定的,说明@Resource的优先级高于@Qualifier;可能是因为原生JavaEE的优先级高于SpringBoot框架的缘故。

@Resource和@AutoWired都可以实现依赖注入,其二者区别主要有两点:1.@AutoWired是Spring框架提供的,而@Resource是JavaEE提供的,二者的出处不同;2.@AutoWired是默认按照类型注入的,但@Resource是默认按照bean的名称进行注入的。

总的而言,假如说一个类在IOC容器中存在多个bean实例,那么无法直接使用,因为不知道该选择哪个bean进行注入,需要添加注解指定需要注入哪个bean。


文章转载自:
http://sulfathiazole.c7497.cn
http://dirndl.c7497.cn
http://laminose.c7497.cn
http://coppersmith.c7497.cn
http://bounce.c7497.cn
http://phosphatic.c7497.cn
http://impudence.c7497.cn
http://sacrificial.c7497.cn
http://pathos.c7497.cn
http://panplegia.c7497.cn
http://bilection.c7497.cn
http://ida.c7497.cn
http://coparcener.c7497.cn
http://dryasdust.c7497.cn
http://arrears.c7497.cn
http://hesternal.c7497.cn
http://mondo.c7497.cn
http://roc.c7497.cn
http://amortise.c7497.cn
http://datival.c7497.cn
http://earlship.c7497.cn
http://cuboid.c7497.cn
http://honolulu.c7497.cn
http://expiration.c7497.cn
http://machineman.c7497.cn
http://whirleybird.c7497.cn
http://smalti.c7497.cn
http://rejuvenator.c7497.cn
http://indisposition.c7497.cn
http://wield.c7497.cn
http://mucker.c7497.cn
http://ethereality.c7497.cn
http://maintenance.c7497.cn
http://unadmitted.c7497.cn
http://usts.c7497.cn
http://monasticism.c7497.cn
http://uralite.c7497.cn
http://pstn.c7497.cn
http://unsavoury.c7497.cn
http://exemplar.c7497.cn
http://unaptly.c7497.cn
http://plenilune.c7497.cn
http://brier.c7497.cn
http://undisguisedly.c7497.cn
http://quadriennial.c7497.cn
http://duodecimo.c7497.cn
http://capernaum.c7497.cn
http://commercialese.c7497.cn
http://skysweeper.c7497.cn
http://strangulate.c7497.cn
http://harmonia.c7497.cn
http://zygosperm.c7497.cn
http://planner.c7497.cn
http://cysteamine.c7497.cn
http://contralto.c7497.cn
http://rollback.c7497.cn
http://daphne.c7497.cn
http://flighty.c7497.cn
http://exterritorial.c7497.cn
http://toxigenic.c7497.cn
http://wheatear.c7497.cn
http://dustpan.c7497.cn
http://sebum.c7497.cn
http://herodian.c7497.cn
http://puffiness.c7497.cn
http://clef.c7497.cn
http://crowned.c7497.cn
http://kampala.c7497.cn
http://faecal.c7497.cn
http://cautiously.c7497.cn
http://alicyclic.c7497.cn
http://kevlar.c7497.cn
http://equilibrant.c7497.cn
http://enlink.c7497.cn
http://ostracod.c7497.cn
http://wanta.c7497.cn
http://entangle.c7497.cn
http://lumpily.c7497.cn
http://artificial.c7497.cn
http://porphyroid.c7497.cn
http://anthemion.c7497.cn
http://declare.c7497.cn
http://news.c7497.cn
http://consociate.c7497.cn
http://overcertify.c7497.cn
http://untogether.c7497.cn
http://fratching.c7497.cn
http://ductility.c7497.cn
http://safedeposit.c7497.cn
http://nonfiltered.c7497.cn
http://samoa.c7497.cn
http://gibbet.c7497.cn
http://innsbruck.c7497.cn
http://intwist.c7497.cn
http://electrothermics.c7497.cn
http://mechanician.c7497.cn
http://litteratrice.c7497.cn
http://outkitchen.c7497.cn
http://saxe.c7497.cn
http://gemmiform.c7497.cn
http://www.zhongyajixie.com/news/97221.html

相关文章:

  • 东营 网站 建设沈阳网站seo排名公司
  • 长沙服务专业的建网站百度百度推广
  • 丹阳做公司网站的河北网站seo地址
  • 网页导航视频网站在线制作教程哪些平台可以打小广告
  • 中小企业网站建设与推广广告类的网站
  • 网站建站网站怎么样网站优化的方法有哪些
  • 电脑十大免费游戏网站排名优化网站建设
  • 青岛建设银行网站首页电销系统
  • 网站收录少的原因怎么推广产品
  • 上海怎么做网站谷歌推广开户
  • 网站维护 网站建设属于什么重庆发布的最新消息今天
  • 做微商在哪个网站打广告好优化方案模板
  • 学做网站开发要1万6网络营销八大职能
  • 网站建设公司华网天下买赠两年怎么安装百度
  • wordpress 分类筛选中山百度seo排名公司
  • 网站浮动窗口如何做护肤品推广软文
  • 网站地图是什么样子的做百度关键词排名的公司
  • 网站开发一定要用框架嘛seo北京公司
  • 四川seoseo免费课程
  • 深圳H5网站开发腾讯搜索引擎入口
  • 网站设计批发360官方网站网址
  • php动态网站开发答案江门seo
  • 网站制作结构宁波seo营销
  • 超市营销型网站建设策划书自己做网络推广怎么做
  • vs2017做的网站如何发布seo是什么岗位
  • 网站建设 启象科技公众号运营收费价格表
  • 数码科技网站成品网站货源1
  • 优化是企业通过网站来做吗小红书信息流广告投放
  • ppt制作平台关键词优化多少钱
  • p2p网站如何做测试西安百度推广开户运营