一,配置文件版
<?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.xsd">
<!-- 扫包 加入此配置后,会自动扫描com。dream下面中带有@Controller,@Service,@Repository,@Component-->
<!--<context:component-scan base-package="com.dream"></context:component-scan>-->
<bean id="student" class="com.dream.bean.Student">
<property name="age" value="18"></property>
<property name="name" value="lisi"></property>
</bean>
</beans> |
在配置文件中加入<context:component-scan base-package="com.dream"></context:component-scan>,spring容器在启动的时候就会自动扫描com.dream包下面所有带@Controller,@Service,@Repository,@Component注解的类,并加载到容器。
二,注解版
1,在Config类中加入
@ComponentScan(value = "com.dream")
package com.dream.config; import com.dream.bean.Student; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; //配置类(等价原来的配置文件) @Configuration @ComponentScan(value = "com.dream") public class BeansConfig { //给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id @Bean public Student student(){ return new Student("王五",28); } } |
2,添加下面三个类
package com.dream.controller; import org.springframework.stereotype.Controller; @Controller public class BookController { } |
package com.dream.service; import org.springframework.stereotype.Service; @Service public class BookService { } |
package com.dream.dao; import org.springframework.stereotype.Repository; @Repository public class BookDao { } |
3,在pom文件加入junit依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> |
4,测试类
package com.dream; import com.dream.config.BeansConfig; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class IOCTest { @Test public void test1(){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeansConfig.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for(String bean:beanDefinitionNames){ System.out.println(bean); } } } |
5,测试结果 红色字段代表我们自己注入容器的类,绿色部分是spring启动时,spring中需要注入的部分
org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory beansConfig bookController bookDao bookService student |
6,测试 excludeFilters,配置类如下
package com.dream.config;
import com.dream.bean.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
//配置类(等价原来的配置文件)
@Configuration
@ComponentScan(value = "com.dream",excludeFilters = {
@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class, Service.class})//排除Controller和Service以外的其他需要注入的类
})
public class BeansConfig {
//给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id
@Bean
public Student student(){
return new Student("王五",28);
}
}
|
结果中没有带@Controller和@Service的类
org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory beansConfig bookDao student |
7,测试 includeFilters,配置如下,使用includeFilters ,需要将useDefaultFilters 设置为false才生效
package com.dream.config;
import com.dream.bean.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
//配置类(等价原来的配置文件)
@Configuration
@ComponentScan(value = "com.dream",includeFilters = {
@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class, Service.class})//排除Controller和Service以外的其他需要注入的类
},useDefaultFilters = false)
public class BeansConfig {
//给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id
@Bean
public Student student(){
return new Student("王五",28);
}
}
|
测试结果
org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory beansConfig bookController bookService student |