Spring 基于 annotation 的自动装配
来源:互联网 发布:学历 英文 知乎 编辑:程序博客网 时间:2024/06/06 11:49
Spring 基于annotion 的自动装配
在 classpath 中扫描组件
组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 特定组件包括:
- @Component: 基本注解, 标识了一个受 Spring 管理的组件
- @Respository: 标识持久层组件
- @Service: 标识服务层(业务层)组件
- @Controller: 标识表现层组件
- 对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写.
- 也可以在注解中通过 value 属性值标识组件的名称
当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 : base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类. 当需要扫描多个包时, 可以使用逗号分隔. 如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类,示例:
<!-- 配置自动扫描的包: 需要加入 aop 对应的 jar 包 --> <context:component-scan base-package="com.shan.spring.annotation.generic"></context:component-scan>
<context:include-filter>
子节点表示要包含的目标类 <context:exclude-filter>
子节点表示要排除在外的目标类 <context:component-scan>
下可以拥有若干个 <context:include-filter>
和 <context:exclude-filter>
子节点 注意:(自己搜索) <context:include-filter>
和 <context:exclude-filter>
子节点支持多种类型的过滤表达式:
测试代码
package com.shan.spring.annotation.generic;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); UserService userService = (UserService) ctx.getBean("userService"); userService.addNew(new User()); RoleService roleService = (RoleService) ctx.getBean("roleService"); roleService.addNew(new Role()); }}
OUTPUT
addNew by com.shan.spring.annotation.generic.UserDao@58a90037Save:com.shan.spring.annotation.generic.User@74294adbaddNew by com.shan.spring.annotation.generic.RoleDao@70a9f84eSave:com.shan.spring.annotation.generic.Role@130f889```
UserService.java
package com.shan.spring.annotation.generic;import org.springframework.stereotype.Service;//若注解没有指定 bean 的 id, 则类名第一个字母小写即为 bean 的 id@Servicepublic class UserService extends BaseService<User>{}
BaseService.java
package com.shan.spring.annotation.generic;import org.springframework.beans.factory.annotation.Autowired;public class BaseService<T> { @Autowired private BaseDao<T> dao; public void addNew(T entity){ System.out.println("addNew by " + dao); dao.save(entity); }}
BaseDao.java
package com.shan.spring.annotation.generic;public class BaseDao<T> { public void save(T entity){ System.out.println("Save:" + entity); }}
关系图
0 0
- Spring 基于 annotation 的自动装配
- spring annotation 之 自动装配
- spring 基于注解的自动装配
- Spring基于Annotation为dao装配sessionFactory的问题
- 模仿 spring IOC Annotation版自动装配
- SPRING的自动装配
- spring的自动装配
- spring的自动装配
- Spring的自动装配
- spring的自动装配
- Spring的自动装配
- spring的自动装配
- spring的自动装配
- Spring的自动装配
- Spring 的自动装配
- spring的自动装配
- spring 的自动装配
- spring的自动装配
- uva 11426
- Android之Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE问题解决
- HDOJ 1210 Eddy's 洗牌问题
- Android Design Support Library详解
- java非对称加密算法原理及应用
- Spring 基于 annotation 的自动装配
- 一个单引号引发的MYSQL性能损失(转)
- android使用min3d引擎加载模型
- 根据生日获取星座
- HDU 1241 Oil Deposits
- 线段树第一题!(激动啊)
- JavaScript基本语法、Console用法和Up&Going书籍总结
- react-active init XXX 没反应
- RYU和mininet安装教程