13Spring通过注解配置Bean(1)
配置Bean的形式:基于XML文件的方式;基于注解的方式(基于注解配置Bean;基于注解来装配Bean的属性)
下面介绍基于注解的方式来配置Bean。
——组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
——特定组件包括:
1.@Component:基本注解,标识了一个受Spring管理的组件
2.@Respository:标识持久层组件
3.@Service:标识服务层(业务层)组件
4.@Controller:标识表现层组件
——对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写;也可以在注解中通过value属性标识组件的名称
当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>(需要导入context命名空间):
——base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类。
——当需要扫描多个包时,可以使用逗号分隔
——如果仅希望扫描特定的类而非基包下的所有类,可适用resource-pattern属性过滤特定的类,实例:
<context:component-scan base-package=“com.spring.beans” resource-pattern="autowire/*.class"/>
——<context:include-filter>子节点表示要包含的目标类
——<context:exclude-filter>子节点表示要排除在外的目标类
——<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点
package annotation; import org.springframework.stereotype.Component; @Component
public class TestObject { }
package annotation.repository;
public interface UserRepository {
void save();
}
package annotation.repository; import org.springframework.stereotype.Repository; @Repository(value = "userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepositoryImpl save");
}
}
package annotation.controller; import org.springframework.stereotype.Controller; @Controller
public class UserController {
public void execute() {
System.out.println("UserController execute");
}
}
package annotation.service; import org.springframework.stereotype.Service; @Service
public class UserService {
public void add() {
System.out.println("UserService add");
}
}
<?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.xsd">
<!--指定Spring IOC容器扫描的包-->
<!--<context:component-scan base-package="annotation">
</context:component-scan>--> <!--可以通过resource-pattern指定扫描的资源-->
<!--<context:component-scan base-package="annotation"
resource-pattern="repository/*.class">
</context:component-scan>-->
<!--context:exclude-filter 子节点指定排除哪些指定表达式的组件-->
<!--context:include-filter 子节点指定包含指定表达式的组件,该子节点需要use-default-filters 配个使用-->
<context:component-scan base-package="annotation">
<!--annotation表示根据指定注解来包含还是不包含-->
<!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:exclude-filter>-->
<!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:include-filter>-->
<!--assignable根据指定类名来包含还是不包含-->
<!-- <context:exclude-filter type="assignable" expression="annotation.repository.UserRepository"></context:exclude-filter>-->
<!--<context:include-filter type="assignable" expression="annotation.repository.UserRepository"></context:include-filter>-->
</context:component-scan>
</beans>
package annotation;
import annotation.controller.UserController;
import annotation.repository.UserRepository;
import annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("13-1.xml");
TestObject to = (TestObject) ctx.getBean("testObject");
System.out.println(to); UserController userController = (UserController) ctx.getBean("userController");
System.out.println(userController); UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService); UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
System.out.println(userRepository);
}
}
13Spring通过注解配置Bean(1)的更多相关文章
- 14Spring通过注解配置Bean(2)
下面将对13Spring通过注解配置Bean(1)的中Repository.Service.Controller通过注解方式来建立关联. <context:component-scan>元 ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等
实验1:配置通过静态工厂方法创建的bean [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...
- 通过注解配置Bean
之前说的三种配置方式,都是使用XML配置,现在我们说说使用注解配置Bean. 这部分内容主要分为两个部分:使用注解配置Bean,使用注解配置Bean属性. 在classpath中扫描组件 组件扫描:S ...
- Spring(十五):通过注解配置 Bean
在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- Spring IOC机制之使用注解配置bean
一. 通过注解配置bean 1.1 概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2 ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
随机推荐
- 【笔记】对自定义异常的理解(Java)
原本,原有的异常是非手动地.自动地抛出的. 了解自定义异常时,发现其信息只是: 继承了谁,即它自己算哪种异常: 它的信息,比如一个ID,这个貌似还是可选的: 它是可被传入信息的 没有遗漏的话,就这仨了 ...
- 【OpenJ_Bailian - 4070 】全排列
全排列 Descriptions: 对于数组[1, 2, 3],他们按照从小到大的全排列是 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 现在给你一个正整数n,n小于8,输出 ...
- Lodash 方法库 原生实现 待完结。。。
'use strict' let _ = { // 将数组(array)拆分成多个 size 长度的区块,并将这些区块组成一个新数组. 如果array 无法被分割成全部等长的区块,那么最后剩余的元素将 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- ACM经历与感悟合集
ACM经历与感悟合集 ACM起步要点总结(转哈工大) ACM 荣耀之路-学习方法 ACM感悟 一位ACMer过来人的心得 ACM经历总结 大学ACM的总结 ACM大牛的退役贴 各大牛退役总结帖 女生应 ...
- Kruskal算法 分类: c/c++ 算法 2014-10-01 17:09 540人阅读 评论(0) 收藏
Kruskal算法计算最小生成树,只与边有关,时间复杂度O(eloge) 步骤: 1.将边按权值递增排序 2.依次取出边加入最小生成树中并保证无环,判断是否成环可利用并查集. 例:http://ac. ...
- 转 windows10 U盘系统启动盘怎么制作
转 windows10 U盘系统启动盘怎么制作 现将http://jingyan.baidu.com/article/9f7e7ec05e24d56f29155455.html 将dvd 写入 is ...
- 最近面试oracle 数据库的知识点
1. Oracle跟SQL Server 2005的区别? 宏观上: 1). 最大的区别在于平台,oracle可以运行在不同的平台上,sql server只能运行在windows平台上,由于windo ...
- MIUI类ROM如何正确修改dpi
(以下以MIUI为例) 在miui上,如果通过简单的修改build.prop会导致图标重绘错误,App图标分裂.此时配合一条简单的命令即可实现完美无bug的dpi修改. 1.使用终端模拟器执行su 2 ...
- Mac eclipse java6环境安装
由于旧版adtbundle eclipse需要java se6版本支持,而较新版本mac系统默认安装较高的java版本,所以这里需要卸载高版本jdk(1.8),然后安装1.6 mac删除jdk jav ...