spring—使用注解配置Bean
2016-09-07 15:37
302 查看
从Spring2.5开始,出现了注解装配JavaBean的新方式。注解可以减少代码的开发量,spring提供了丰富的注解功能,现在项目中注解的方式使用的也越来越多了。
** 开启注解扫描
Spring容器默认是禁用注解配置的。打开注解扫描的方式主要有两种: <context:component-scan>组件扫描和<context:annotation-config>注解配置。
一般选择第一种,因为第一种的功能比第二种强大的多。<context:annotation-config>用于激活那些已经在Spring容器中注册过的Bean,而<context:component-scan>还可以在指定的package下扫描注册Bean的功能。
具体区别:http://www.cnblogs.com/leiOOlei/p/3713989.html
# <context:annotation-config>使用
先加入一个player类和team类,需要在Player中的Team属性加上@autowire注解,因为Player类依赖于team类,当然也可以把@autowire属性注解放置在任何setter函数上
在spring的配置文件中配置
结果:Player [name=wxh, age=22, team=Team[name=boston]] ==> success
# 使用更加强大的<context:component-scan>
开启注解扫描
在Player类和Team类中只需要在类的上面加上@Component注解就可以了。因为<context:component-scan>有注册bean的功能,@Component相当于在springIOC容器中注册了这个类。
Tips:
>>当springIOC容器中存在两个或者以上Team类的Bean是,自动装配会失败,因为默认的@autowired是byType的,这时候我们需要@qualifer(“value”)指定选择哪个bean,这时自动装配就会很据byName装配。[qualifier:预选的]
>>@component
中有value属性可以指定Bean的Id,其实在@service、@Controller
、@repository中都有
** 特殊的注解标注
>> @Component: 通用的构造型注解,标识该类为spring组件[不推荐使用]
>> @Controller: 标识将该类定义为SpringMVC的控制器
==> Controller层
>> @Service: 标识将该类定义为业务服务==> Service层
>> @Repository: 标识将该类定义为数据仓库==> Dao层
** 开启注解扫描
Spring容器默认是禁用注解配置的。打开注解扫描的方式主要有两种: <context:component-scan>组件扫描和<context:annotation-config>注解配置。
一般选择第一种,因为第一种的功能比第二种强大的多。<context:annotation-config>用于激活那些已经在Spring容器中注册过的Bean,而<context:component-scan>还可以在指定的package下扫描注册Bean的功能。
具体区别:http://www.cnblogs.com/leiOOlei/p/3713989.html
# <context:annotation-config>使用
先加入一个player类和team类,需要在Player中的Team属性加上@autowire注解,因为Player类依赖于team类,当然也可以把@autowire属性注解放置在任何setter函数上
publicclass Player { private String name; privateintage; @Autowired private Team team; public Player() {} public Player(String name, intage, Team team) { super(); this.name = name; this.age = age; this.team = team; } public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } publicint getAge() { returnage; } publicvoid setAge(intage) { this.age = age; } public Team getTeam() { returnteam; } publicvoid setTeam(Team team) { this.team = team; } @Override public String toString() { return"Player [name=" + name + ", age=" + age + ", team=" + team + "]"; } } publicclass Team { private String name; public Team() {} public Team(String name) { this.name = name; } public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } @Override public String toString() { return"Team [name=" + name + "]"; } } |
<!-- 开启注解配置 --> <context:annotation-config></context:annotation-config> <bean id="player" class="annotation.com.wxh.blog.Player" p:name="wxh" p:age="22"></bean> <bean id="team" class="annotation.com.wxh.blog.Team" p:name="boston"></bean> |
# 使用更加强大的<context:component-scan>
开启注解扫描
<!-- 对于扫描到的组件,Spring会有默认的命名策略:第一个字母小写 在组件上使用注解后需要在Spring的文件中声明<context:component-scan/> base-package属性指定一个需要扫描的基类包,Spring会扫描这个包及其子包的所有类 当需要扫描多个包的时候用“,”分开 --> <context:component-scan base-package="annotation.com.wxh.blog"></context:component-scan> |
Tips:
>>当springIOC容器中存在两个或者以上Team类的Bean是,自动装配会失败,因为默认的@autowired是byType的,这时候我们需要@qualifer(“value”)指定选择哪个bean,这时自动装配就会很据byName装配。[qualifier:预选的]
>>@component
中有value属性可以指定Bean的Id,其实在@service、@Controller
、@repository中都有
** 特殊的注解标注
>> @Component: 通用的构造型注解,标识该类为spring组件[不推荐使用]
>> @Controller: 标识将该类定义为SpringMVC的控制器
==> Controller层
>> @Service: 标识将该类定义为业务服务==> Service层
>> @Repository: 标识将该类定义为数据仓库==> Dao层
相关文章推荐
- Spring配置使用注解注入bean
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 05_Spring的bean管理_注解和配置文件混合使用
- Java - Spring中如何使用注解来配置Bean?有哪些相关的注解?
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Springboot 中同时使用mybatis注解和springbean-xml配置方式
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- spring学习笔记(12)——使用注解方式配置bean
- 使用Spring 2.5注解自动注册Bean的配置
- 04-Spring-使用ioc注解方式配置bean
- 简化Spring的XML配置(二)——使用注解装配bean
- [注解的力量]Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Spring学习(九)使用ioc注解方式配置bean
- Spring使用@Autowired,@Qualifier,@Resource注解配置bean
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Spring中bean管理-配置文件和注解方式混合使用
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
- Spring使用注解的方式配置bean的属性-bean之间的引用关系
- Spring4----使用注解方式配置bean和bean的属性