spring的annotation
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的更多相关文章
- Spring的annotation用在set方法上 hibernate的annotation用get方法上
1.Spring的annotation用在set方法上 2.hibernate的annotation用在get方法上
- Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别
Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...
- spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
- Spring Enable annotation – writing a custom Enable annotation
原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...
- [Spring MVC] - Annotation验证
使用Spring MVC的Annotation验证可以直接对view model的简单数据验证,注意,这里是简单的,如果model的数据验证需要有一些比较复杂的业务逻辑性在里头,只是使用annotat ...
- [Spring] IOC - Annotation
Spring Annotation使用例子. 与XML配置的例子一样:http://www.cnblogs.com/HD/p/3962541.html Project结构: 配置文件:springCo ...
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
<context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config& ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...
- 实现运行在独立线程池的调度功能,基于Spring和Annotation
使用Spring的注解(@Scheduled)声明多个调度的时候,由于其默认实现机制,将导致多个调度方法之间相互干扰(简单理解就是调度不按配置的时间点执行). 为了解决该问题尝试了修改线程池大小,但是 ...
- (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别
转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...
随机推荐
- 582. Kill Process
Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...
- SpringBoot入门系列~Spring-Data-JPA自动建表
1.pom.xml引入Spring-Data-Jpa和mysql依赖 <!-- Spring-data-jpa依赖 --> <dependency> <groupId&g ...
- js数组转换成json串 (JSON.stringify)
例如: var giftlist[1490011777] = []; giftlist[1490011777]['id'] = 1490011777; giftlist[1490011777]['na ...
- CALayer之 customizing timing of an animation
customizing timing of an animation Timing is an important part of animations, and with Core Animatio ...
- webstrom设置代码缩进四个空格
言,今天在github上拉了一份代码,发现代码缩进是2个空格,不习惯.找到了解决方案. 一.File ---> Settings 搜索Code Style,取消勾选使用现文件的缩进方式,见下图. ...
- Setting up Storm and Running Your First Topology
http://www.haroldnguyen.com/blog/2015/01/setting-up-storm-and-running-your-first-topology/ --------- ...
- SaltStack学习系列之自定义grains
Master端 打开存放自定义grains的目录 vim /etc/salt/master file_roots: base: - /srv/salt/ 建立自定义模块 cd /srv/salt mk ...
- 附录A 思科互联网络操作系统(IOS)
思科互联网络操作系统(IOS) 要点 实现IP编址方案和IP服务,以满足中型企业分支机构网络的网络需求 口在路由器上配置和验证 DHCP和DNS 以及排除其故障(包括 CLI/SDM ). 口配置和验 ...
- redis 事务 及发布于订阅功能
事务: Redis事务可以一次执行多个命令,事务具有以下特征: 1.隔离操作:事务中的所有命令都会序列化.按顺序地执行,不会被其他命令打扰. 2.原子操作:事务中的命令要么全部被执行,要么全部都不执行 ...
- #pragma pack (n) 惹的祸
今天遇到了一个问题,使用数据流传输的数据在解析的时候数据错位.想了非常久,发现是#pragma pack (n)惹的祸. 首先.解析方使用了编译字节设置,可是在发送方没有使用,于是用相同的结构体解析数 ...