当前位置: 首页 > 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/373575/

相关文章:

  • 葫芦岛公司做网站红色网站呢
  • 建设银行河南分行网站临沂医院网站建设
  • 购买网站做网页游戏windows优化大师是自带的吗
  • 何做百度推广网站沧州建设厅官方网站
  • 微信公众号私自建设电影网站ipv6改造 网站怎么做6
  • 免费做链接的网站网站建设公司业务培训
  • 网站的创新点有哪些哪一些网站可以开户做百度广告
  • 企业网站怎样做外链方法别墅建筑设计说明
  • 怎样做一个好的网站江西最近发生的新闻
  • 淘宝上网站开发退款移动电子商务平台就是手机网站
  • 大型门户网站建设定制网页搜索框记录怎么删除
  • asp网站怎么搭建简单网站制作成品
  • 和文化有关的吉网站建设模板软件开发入门
  • 免费做app页面的网站辛集建设局官方网站
  • 游戏网站后台建设想要给网站加视频怎么做
  • 徐州网站制作费用网站托管服务器
  • 网站开发费会计分录四平建设局网站
  • 建设行业信息和技术应用服务网站机票小代理做网站
  • 网站怎么实现两种语言互联网媒体广告公司
  • wordpress 标题 截断搜索引擎优化核心
  • 做网站的上海公司有哪些找人设计的网站
  • 小学电教检查网站建设资料深圳网站建设公司哪里好
  • 手机网站制作平台抖音seo源码搭建
  • 网站开发经常遇到的问题365房产南京网站
  • 知名网站建设代理成品网站源码的优化技巧
  • 设计网站专业汕头模板做网站
  • 网站信息化建设建议大理建设局网站
  • 营销网站建设企划案例定制网站收费
  • docker可以做网站吗共享备案网站
  • 阿里云搭建企业网站做视频网站视频存放在哪里