您当前的位置: 首页 >  spring

小志的博客

暂无认证

  • 1浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

spring的自动扫描组件——@ComponentScan和@Filter的示例

小志的博客 发布时间:2019-11-18 14:22:11 ,浏览量:1

@ComponentScan和和@Filter示例如下:

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:


	org.springframework
	spring-context
	5.2.0.RELEASE


	junit
	junit
	4.12
	test

2、创建一个person的实体类、controller层、service层、dao层、代码分别如下:

1)person的实体类

package com.rf.bean;

public class PersonBean {
	private String id;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "PersonBean [id=" + id + ", name=" + name + "]";
	}
	public PersonBean(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public PersonBean() {
		super();
	}
}

2)controller层

package com.rf.controller;

import org.springframework.stereotype.Controller;

@Controller
public class BookController {

}

3)service层

package com.rf.service;

import org.springframework.stereotype.Service;

@Service
public class BookService {

}

4)dao层

package com.rf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

}

3、创建一个配置类,代码如下;

package com.rf.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 org.springframework.stereotype.Service;

import com.rf.bean.PersonBean;
import com.rf.controller.BookController;
import com.rf.service.BookService;


//配置类==配置文件
@Configuration //告诉spring这是一个配置类
//包扫描,扫描com.rf包下只要标注了@Controller、@Service、@Repository、@Component的类
@ComponentScan(value="com.rf")
public class MyConfig {
	
	//给容器注册一个bean,类型为返回值的类型,id默认使用方法名称
	@Bean
	public PersonBean person(){
		//new一个带参构造方法
		return new PersonBean("1", "张三");
	}
}

4、配置类的代码类似于spring的bean.xml文件配置,代码如下:



  	
  	    
  	
  	
  		
  		
  	 
  


5、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rf.config.MyConfig;
import com.rf.config.MyConfig2;

public class Test {
	
	@org.junit.Test	
	public void test01(){
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
		String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
		//遍历ioc容器中有哪些bean
		for(String beanname:beanDefinitionNames){
			System.out.println(beanname);
		}
	}
}

6、把bean的xml文件配置注释掉(即第4步的代码注释掉),启动测试类,运行效果如下: 在这里插入图片描述7、修改第3步的配置类,代码如下:

package com.rf.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 org.springframework.stereotype.Service;

import com.rf.bean.PersonBean;
import com.rf.controller.BookController;
import com.rf.service.BookService;

/**
 * excludeFilters 指定扫描的时候按照什么规则排除哪些组件
 * includeFilters 指定扫描的时候包含哪些组件
 * @Filter 过滤注解;
 * 第一个参数过滤类型annotation(选择注解)、assignable_type(选择某个类)、aspectj(选择ASPECTJ表达式)、regex(选择正则表达式)、custom(自定义);
 * 第二个参数过滤带有哪些注解的类
 * */
@Configuration //告诉spring这是一个配置类
@ComponentScan(value="com.rf",excludeFilters={
	@Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
	@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class})
})
public class MyConfig {
	
	//给容器注册一个bean,类型为返回值的类型,id默认使用方法名称
	@Bean
	public PersonBean person(){
		//new一个带参构造方法
		return new PersonBean("1", "张三");
	}
}

8、把bean的xml文件配置注释掉(即第4步的代码注释掉),启动测试类,运行效果如下: 在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0428s