一、注解配置 bean
二、使用注解标识组件
使用注解将组件快速的加入到容器中需要几步:
1)给需要添加的组件上标四个注解的任何一个
2)告诉Spring自动扫描加了注解的组件;依赖context名称空间
3)一定要导入aop包,才支持加注解模式
//组件的id默认为_组件类名首字母小写
@Controller("bookServletABC")或(value="bookServletABC")
public class BookServlet{}
组件的作用域—默认为单例模式
默认为单例模式,可以使用 @Scope 注解修改 bean 的作用域。
@Scope(value = "prototype")
三、扫描组件
<context:component-scan base-package="com.spring.ioc" ></context:component-scan>
<context:component-scan base-package="com.spring.ioc.user" resource-pattern="service/*.class">
四、使用context:include-filter指定扫描包时要包含的类—包含
<context> 中有一个 use-default-filters 属性,该属性默认为 true,表示扫描该包下的所有类。
<--
只扫描进那些组件;默认都是全部扫描
一定要禁用掉默认的过滤规则才行:
use-default-filters属性="false"
-->
<context:component-scan base-package="com.achang" use-default-filters="false">
<!--指定只扫描那些组件-->
<context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
</context:component-scan>
【type="annotation"】:指定排除规则,按照注解进行包含或排除。标注指定注解的组件不要扫描
expression属性:指定注解类型的全类名
【type=assignable】:指定排除某个具体的类,按照类排除
expression属性:指定类的全类名
type=aspectj:后来aspectj表达式
type=custom:自定义一个TypeFilter;自己写代码觉得排除规则,编写一个类实现TypeFilter类的match()方法
tyoe=regex:正则表达式
类别
|
示例
|
说明
|
annotation
|
com.spring.XxxAnnotation
|
过滤所有标注了XxxAnnotation的类。这个规则根据目标组件是否标注了指定类型的注解进行过滤。
|
assignable
|
com.spring.BaseXxx
|
过滤所有BaseXxx类的子类。这个规则根据目标组件是否是指定类型的子类的方式进行过滤。
|
aspectj
|
com.spring.*Service+
|
所有类名是以Service结束的,或这样的类的子类。这个规则根据AspectJ表达式进行过滤。
|
regex
|
com\.spring\.anno\.*
|
所有com.atguigu.anno包下的类。这个规则根据正则表达式匹配到的类名进行过滤。
|
custom
|
com.spring.XxxTypeFilter
|
使用XxxTypeFilter类通过编码的方式自定义过滤规则。该类必须实现org.springframework.core.type.filter.TypeFilter接口
|
五、使用context:exclude-filter指定扫描包时不包含的类—排除
<context> 中有一个 use-default-filters 属性,该属性默认为 true,表示扫描该包下的所有类。
<--
扫描的时候可以排除不要的组件
-->
<context:component-scan base-package="com.achang" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
六、代码示例
DAO层:
package com.spring.ioc.User.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements IUserDao {
public UserDaoImpl() {
super();
System.out.println("UserDaoImpl构造器");
}
@Override
public void addUser() {
System.out.println("UserDaoImpl:添加成功");
}
}
Service层:
package com.spring.ioc.User.service;
import org.springframework.stereotype.Service;
import com.spring.ioc.User.dao.IUserDao;
import com.spring.ioc.User.dao.UserDaoImpl;
@Service
public class UserServiceImpl implements IUserService {
private IUserDao userDao = new UserDaoImpl();
@Override
public void addUser() {
userDao.addUser();
}
public UserServiceImpl() {
super();
System.out.println("UserServiceImpl构造器");
}
}
Controller层:
package com.spring.ioc.User.controller;
import org.springframework.stereotype.Controller;
import com.spring.ioc.User.service.IUserService;
import com.spring.ioc.User.service.UserServiceImpl;
@Controller
public class UserController {
private IUserService userService = new UserServiceImpl();
public void addUser() {
userService.addUser();
}
public UserController() {
super();
System.out.println("UserController构造器");
}
}
Spring 配置文件:
<!-- 扫描器 使用 include-filter-->
<context:component-scan base-package="com.spring.ioc.User" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <!-- 扫描添加注解为 Controller 的类 -->
<context:include-filter type="assignable" expression="com.spring.ioc.User.service.IUserService"/> <!-- 扫描类型是 IUserService 的类或其子类 -->
</context:component-scan>
<!-- 扫描器 使用 exclude-filter-->
<context:component-scan base-package="com.spring.ioc.User" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="assignable" expression="com.spring.ioc.User.service.UserServiceImpl"/>
</context:component-scan>
org.springframework.stereotype.Controller
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战