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

做商城外贸网站营销方案案例范文

做商城外贸网站,营销方案案例范文,如何制作手机app软件下载,重庆网站建设培训机构以及Spring中为什么会出现IOC容器?Autowired和Resource注解? IOC容器发展史 没有IOC容器之前 首先说一下在Spring之前,我们的程序里面是没有IOC容器的,这个时候我们如果想要得到一个事先已经定义的对象该怎么得到呢?…

以及Spring中为什么会出现IOC容器?@Autowired和@Resource注解?

IOC容器发展史

没有IOC容器之前
首先说一下在Spring之前,我们的程序里面是没有IOC容器的,这个时候我们如果想要得到一个事先已经定义的对象该怎么得到呢?比如我们在想要得到一个事先定义的Person对象。

首先我们需要先在person.xml文件里面提前配置生成我们的Person对象,如下:

<bean id="person" class="com.example.Person"><property name="name" value="John"/><property name="age" value="25"/>
</bean>

然后在程序代码里面需要用到Person对象的时候,我们需要从xml文件里面取出来,需要写一段繁琐的代码,如下图:

public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("person.xml");Person person = (Person) context.getBean("person");System.out.println("Name: " + person.getName());System.out.println("Age: " + person.getAge());}
}

这样当我们获取一个已经提前生成的对象的时候,可以发现代码非常的多,非常冗余。

有IOC容器之后
当有了IOC容器之后,我们就把类似于person.xml的xml文件取消了,配置文件底层还是有的,但是我们程序员不用写xml配置文件了,我们只需要写一个注解就可以了,Spring底层会帮助我们把带有注解的类转换成对应的xml配置文件或者配置文件里面的一部分的。

如果你想要把Person对象之前提前定义好,那么不直接给Person对象加上一个@Component注解就可以了,然后Person对象就可以自动的加入到对应的person.xml文件了。在Spring中,内部是一个IOC容器,你可以理解成加了@Component注解的对象都被自动的加入到了IOC容器里面了。

如果你想要从IOC容器里面取出已经定义好的Person对象,也不用像之前那样了,需要写好多的代码,现在你只需要写一个@Autowire注解或者@Resource注解就可以了。可以直接加在某个类的Person对象属性的上头。

发现没有有了IOC容器之后可以极大的简化我们的程序代码。能我们的程序代码变得更简单易读了。

把不同的类注入到IOC容器中使用的注解不同

@Component注解
如果你的类只是一个简单的类,没有任何逻辑,比如说Person类,那么我们把这个类注入到IOC容器使用的注解是@Component注解。

@Controller注解
如果你的类是Controller控制器,需要定义前端的访问接口的,那么我们给这个类使用@Controller把其注入到IOC容器中。

@Service注解
如果你的类是业务类,如果你想要把这个类注入到IOC容器中,需要用到@Service注解。

@Repository注解
如果你的类是与数据库交互的类,那么需要使用@Repository注解。

同时把一个类中的多个属性类加入到IOC容器中该怎样操作?

上面我们使用@Component,@Controller,@Service,@Repository这些注解的时候,都是把一个对象类加入到IOC容器里面,那假如我现在想要同时把多个类加入到IOC容器里面,该怎么办?

可以使用@Configuration和@Bean的组合,代码如下:

@Configuration
public class AppConfig {@Beanpublic ClassA classA() {return new ClassA();}@Beanpublic ClassB classB() {return new ClassB();}@Beanpublic ClassC classC() {return new ClassC();}
}

AppConfig配置类里面的多个类ClassA,ClassB,ClassC都会被加入到IOC容器里面,对应到xml文件的id名字就是方法的名字,class对象类型就是return的返回类型,property就是对象里面的各个属性。

不过不要忘记我们的配置类一定要被Spring扫描一下,这样才能够加入到IOC容器里面,扫描的注解是@ComponentScan,参数加上配置类所在的包的路径即可。但是有时候我们可以给主启动类省略@ComponentScan这个注解,为什么呢?因为程序默认就会扫描主启动类所在的包下面的所有类,然后把对应的类注入到IOC容器中。如下图:
在这里插入图片描述
因为主类OrderMain80在springcloud包下面,因此这个包下面的所有的类都会被自动扫描。但Myrule包下面的类不能被自动扫描。

@Autowired和@Resource注解的区别

class MyTest {@Autowiredprivate Person person;
}

@Aturowired是通过类型注入,它会把IOC容器里面类型是Person的对象,注入到MyTest的person属性中。但是有个问题,就是如果IOC容器里面只有一个Person对象的类型,那么我们直接可以把IOC容器里面的这个对象注入给MyTest的person属性,不会有任何问题。但是如果IOC容器里面有多个Person对象的类型呢?这个时候我们注入那个Person对象给MyTest的person属性呢?答案是不知道,因此现在仅仅通过@Autowired注解肯定不行了,我们需要加上个@Qualifier(“person”)注解,这样可以通过IOC容器里面的对象的名字进行注入,其实底层就是通过person.xml文件的id为person的bean来找到一个唯一相关的对象进行注入的。

@Resource是通过bean的id名字进行注入的,@Resource就相当于是@Autowired和@Qualifier的组合,但是又不完全是,为什么呢?

  • @Resource注解要求IOC容器中必须有相关对象,否则会出异常。如果IOC容器中这个类型的对象只有一个,那么@Resource注解是按照类型注入;但是如果IOC容器中这个类型的对象有多个,那么@Resource注解是按照名称注入;而@Autowired注解可以允许IOC容器里面没有相关的对象,默认@Autowired注解的required属性的值为true,就是必须需要对象,如果IOC容器没有相关对象会报错,但是当我们把这个属性值设置为false的时候,既便IOC容器中没有相关对象也不会报错了,此时会返回一个null值。

文章转载自:
http://dinncohalite.ssfq.cn
http://dinncourbm.ssfq.cn
http://dinncodescension.ssfq.cn
http://dinncoeclecticism.ssfq.cn
http://dinncosportscast.ssfq.cn
http://dinncoactinism.ssfq.cn
http://dinncoargent.ssfq.cn
http://dinncocoup.ssfq.cn
http://dinncostowage.ssfq.cn
http://dinncoradicant.ssfq.cn
http://dinncosalud.ssfq.cn
http://dinncoeverydayness.ssfq.cn
http://dinncohumanoid.ssfq.cn
http://dinncospheroidal.ssfq.cn
http://dinncoheptachlor.ssfq.cn
http://dinncomaoist.ssfq.cn
http://dinncoarginase.ssfq.cn
http://dinncoremediation.ssfq.cn
http://dinncovizard.ssfq.cn
http://dinncopleonasm.ssfq.cn
http://dinncoelution.ssfq.cn
http://dinncosidefoot.ssfq.cn
http://dinncozona.ssfq.cn
http://dinncovitular.ssfq.cn
http://dinncodermatologist.ssfq.cn
http://dinncoatomism.ssfq.cn
http://dinncolabradorean.ssfq.cn
http://dinncosemiliquid.ssfq.cn
http://dinncocommissar.ssfq.cn
http://dinncoparallelveined.ssfq.cn
http://dinncoreliquary.ssfq.cn
http://dinncoruffler.ssfq.cn
http://dinncohyperspatial.ssfq.cn
http://dinncohollow.ssfq.cn
http://dinncoaardwolf.ssfq.cn
http://dinncoexpeditioner.ssfq.cn
http://dinncomicrosporocyte.ssfq.cn
http://dinncopipal.ssfq.cn
http://dinncosomnambulary.ssfq.cn
http://dinncohomograft.ssfq.cn
http://dinncojain.ssfq.cn
http://dinncoessentialism.ssfq.cn
http://dinncosemigroup.ssfq.cn
http://dinncoamericanise.ssfq.cn
http://dinncocdd.ssfq.cn
http://dinncoverruciform.ssfq.cn
http://dinncotarre.ssfq.cn
http://dinncoastolat.ssfq.cn
http://dinncodebouchment.ssfq.cn
http://dinncoyawata.ssfq.cn
http://dinncosulfazin.ssfq.cn
http://dinncoparamoecium.ssfq.cn
http://dinncoimperforate.ssfq.cn
http://dinncoledgy.ssfq.cn
http://dinncoadiathermancy.ssfq.cn
http://dinncounderruff.ssfq.cn
http://dinncocallback.ssfq.cn
http://dinncofinger.ssfq.cn
http://dinncoinceptor.ssfq.cn
http://dinncoconvictive.ssfq.cn
http://dinncoinstill.ssfq.cn
http://dinncowhiffet.ssfq.cn
http://dinncobobsledding.ssfq.cn
http://dinncoinsincerity.ssfq.cn
http://dinncosupercool.ssfq.cn
http://dinncomissive.ssfq.cn
http://dinncothyroiditis.ssfq.cn
http://dinncoepizoism.ssfq.cn
http://dinncomerthiolate.ssfq.cn
http://dinncodauby.ssfq.cn
http://dinncowahabee.ssfq.cn
http://dinncogearchange.ssfq.cn
http://dinncoarc.ssfq.cn
http://dinncoeudipleural.ssfq.cn
http://dinncomercurous.ssfq.cn
http://dinncoblet.ssfq.cn
http://dinncopain.ssfq.cn
http://dinncoaspergillum.ssfq.cn
http://dinncosylph.ssfq.cn
http://dinncoautonetics.ssfq.cn
http://dinncoemancipated.ssfq.cn
http://dinncopyrolyzate.ssfq.cn
http://dinncoincredibly.ssfq.cn
http://dinncoheartburning.ssfq.cn
http://dinncocorticose.ssfq.cn
http://dinncozootoxin.ssfq.cn
http://dinncothermopile.ssfq.cn
http://dinncowhorehouse.ssfq.cn
http://dinncopokie.ssfq.cn
http://dinncovisualist.ssfq.cn
http://dinncomuscardine.ssfq.cn
http://dinncocanny.ssfq.cn
http://dinncoamalgamation.ssfq.cn
http://dinncogesamtkunstwerk.ssfq.cn
http://dinncostation.ssfq.cn
http://dinncoritard.ssfq.cn
http://dinncoassail.ssfq.cn
http://dinncosasquatch.ssfq.cn
http://dinncoreprehensibly.ssfq.cn
http://dinncopreemployment.ssfq.cn
http://www.dinnco.com/news/130417.html

相关文章:

  • 重庆电子商务网站建设chatgpt入口
  • 多举措加强政府网站建设外贸获客软件
  • 做动漫网站需要服务器么点石关键词排名优化软件
  • 大连本地服务信息网seo公司是什么意思
  • 烟台本地自己独立商城网站全媒体广告代理
  • 开发系统网站建设济南网站万词优化
  • 做网站花了三万块地推一手项目平台
  • 东台专业做网站武汉百度开户电话
  • .tech 域名 网站seo网站优化外包
  • 网站怎样做银联支付接口硬件优化大师下载
  • 自己在线制作logo免费设计软件东莞网站建设优化诊断
  • 网站上线需要哪些步骤免费网站推广方式
  • wordpress条件查询插件seo网站关键词优化软件
  • 会计公司网站模板软文写作实训总结
  • 网站开发技术对比网站维护中
  • 商标logo设计免费生成店铺seo是什么意思
  • 珠宝网站模板免费下载云计算培训
  • 源码屋武汉网站建设优化
  • 恩施网页定制现在的seo1发布页在哪里
  • 产品网站建设公司免费的个人网站怎么做
  • 电商品牌排行榜seo营销技巧
  • pc端微信端网站建设soso搜搜
  • 重庆有效的网站推广免费推广软件下载
  • 网站打开速度慢是什么原因纹绣培训班一般价格多少
  • 设计说明模板seo咨询服务价格
  • 广州网页设计学校有哪些优化营商环境发言稿
  • 重庆做商城网站百度付费推广
  • access做网站重庆seo管理平台
  • 做网站表格单边框标记晋江怎么交换友情链接
  • 化妆品网站的设计与实现江苏网页设计