spring之通过注解方式配置Bean(一)
(1)组件扫描:spring能够从classpath下自动扫描、侦测和实例化具有特定注解的组件。
(2)特定组件包括:
- @Component:基本注解,标识一个受spring管理的组件;
- @Respority:标识持久层组件;
- @Service:标识服务层(业务层)组件;
- @Controller:标识表现层组件;
(3)对于扫描到的组件,spring有默认的命名规则:使用非限定类名。第一个字母小写,也可以在注解中通过value属性值标识组件的名称。
(4)当在组件类上使用了特定的注解之后,还需要在spring的配置文件中声明<context:component-scan>:
- base-package属性指定一个需要扫描的基类包,spring容器将会扫描这个基类包里及其子包的所有类;
- 当需要扫描多个包时,可以使用逗号分隔;
- 如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类,示例:
<contest:component-scan base-package=":com.gong.spring.beans" resource-pattern="autowire/*.class"> - <context:include-filter>子节点表示要包含的目标类
- <context:exclude-filter>子节点表示要排除在外的目标类
- <context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点;
一、所需环境
引入以下jar包,加入到build path中:

两个连接数据库的,五个spring核心包,aop是额外的使用注解方式需要引入的包,一定要记得引入这个包。
基本目录如下:

二、基本配置
TestObjec.java
package com.gong.spring.beans.annotation; import org.springframework.stereotype.Component; @Component
public class TestObject { }
UserController.java
package com.gong.spring.beans.annotation.controller; import org.springframework.stereotype.Controller; @Controller
public class UserController {
public void execute() {
System.out.println("UserController的execute方法");
}
}
UserRepository.java
package com.gong.spring.beans.annotation.repository;
public interface UserRepository {
public void save();
}
UserRepositoryImpl.java
package com.gong.spring.beans.annotation.repository; import org.springframework.stereotype.Repository; //可以指定使用时的名字
@Repository(value="userRepository")
public class UserRepositoryImpl implements UserRepository{ @Override
public void save() {
// TODO Auto-generated method stub
System.out.println("UserReposityImpl的save方法");
} }
UserService.java
package com.gong.spring.beans.annotation.service; import org.springframework.stereotype.Service; @Service
public class UserService {
public void add() {
System.out.println("UserService中的add方法");
}
}
Main.java
package com.gong.spring.beans.annotation; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gong.spring.beans.annotation.controller.UserController;
import com.gong.spring.beans.annotation.repository.UserRepository;
import com.gong.spring.beans.annotation.service.UserService; public class Main {
public static void main(String[] args) {
//1.创建spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
//2.从容器中获取Bean实例
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);
} }
beans-annotation.xml(需要引入context命名空间)
<?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.3.xsd"> <!-- 配置springIOC容器扫描的包 -->
<context:component-scan base-package="com.gong.spring.beans.annotation"></context:component-scan> </beans>
输出:

说明这些带有注解的类已经被spring所识别并被IOC容器所管理。需要注意的是,默认情况下获取bean的实例时,名字是类名,但首字母是小写。例如TestObject通过getBean("testObject")来获取,当然,也可以在注解时使用value属性来声明bean的名字。
三、子节点配置
再来看:
<!-- 配置springIOC容器扫描的包 -->
<context:component-scan base-package="com.gong.spring.beans.annotation"
resource-pattern="repository/*.class"></context:component-scan>
指定扫描的模式之后,我们就只能访问到repository下面的组件了,即输出:
=
在<context:include-filter>和<context:exclude-filter>子节点支持多种类型的表达式:
- annotation:所有标注了XxxAnnotation的类;
- assignable:所有继承或扩展XxxService的类;
- aspectj
- regex
- custom
(1)使用annotation的方式
<!-- 配置springIOC容器扫描的包 -->
<context:component-scan base-package="com.gong.spring.beans.annotation">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
不包含org.springframework.stereotype.Repository,即输出:

<!-- 配置springIOC容器扫描的包 -->
<context:component-scan base-package="com.gong.spring.beans.annotation"
use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
只包含org.springframework.stereotype.Repository,注意需要配置use-default-filters为false,即输出:

(2)使用assignable方式
<!-- 配置springIOC容器扫描的包 -->
<context:component-scan base-package="com.gong.spring.beans.annotation">
<context:exclude-filter type="assignable" expression="com.gong.spring.beans.annotation.repository.UserRepository"/>
</context:component-scan>
不包含UserRepository接口及其实现类的。
<!-- 配置springIOC容器扫描的包 -->
<context:component-scan base-package="com.gong.spring.beans.annotation"
use-default-filters="false">
<context:include-filter type="assignable" expression="com.gong.spring.beans.annotation.repository.UserRepository"/>
</context:component-scan>
设置use-default-filters="false"。只包含UserRepository接口及其实现类的。
spring之通过注解方式配置Bean(一)的更多相关文章
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- Spring使用ioc注解方式配置bean
context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...
- Spring初学之注解方式配置bean
直接看代码: UserController.java package spring.beans.annotation.controller; import org.springframework.be ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- 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 boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring中基于注解方式管理bean
操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...
- spring笔记--通过注解(annotation)配置Bean
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
随机推荐
- 从HelloWorld看Knative Serving代码实现
摘要: Knative Serving以Kubernetes和Istio为基础,支持无服务器应用程序和函数的部署并提供服务.我们从部署一个HelloWorld示例入手来分析Knative Servin ...
- @loj - 6354@「CodePlus 2018 4 月赛」最短路
目录 @description@ @solution@ @accepted code@ @details@ @description@ 企鹅国中有 N 座城市,编号从 1 到 N . 对于任意的两座城 ...
- python 直接if判断和is not None的区别
tmpName = ''if tmpName: print tmpName #没有输出if tmpName is not None: print tmpName #有输出,是空行
- 非常优秀的Javascript(AJAX) 开发工具:Aptana
非常优秀的Javascript(AJAX) 开发工具:Aptana 下面我要向你介绍一款非常优秀的Javascript(AJAX) 开发工具:Aptana.应为它实在太棒了,所以我忍不住想向你推荐它. ...
- 推荐几个web前端比较实用的网站
第一次写博客,说实在的有点紧张和兴奋,哈哈哈哈,本人工作了有两年的时间,平时也有做笔记的习惯,但是都做得乱七八糟的,所以就想通过写博客来记录.好了,废话不多说了,先来几个觉得在工作中使用到的,还不错的 ...
- H3C 网络层
- supersocket为动态命令增加命令过滤器
由于我们无法像 C# 中一样方便的将 CLR 属性添加到 Python 文件或者函数中,因此我们需要定义一个函数 "getFilters()" 用于将命令过滤器方会给 CLR 运行 ...
- input 的 pattern 验证表单
pattern 用于定义验证输入正则表达式 pattern 属性适用于以下 <input> 类型:text, search, url, telephone, email 以及 passwo ...
- python中使用指定GPU
import os os.environ["CUDA_VISIBLE_DEVICES"] = "2" # 或 os.environ["CUDA_VIS ...
- vue-router在新窗口打开页面
1. <router-link>标签实现新窗口打开: <router-link target="_blank" :to="{path:'/app/dat ...