spring容器创建bean对象的方式:

1,使用反射调用无参构造器来创建实例(前提是这个类有无参构造器)(常规方式)

2,通过工厂类获得实例(工厂类实现了接口FactoryBean<?>)

3,通过实例工厂获得bean对象(不需要实现或者继承任何接口或者父类)

4,通过静态工厂获得实例

ioc中的annotation配置

1,@Autowired
    1) @Autowired使用后需要在xml文件加入以下配置才能生效: <context:annotation-config/>

 2)@Autowired默认使用byType来装配属性,如果匹配到类型的多个实例,再通过byName来确定Bean。

2,@Resource

  1)@Resource的作用和@Autowired差不多,只不过 @Resource是默认先用byName,如果找不到合适的就再用byType来注入

  2)在xml文件加入以下配置才能生效: <context:annotation-config/>

byName就是通过Bean标签中的id或者name属性值进行匹配注入(匹配的是setXxx中去掉set后的名字),byType就是按Bean标签中的Class的类型进行匹配注入。(根据当前类中的set方法里面参数的类型,
            去容器中找相匹配的对象)

eg:

注意:自动装配只对[对象类型]起作用,对基本类型不起作用.

@Autowired
private Car car;
  <!-- autowire注解 默认bytype,再byname -->
<bean name="car" class="com.briup.ioc.annotation.Car">
<property name="price" value="33333"></property>
<property name="name" value="test"></property>
</bean>
<bean name="car123456" class="com.briup.ioc.annotation.Car">
<property name="price" value="122222"></property>
<property name="name" value="baoma"></property>
</bean>
结果为:
33333.0
test

3,@controller 控制器(注入服务)
用于标注控制层,相当于struts中的action层

4、@service 服务(注入dao)
用于标注服务层,主要用来进行业务的逻辑处理

5、@repository(实现dao访问)
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件.

6、@component (把普通pojo实例化到spring容器中,相当于配置文件中的
<bean id="" class=""/>)
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

@Component是Spring中所有bean组件的通用形式, @Repository @Service @Controller 则是 @Component的细化(它们区别很小,但最好归类使用)

@Component有一个可选的参数,用于指定bean的名称
        @Component("boss")

若不指定,默认为当前类的类名小写

@Component注解可以直接定义bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义

@Component注解直接写在类上面即可

注意:
    1.component-scan标签默认情况下自动扫描指定路径下的包(含所有子包)

2.component-scan标签将带有@Component @Repository @Service @Controller注解的类自动注册到spring容器中

3.component-scan标签对标记了@Required @Autowired @PostConstruct @PreDestroy
@Resource @WebServiceRef @EJB   @PersistenceContext
@PersistenceUnit等注解的类进行对应的操作,使注解生效
    
    4.component-scan标签包含了annotation-config标签的作用

spring的annotation的更多相关文章

  1. Spring的annotation用在set方法上 hibernate的annotation用get方法上

    1.Spring的annotation用在set方法上 2.hibernate的annotation用在get方法上

  2. Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别

    Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...

  3. spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on

    spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on

  4. Spring Enable annotation – writing a custom Enable annotation

    原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...

  5. [Spring MVC] - Annotation验证

    使用Spring MVC的Annotation验证可以直接对view model的简单数据验证,注意,这里是简单的,如果model的数据验证需要有一些比较复杂的业务逻辑性在里头,只是使用annotat ...

  6. [Spring] IOC - Annotation

    Spring Annotation使用例子. 与XML配置的例子一样:http://www.cnblogs.com/HD/p/3962541.html Project结构: 配置文件:springCo ...

  7. Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别

    <context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config& ...

  8. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  9. 实现运行在独立线程池的调度功能,基于Spring和Annotation

    使用Spring的注解(@Scheduled)声明多个调度的时候,由于其默认实现机制,将导致多个调度方法之间相互干扰(简单理解就是调度不按配置的时间点执行). 为了解决该问题尝试了修改线程池大小,但是 ...

  10. (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别

    转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...

随机推荐

  1. 593. Valid Square

    Problem statement: Given the coordinates of four points in 2D space, return whether the four points ...

  2. ELK pipeline

    https://www.felayman.com/articles/2017/11/24/1511527532643.html?utm_medium=hao.caibaojian.com&ut ...

  3. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  4. JSP的异常处理

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/exception-handling.html: 当写JSP代码的时候,有可能会留下一个编码错误,并且它会 ...

  5. 关于warning

    IT男正吸着雪茄,吐着烟圈.他女朋友生气了发飙道:“你没看见包装盒上的警告(Warning)么?吸烟有害健康!” IT男淡定地回答道:“我是程序员.我们不关心警告,只关心错误.”

  6. centos7grub2 引导win10

    centos7+win10安装完成之后,使用gurb2引导win10系统 方式:使用ntfs-3g 步骤: 1.加源  wget -O /etc/yum.repos.d/epel.repo http: ...

  7. FFmpeg的HEVC解码器源码简单分析:概述

    ===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...

  8. 秒懂C#通过Emit动态生成代码 C#使用Emit构造拦截器动态代理类

    秒懂C#通过Emit动态生成代码   首先需要声明一个程序集名称, 1 // specify a new assembly name 2 var assemblyName = new Assembly ...

  9. Ubuntu18.04系统中vi键盘输入字符不匹配

    起因 今天重装了我的雷神笔记本的ubuntu18.04,不要问我为什么,我就是想复习下重装系统而已.好吧,我承认我改错文件启动不起来了. 于是我要重装jdk.maven and so on,但是当我用 ...

  10. Android 网络学习之获取server文本文件

    上次我们学习怎样从网络上获取一张图片,今天我们学习怎样从网络上获取文本文件.以XML文件为样例. 由于XML文件在实际开发中最为常见. 我们以以下图片为样例学习怎样从网络上获取XML文件 我们的xml ...