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

免费网站建设的网上设计平台有哪些

免费网站建设的,网上设计平台有哪些,深圳网站建设黄浦网络-技术差,中国网站模板免费下载1.相同点 Resource和Autowired这两个注解的作用都是在Spring生态里面去实现Bean的依赖注入 2.不同点 2.1 Autowired 首先,Autowired是Spring里面提供的一个注解,默认是根据类型来实现Bean的依赖注入。 Autowired注解里面有一个required属性默认值是t…

1.相同点

@Resource和@Autowired这两个注解的作用都是在Spring生态里面去实现Bean的依赖注入

2.不同点

2.1 @Autowired

首先,@Autowired是Spring里面提供的一个注解,默认是根据类型来实现Bean的依赖注入。
@Autowired注解里面有一个required属性默认值是true,表示强制要求bean实例的注入,在应用启动的时候,如果IOC容器里面不存在对应类型的Bean,就会报错。当然,如果不希望自动注入,可以把这个属性设置成false。

@Controller
public class HelloController {@Autowired(required = true)private HelloService helloService;@ResponseBody@RequestMapping("/hello")public String hello() {return helloService.hello();}
}

但是当项目有多个相同类型的Bean被定义时,使用@Autowired会报错

@org.springframework.context.annotation.Configuration
public class Configuration {@Bean("hello1")public HelloService hello1() {return new HelloServiceImpl();}@Bean("hello2")public HelloService hello2() {return new HelloServiceImpl();}
}

所以Spring启动的时候,会提示一个错误,大概意思原本只能注入一个单实例Bean,但是在IOC容器里面却发现有多个,导致注入失败。

Field helloService in com.zte.helloworld.contorller.HelloController required a single bean, but 3 were found:- helloServiceImpl: defined in file [D:\JavaWorkSpace\code\helloworld\target\classes\com\zte\helloworld\service\impl\HelloServiceImpl.class]- hello1: defined by method 'hello1' in class path resource [com/zte/helloworld/config/Configuration.class]- hello2: defined by method 'hello2' in class path resource [com/zte/helloworld/config/Configuration.class]Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

针对这个问题,我们可以使用 @Primary或者@Qualifier这两个注解来解决。
@Primary表示主要的bean,当存在多个相同类型的Bean的时候,优先使用声明了@Primary的Bean。
@Qualifier的作用类似于条件筛选,它可以根据Bean的名字找到需要装配的目标Bean。

@Controller
public class HelloController {@Autowired(required = true)@Qualifier("hello1")private HelloService helloService;@ResponseBody@RequestMapping("/hello")public String hello() {return helloService.hello();}
}

2.2 @Resource

@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持。
它的使用方式和@Autowired完全相同,最大的差异于@Resource可以支持ByName和ByType两种注入方式。
如果使用name,Spring就根据bean的名字进行依赖注入,如果使用type,Spring就根据类型实现依赖注入。
如果两个属性都没配置,就先根据定义的属性名字去匹配,如果没匹配成功,再根据类型匹配。两个都没匹配到,就报错。

@Controller
public class HelloController {//    @Autowired(required = true)
//    @Qualifier("hello1")
//    private HelloService helloService;@Resource(name = "helloService01")private HelloService01 helloService01;@Resource(type = HelloService02.class)private HelloService02 helloService02;@ResponseBody@RequestMapping("/hello")public String hello() {return helloService02.hello();}
}

3.总结

  • @Autowired是根据type来匹配,@Resource可以根据name和type来匹配,默认是name匹配。
  • @Autowired是Spring定义的注解,@Resource是JSR 250规范里面定义的注解,而Spring对JSR 250规范提供了支持。
  • @Autowired如果需要支持name匹配,就需要配合@Primary或者@Qualifier来实现。
http://www.yayakq.cn/news/828222/

相关文章:

  • 学会网站开发需要多久网络文化经营许可证有什么用
  • 长沙网站到首页排名带会员中心的WordPress主题
  • 网站首页浮动窗口代码各地微信推广平台大全
  • 网站建设硬件预算企业 手机网站
  • seo优化网站源码快速网站推广优化
  • 石家庄建设工程招标办网站中国商业联盟官网
  • 什么样的网站利于seo网站内部优化方法
  • 做母婴网站上海建站模板系统
  • 杭州新网站建设方案郴州网站建设设计制作
  • 北京网站优化快速排名建设工程合同管理多少分及格
  • 旅游电子商务网站开发项目进度表拼团小程序代理
  • 建设小型网站wordpress主题 添加自定义菜单
  • 网站集约建设后网站域名规范什么网站免费做游戏
  • 无锡哪个网站建设比较好怎么知道网站哪家公司做的
  • 九江做网站的公司移除wordpress上边栏
  • 校园网站设计代码怎么查自己是不是备案人员
  • 视频网站的链接怎么做毕业ppt模板免费下载
  • 上海 网站建设公司济南百度推广电话
  • 新手建设什么网站好西安哪家网站建设公司好
  • 廉政网站 建设需求上海app开发网站建设
  • 全国做暧小视频网站小程序开发外包服务公司
  • 有服务器还需要买网站空间吗网络公司排名及分析
  • 游戏开发和网站开发十年经验网站开发企业
  • 成都市城乡建设管理局网站php进销存管理系统
  • 网站首页ui月编程做网站
  • 王建设医生网站与众不同的网站
  • 自己做网站推广建设集团网站方案设计
  • wordpress安装与使用免费网站自动优化软件
  • 天津 做网站建网站程序下载
  • 网站内容维护秦皇岛北戴河