首页 技术 正文
技术 2022年11月14日
0 收藏 391 点赞 4,317 浏览 5080 个字

上一篇我们讲到,讲@Bean注解标在某个方法上,那么ioc容器启动的时候就会将方法返回值放到ioc容器中

在开发中,实际上包扫描用的比较多,接下来我们会介绍两种方式一种是基于xml,一种是基于注解。

咱们先来xml的形式进行包扫描

这里我用的是spring suit tool 版本的eclipse,专门开发spring项目的

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

勾选上后会有自动提示的,包括创建的时候

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

这次就有提示了

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

<?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">
<!-- class = "",输入person alt+/ 会提示 -->
<bean id = "person" class="com.liuyuan.bean.Person"></bean><bean id = "person2" class ="com.liuyuan.bean.Person">
<!-- 这里的name="",也可以使用快捷键alt+/ -->
<property name="age" value="18"></property>
<property name="name" value = "zhangsan"></property>
</bean>
<!-- 包扫描,只要标注了@Controller,@Service,@Component,@Repository就会被扫描,加进ioc容器 -->
<context:component-scan base-package="com.liuyuan"></context:component-scan></beans>

对于注解形式

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

加了@ComponentScan(value= “com.liuyuan”)

package com.liuyuan.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;import com.liuyuan.bean.Person;//配置类等同于之前的配置文件
@Configuration //告诉spring这是一个配置类
@ComponentScan(value= "com.liuyuan")
public class MainConfig {
//给容器注册一个Bean,类型为返回值得类型,
///id默认是以方法名作为id
@Bean("AAA")
public Person person() {
return new Person("lisi",20);
}
}

建了com.liuyuan.dao,com.liuyuan.service,com.liuyuan.controller 这些包,里面对应的建了BookDao,BookService,BookControler,在这三个类上面加了注解,@Repository,@Service,@Controller

注解,完了建立一个测试类

package com.liuyuan.test;import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.liuyuan.config.MainConfig;public class IOCTest {
@SuppressWarnings("resource")
@Test
public void test01() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
String [] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

这些Bean都是类名第一位小写

那个mainConfig为什么有呢?因为@Configration注解,可以点进去看一下

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

接下来我们定一些包扫描规则

比如排除一部分Bean

@ComponentScan(value= “com.liuyuan”,excludeFilters = {
                                @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
})

这个 filter实际上是一个数组,可以写多个,并且可以指定过滤的形式

package com.liuyuan.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;import com.liuyuan.bean.Person;
import com.liuyuan.service.BookService;//配置类等同于之前的配置文件
@Configuration //告诉spring这是一个配置类
@ComponentScan(value= "com.liuyuan",excludeFilters = {
@Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
})
public class MainConfig {
//给容器注册一个Bean,类型为返回值得类型,
///id默认是以方法名作为id
@Bean("AAA")
public Person person() {
return new Person("lisi",20);
}
}

再次启动测试类,发现bookController真的被排除了

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

接下来再来一个包含某些进行扫描的定义规则

还记得在xml里面配置的时候需要禁用掉默认的过滤规则

加个use-default-filters=”true”

如下

<?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">
<!-- class = "",输入person alt+/ 会提示 -->
<bean id = "person" class="com.liuyuan.bean.Person"></bean><bean id = "person2" class ="com.liuyuan.bean.Person">
<!-- 这里的name="",也可以使用快捷键alt+/ -->
<property name="age" value="18"></property>
<property name="name" value = "zhangsan"></property>
</bean>
<!-- 包扫描,只要标注了@Controller,@Service,@Component,@Repository就会被扫描,加进ioc容器use-default-filters="false"
只包含某些需要禁用掉默认的过滤规则,只包含啊才能生效-->
<context:component-scan base-package="com.liuyuan" use-default-filters="false"></context:component-scan></beans>

对于注解形式的

package com.liuyuan.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;import com.liuyuan.bean.Person;
import com.liuyuan.service.BookService;//配置类等同于之前的配置文件
@Configuration //告诉spring这是一个配置类
@ComponentScan(value= "com.liuyuan",/*excludeFilters = {
@Filter(type=FilterType.ANNOTATION,classes={Controller.class})*/
includeFilters= {@Filter(type=FilterType.ANNOTATION,classes= {Controller.class})
},useDefaultFilters=false)
public class MainConfig {
//给容器注册一个Bean,类型为返回值得类型,
///id默认是以方法名作为id
@Bean("AAA")
public Person person() {
return new Person("lisi",20);
}
}

spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

对于mainCofig已经看到@Component注解,Bean也看下吧

查看确实是没有,为什么会被spring 的ioc管理呢?

留个疑问吧

来源:淮安网站优化

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295