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. [SPOJ7258]Lexicographical Substring Search

    [SPOJ7258]Lexicographical Substring Search 试题描述 Little Daniel loves to play with strings! He always ...

  2. HDU 5876 补图 单源 最短路

    ---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  3. [bzoj3527][Zjoi2014]力_FFT

    力 bzoj-3527 Zjoi-2014 题目大意:给定长度为$n$的$q$序列,定义$F_i=\sum\limits_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum\lim ...

  4. Ubuntu 16.04出现:qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory

    没有安装qt4-qmake,安装即可: sudo apt-get install qt4-qmake 参考: https://stackoverflow.com/questions/23703864/ ...

  5. Ubuntu 16.04安装WebStorm

    前提:必须正确安装JDK. 下载: http://confluence.jetbrains.com/display/WI/WebStorm+EAP 或者下载历史版本:https://www.jetbr ...

  6. php-7.1编译记录

    编译php-7.1.28步骤 检查环境 ./configure \ --prefix=/u01/server/php-7.1.28 \ --enable-fpm \ --with-fpm-user=d ...

  7. WebLogic(12C)——简单公布和JDBC

    一,简单公布应用 1.点击"安装".開始部署应用: 2,找到要部署的项目路径: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFue ...

  8. 【转】C++ 进程间的通讯(一):简单的有名管道实现

    原文: C++ 进程间的通讯(一):简单的有名管道实现 -------------------------------------------------- 进程间的通讯(一):简单的有名管道实现   ...

  9. ubuntu下vim及man帮助文档的汉化

    vim是一个功能超级强大的编辑器,当然我们也可将它配置超强的IDE.这类教程网上非常多,我就不再此赘述了. 我们在使用中对不熟悉的命令,不熟悉的插件的使用方法常常须要查看文档,全英文环境确实看着人头都 ...

  10. react组件是怎么来的

    组件的创造方法为React.createClass() ——创造一个类,react系统内部设计了一套类系统,利用它来创造react组件.但这并不是必须的,我们还可以用es6的class类来创造组件,这 ...