Spring--通过注解来配置bean
Spring通过注解配置bean
	  基于注解配置bean
	  基于注解来配置bean的属性
在classpath中扫描组件
	  组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
	  特定的组件包括:
		    -@Component:基本注解,标识了一个受Spring管理的组件
		    -@Responsitory:标识持久层组件
		    -@Service:标识服务层(业务层)组件
		    -@Controller:标识表现层组件
	  对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写。也可以在注解中通过value属性值标识组件的名称。
	  当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>:
		    base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类
		    当需要扫描多个包时,可以使用逗号分隔
		    如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,示例:
    <context:component-sacn base-package="com.yl.spring.beans" resource-pattern="autowire/*.class"/>
    <context:include-filter>子节点表示要包含的目标类
    <context:exclude-filter>子节点表示要排除在外的目标类
    <context:component-sacn>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点
<context:include-filter>和<context:exclude-filter>子节点支持多种类型的过滤表达式:
| 类别 | 示例 | 说明 | 
| annotation | com.yl.XxxAnnotation | 所有标注了XxxAnnotation的类,该类型采用目标类是否标注了某个注解进行过滤 | 
| assinable | com.yl.XxxService | 所有继承或扩展XxxService的类,该类型采用了目标类是否继承或扩展某个特定类进行过滤 | 
| aspectj | com.yl.*Service | 所有类名义Service结束的类及继承或扩展它们的类,该类型采用AspectJ表达式进行过滤 | 
| regex | com.yl.anno.* | 所有com.yl.anno包下的类。该类型采用正则表达式,根据类的类名进行过滤 | 
| custom | com.yl.XxxTypeFilter | 采用XxxTypeFilter通过代码的方式定义过滤原则。该类必须实现org.springframework.core.type.TypeFilter接口 | 
组件装配
	<context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired和@Resource、和@Inject注解的属性
使用@Autowired自动装配bean
	  @Autowired注解自动装配具有兼容类型的单个bean属性
	  -构造器,普通字段(即使是非public),一切只有参数的方法都可以应用@Autowired
	  -默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的bean装配属性时,会抛出异常。若某一属性允许不被设置,可以设置@Autowired注解的required属性为false
	  -默认情况下,当IOC容器里存在多个类型兼容的bean时,通过类型的自动装配将无法工作。此时可以在@Qualifiter注解里提供bean的名称,Spring允许对方法的入参标注 @Qualifiter已指定注入bean的名称
	  -@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的bean进行自动匹配
	  -@Autowired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的bean
	  -@Autowired注解用在java.util.Map上时,若该Map的键值作为String,那么Spring将自动装配与之Map值类型兼容的bean,此时bean的名称作为键值
TestObject.java
package com.yl.annotation; import org.springframework.stereotype.Component; @Component
public class TestObject { }
UserRepository.java接口
 package com.yl.annotation.repository;
 public interface UserRepository {
     public void save();
 }
UserRepositoryImpl.java
package com.yl.annotation.repository; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.yl.annotation.TestObject; @Repository
//@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository { @Autowired(required=false)
private TestObject testObject; @Override
public void save() {
System.out.println("UserRepository save...");
System.out.println(testObject);
} }
UserJdbcRepository.java
package com.yl.annotation.repository; import org.springframework.stereotype.Repository; @Repository
public class UserJdbcRepository implements UserRepository { @Override
public void save() {
System.out.println("UserJdbcRepository save...");
} }
UserService.java
package com.yl.annotation.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import com.yl.annotation.repository.UserRepository; @Service
public class UserService {
@Autowired
@Qualifier("userJdbcRepository")
private UserRepository userRepository; /*@Autowired
@Qualifier("userJdbcRepository")
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}*/ public void add() {
System.out.println("UserService add...");
userRepository.save();
}
}
UserController.java
package com.yl.annotation.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import com.yl.annotation.service.UserService; @Controller
public class UserController {
@Autowired
private UserService userService; public void execute() {
System.out.println("UserController execute...");
userService.add();
}
}
beans-annotation.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- 指定Spring IOC容器扫描的包 -->
<!-- 可以通过resource-pattern指定扫描的资源 -->
<!-- <context:component-scan
base-package="com.yl.annotation"
resource-pattern="repository/*.class"></context:component-scan> --> <!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 -->
<!-- context:include-filter 子节点指定包含哪些指定表达式的组件, 该子节点需要use-default-filters配合使用 -->
<context:component-scan
base-package="com.yl.annotation" >
<!-- use-default-filters="false"> -->
<!-- <context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/> --> <!-- <context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/> --> <!-- <context:exclude-filter type="assignable"
expression="com.yl.annotation.repository.UserRepository"/> --> <!-- <context:include-filter type="annotation"
expression="com.yl.annotation.repository.UserRepository"/> -->
</context:component-scan>
</beans>
使用@Resource或@Inject自动装配bean
	  Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解的功用类似
	  @Resource注解要求提供一个bean名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为bean的名称
	  @Inject和@Autowired注解一样也是按类型注入的bean,但是没有required属性
	  建议使用@Autowired注解
Spring--通过注解来配置bean的更多相关文章
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
		
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
 - spring学习笔记 星球日two - 注解方式配置bean
		
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
 - Spring框架学习(6)使用ioc注解方式配置bean
		
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
 - Spring--通过注解来配置bean【转】
		
Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpat ...
 - Spring 基于注解零配置开发
		
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
 - Spring中三种配置Bean的方式
		
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
 - spring笔记--通过注解(annotation)配置Bean
		
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
 - Spring基础——在Spring Config 文件中配置 Bean
		
一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...
 - (spring-第4回【IoC基础篇】)spring基于注解的配置
		
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
 
随机推荐
- 兼容IE,Firefox,Opera等浏览器的添加到收藏夹js代码实现
			
function AddToFavorites() { var title = document.title; var url = location.href; if (window.sidebar) ...
 - easy ui 表单提交添加遮罩,避免数据重复提交
			
如下图: //点击提交按钮保存数据 $('#btn_submit').click(function () { //增加遮罩层 $.messager.progress({ title: '温馨提示', ...
 - EXTJS 4.2 资料 控件之Grid 行编辑绑定下拉框,并点一次触发一次事件
			
主要代码: { header: '属性值', dataIndex: 'PropertyValueName', width: 130, editor: new Ext.form.field.ComboB ...
 - DB天气app冲刺二阶段第一天
			
原来找人也是个力气活...好费劲呀..今天的进度有点慢,,确切的说是没有什么进度 因为不会弄了..加上今天一个劲的找同学帮忙写评论.心思没定下来 根本没思路了..明天按照今天的计划继续冲刺..
 - ExtJs 4.2.1 复选框数据项动态加载(更新一下)
			
最近在做博客项目,后台管理用的是ExtJs4.2.1版本,因为是初学所以在使用的时候也遇到不少的这样或那样的问题,也写了不少这方面的博客,今天要写的博客是关于复选框数据项动态的加载功能,以前也没用过, ...
 - NOSQL Mongo入门学习笔记 - 数据的基本插入(二)
			
成功运行起来mongo之后,进入了命令行模式,mongo默认会选择test数据库 1. 使用db命令打印出来当前选定的数据库: > db test 2. 使用show dbs 命令可以打印出数据 ...
 - Mybatis的分页查询
			
示例1:查询业务员的联系记录 1.控制器代码(RelationController.java) //分页列出联系记录 @RequestMapping(value="toPage/custom ...
 - 手机开发类型jquery的框架Zepto(API)
			
Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. http://www.html-5.cn/M ...
 - 其实,前面倒腾那么多,只是为了想玩SPRING BOOT
			
嘿嘿,,曲线达到.. 看来看来很多国内的速成,都不爽. 官方教程最体贴~~~:) http://docs.spring.io/spring-boot/docs/current/reference/ht ...
 - html10个特效(转载)
			
http://www.html5tricks.com/10-html5-jquery-image-animatin.html 现在网页上的图片已经不再是10几年前那种低像素的静态图片了,有了HTML5 ...