spring配置bean和component-scan的两种方式

  1. 创建简单的maven工程,在pom文件中添加spring的依赖,spring-context;
    找依赖的一个网址是:
    https://mvnrepository.com/
  2. 如果使用xml方式配置spring容器,主要是标签的形式
<?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" 
       xmlns:tx="http://www.springframework.org/schema/tx" 
       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.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd"> 
       
      <bean id="student" class="com.xw.bean.Student">
      	<property name="name" value="sssss"></property>
      	<property name="age" value="44"></property>
      </bean>
      
      <!--  只要标注了@Controller,@Service,@Repository,@Component都会被加载到容器中 -->
      <context:component-scan base-package="com.xw" use-default-filters="false">
      	<context:include-filter type="annotation" expression="com.xw.controller"/>
      </context:component-scan>
</beans>
  1. 也可以使用java类来进行相关配置
package com.xw.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.xw.bean.Student;

@Configuration	//声明这是一个配置的类
@ComponentScan( value={"com.xw"} )   //配置自动扫描的包,可以指定包含的和不包含的

//FilterType 扫描包时按哪些规则来排除或包含组件
//FilterType.ANNOTATION 按照注解
//FilterType.ASSIGNABLE_TYPE  按给定类型
//FilterType.ASPECTJ  按ASPECTJ表达式
//FilterType.REGEX  按正则表达式
//FilterType.CUSTOM  自定义规则,可以写自己的自定义规则的类
public class MainConfig {

	//配置一个bean,默认的id是方法名
	@Bean(value="student1")
	public Student student(){
		return new Student("sdf", 3);
	}
}
  1. 在使用@ComponentScan注解的时候,可以配置排除组件或者是包含组件
    配置包含组件:必须要把默认配置改为false
@ComponentScan(   
        value={"com.xw"},
		includeFilters={
				@Filter(type=FilterType.ANNOTATION , classes={Controller.class})
		},
		useDefaultFilters = false
		)

配置不包含组件:

@ComponentScan(
		value={"com.xw"},excludeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
		@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={Student.class})
})
  1. 配置包含和不包含组件的规则有以下几种

    FilterType.ANNOTATION 按照注解
    FilterType.ASSIGNABLE_TYPE 按给定类型
    FilterType.ASPECTJ 按ASPECTJ表达式
    FilterType.REGEX 按正则表达式
    FilterType.CUSTOM 自定义规则,可以写自己的自定义规则的类,需要实现TypeFilter接口

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值