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 ...
随机推荐
- 【莫比乌斯反演+树状数组+分块求和】GCD Array
https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/I [题意] 给定长度为l的一个数组,初始值为0:规定了两种操作: [思路] 找到 ...
- 3D模型
题目描述 一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成.在每个网格上都可以有一个建筑,建筑由若干个1×1×1的立方体搭建而成(也就是所有建筑的底部都在同一平面上的).几个典型的城市模 ...
- NOIP2013提高组D2T3 华容道
n<=30 * m<=30 的地图上,0表示墙壁,1表示可以放箱子的空地.q<=500次询问,每次问:当空地上唯一没有放箱子的空格子在(ex,ey)时,把位于(sx,sy)的箱子移动 ...
- Codeforces Round #489 (Div. 2) B、C
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- CodeForces - 812C Sagheer and Nubian Market 二分
On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...
- pipenv 的使用
pipenv 的使用 学习了:https://blog.csdn.net/chroming/article/details/77104873?locationNum=4&fps=1 https ...
- C#.NET 如何快速输入一个对象事件对应的方法
直接在Textbox图像对象中找到这个对象的KeyPress方法,然后输入触发的事件名称.效率更高,不容易出错. "void TypeAreaKeyPress(object sender, ...
- [Java] 实验5參考代码
实验4月3日晚截止,实验截止后将在此给出完整的參考代码. 1. 怎样使用以下的代码模板: 1.1 在eclipse中创建相应名称的类 1.2 将代码拷贝到类文件中 1.3 在//todo凝视中 ...
- HDMI接口基础知识及硬件设计
参考资料:http://blog.csdn.net/u013625961/article/details/53434189: http://blog.csdn.net/u014276460/artic ...
- 玩转单元測试之WireMock -- Web服务模拟器
WireMock 是一个灵活的库用于 Web 服务測试,和其它測试工具不同的是.WireMock 创建一个实际的 HTTPserver来执行你的 Web 服务以方便測试. 它支持 HTTP 响应存根. ...