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 ...
随机推荐
- 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...
- hdu 3691最小割将一个图分成两部分
转载地址:http://blog.csdn.net/xdu_truth/article/details/8104721 题意:题给出一个无向图和一个源点,让你求从这个点出发到某个点最大流的最小值.由最 ...
- poj 3667 Hotel (线段树的合并操作)
Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- 姓名与ID(codevs 1027 未结题)
题目描述 Description 有N个人,各自有一个姓名和ID(别名).每个人的姓名和ID都没有重复.这些人依次进入一间房间,然后可能会离开.过程中可以得到一些信息,告知在房间里的某个人的ID.你的 ...
- baidu 和 es 使用
http://www.cnblogs.com/kangoroo/p/8047586.html
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- C#高级编程四十八天----列表
C#中的List C#中deList怎么样?List<T>类是ArrayList类的泛型等效类,该类使用大小可按需动态增长的数组实现List<T>泛型接口. 泛型的优点:它为使 ...
- cocos2d-x 3.6版连连看版本号控制
为了以后的开发和管理.源代码开发必须要使用版本号控制.我们当然选择git来做版本号控制了. 假设你在终端输入git,提示不是一个命令的话.那就说明你的机器没有安装git工具.那就安装一个,百度之有非常 ...
- 通讯编程入门--WEBSOCKET
C#通讯编程入门--WEBSOCKET WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System ...
- iOS开发——高级篇——iOS 项目的目录结构
最近闲来无事去面试一下iOS开发,让我感到吃惊的,面试官竟然问怎么分目录结构,还具体问每个子目录的文件名. 目录结构确实非常重要,面试官这么问,无疑是想窥探开发经验.清晰的目录结构,可让人一眼明白相应 ...