Spring IOC容器Bean注解创建对象组件扫描

网友投稿 327 2022-07-27


目录Spring IOC Bean注解对象组件扫描一、spring 针对 bean 管理中创建对象提供注解1. 引入依赖2. 开启组件扫描3. 创建类,并添加注解来创建对象4. 测试一下二、组件扫描的其他过滤条件1. include-filter2. exclude-filter

Spring IOC Bean注解对象组件扫描

什么是注解?

注解是代码里的特殊标记,格式:

@注解名称(属性名称=属性值, 属性名称2=属性值...)

可以作用在:类、方法、属性上面。

使用注解的目的:简化 xml 配置,让使用配置更简洁优雅。

一、spring 针对 bean 管理中创建对象提供注解

@Component@Service@Controller@Repository

这 4 个注解功能是一样的,都可以用来创建 bean 实例。

但是通常实际应用中,为了让开发人员更加清晰当前组件所扮演的角色,一般会让它们各自应用在不同的层。比如 @Service 用在逻辑层、@Service 用在web层等。

示例

1. 引入依赖

引入 AOP 依赖,可以在这里搜索下载需要的 jar 包。

2. 开启组件扫描

其实就是告诉 spring 你要在什么地方使用注解。通过在 xml 里配置,spring就会到对应位置扫描下面的类:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

现在,我这里有多个包:

如果要扫描多个包,可以用逗号,隔开:

如果所有下层的包都要扫描,那也可以之间写上层的目录:

3. 创建类,并添加注解来创建对象

package com.pingguo.spring5.service;

import org.springframework.stereotype.Component;

@Component(value = "userService")

public class UserService {

public void add() {

System.out.println("service add() ... ...");

}

}

现在终于不用去 xml 写 bean 标签了。

@Component(value = "userService"),这里 value 的值,等同于

4. 测试一下

package com.pingguo.spring5.testdemo;

import com.pingguo.spring5.service.UserService;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {

@Test

public void testService() {

ApplicationContext context

= new ClassPathXmlApplicationContext("bean1.xml");

UserService userService = context.getBean("userService", UserService.class);

System.out.println(userService);

userService.add();

}

}

运行一下:

com.pingguo.spring5.service.UserService@60611244

service add() ... ...

Process finished with exit code 0

成功。

如果把注解换成其他几个,重新运行测试方法,结果也是一样的。

二、组件扫描的其他过滤条件

在上述的开启扫描配pBwABu置:

意思就是说扫描包路径com.pingguo.spring5下的所有类。

其实这里有个属性 use-default-filters,默认情况下就是等于true,也就是使用默认过滤规则,会去扫描路径下的所有。

那如果use-default-filters="false",就是不使用默认过滤条件,我们可以自己配置过滤。

1. include-filter

在指定的包路径下,只扫描包含了某种注解的类。比如:

这就是说,在路径com.pingguo.spring5下,只扫描Service注解的类。

2. exclude-filter

与上面相反,这里是除了xx之外,都去扫描。

做了改动之后,意思也变了。现在是说在路径com.pingguo.spring5下,除了Service注解的类,其他都扫描。

以上就是Spring IOC容器Bean注解创建对象组件扫描的详细内容,更多关于Spring IOC Bean注解对象组件扫描的资料请关注我们其它相关文章!

4. 测试一下

package com.pingguo.spring5.testdemo;

import com.pingguo.spring5.service.UserService;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {

@Test

public void testService() {

ApplicationContext context

= new ClassPathXmlApplicationContext("bean1.xml");

UserService userService = context.getBean("userService", UserService.class);

System.out.println(userService);

userService.add();

}

}

运行一下:

com.pingguo.spring5.service.UserService@60611244

service add() ... ...

Process finished with exit code 0

成功。

如果把注解换成其他几个,重新运行测试方法,结果也是一样的。

二、组件扫描的其他过滤条件

在上述的开启扫描配pBwABu置:

意思就是说扫描包路径com.pingguo.spring5下的所有类。

其实这里有个属性 use-default-filters,默认情况下就是等于true,也就是使用默认过滤规则,会去扫描路径下的所有。

那如果use-default-filters="false",就是不使用默认过滤条件,我们可以自己配置过滤。

1. include-filter

在指定的包路径下,只扫描包含了某种注解的类。比如:

这就是说,在路径com.pingguo.spring5下,只扫描Service注解的类。

2. exclude-filter

与上面相反,这里是除了xx之外,都去扫描。

做了改动之后,意思也变了。现在是说在路径com.pingguo.spring5下,除了Service注解的类,其他都扫描。

以上就是Spring IOC容器Bean注解创建对象组件扫描的详细内容,更多关于Spring IOC Bean注解对象组件扫描的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java实现简单学生成绩档案管理系统(java学生成绩管理系统登录界面)
下一篇:Spring IOC容器的Bean管理基于注解属性注入方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~