Spring:IoC 用法(六、自动扫描Bean用法)
2017-07-19 10:12
295 查看
发现 Bean 扩展
1、定义Bean
定义Controller 控制层@Controller public class TestController { // 测试方法 public String testController() { return "SUCCESS"; } }
定义Service服务层接口
public interface TestService { // 测试注入:调用方法 String testService(); }
定义Service服务层接口实现
// 接口实现类 @Service public class TestServiceImpl implements TestService { @Autowired private TestRepository repository; // 测试方法 public String testService() { return repository.testRepository(); } }
定义Repository数据层接口
public interface TestRepository { // 测试注入:调用方法 String testRepository(); }
定义Repository数据层接口实现
// 接口实现类 @Repository public class TestRepositoryImpl implements TestRepository { // 测试注入方法 public String testRepository() { return "------------------------- >>> 获取数据成功!!"; } }
2、发现Bean
1)、XML 发现 Bean
扫描指定包路径及以下所有子路径,带有@Component注解及子注解的类,可以同时存在多个<context:component-scan base-package="test.define"/>
use-default-filters 属性:自动配置时,细粒度初始化Bean加载
true:默认为true
<context:component-scan base-package="test.define" use-default-filters="true"> <!-- 指定路径下,排除带有@Service --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
false:如果不想发现指定路径所有带注解的类,而是想发现指定的类,或者排除指定的类
<context:component-scan base-package="test.define" use-default-filters="false"> <!-- 指定路径下,仅扫描带有@Controller的类 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
2)、Java 发现 Bean
默认扫描当前包及子包,带有@Component注解及子注解的类@Configuration @ComponentScan public class TestConfig { }
扫描单个指定路径下的包及子包,带有@Component注解及子注解的类
@Configuration @ComponentScan("test.define") public class TestConfig { }
@Configuration @ComponentScan(basePackages = "test.define") public class TestConfig { }
扫描多个指定路径下的包及子包,带有@Component注解及子注解的类
@Configuration @ComponentScan(basePackages = {"test.define","test.define"}) public class TestConfig { }
发现一个带有@Component注解及子注解的指定类
@Configuration @ComponentScan(basePackageClasses = TestController.class) public class TestConfig { }
发现多个带有@Component注解及子注解的指定类
@Configuration @ComponentScan(basePackageClasses = {TestController.class, TestServiceImpl.class, TestRepositoryImpl.class}) public class TestConfig { }use-default-filters 属性:自动配置时,细粒度初始化Bean加载
true(默认为true)
排除带有@Service的注解
@Configuration @ComponentScan(useDefaultFilters = true, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Service.class)}) public class TestConfig { }
false
仅扫描带有@Controller的注解
@Configuration @ComponentScan(useDefaultFilters = false, includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)}) public class TestConfig { }
3、测试代码
依赖<properties> <spring.version>4.2.1.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 依赖注入 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- 仅测试 --> <dependency> <groupId> 7db9 junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
测试类
public class Test { @org.junit.Test public void testXMLConfig() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring.xml"); TestController controller = context.getBean(TestController.class); String data1 = controller.testController(); System.out.println("Controller 结果:" + data1); TestService service = context.getBean(TestServiceImpl.class); String data2 = service.testService(); System.out.println("Service 结果:" + data2); context.close(); } @org.junit.Test public void testJavaConfig() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); TestService service = context.getBean(TestServiceImpl.class); String data2 = service.testService(); System.out.println("Service 结果:" + data2); context.close(); } }
相关文章推荐
- 第二章 IoC Spring自动扫描和管理Bean
- Spring自动扫描组件或Bean介绍
- Spring整理系列(11)——@Configuration注解、@Bean注解以及配置自动扫描、bean作用域 发表于2016/8/11 15:16:16 10628人阅读 分类: Spring
- [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
- 第四章 Spring进阶-自动扫描和管理Bean
- Spring:IoC 用法(八、<bean>用法)
- 让Spring自动扫描和管理Bean
- spring学习笔记4--让sping自动扫描和管理Bean
- java Spring常用注解,自动扫描装配Bean
- Spring、Spring自动扫描和管理Bean
- Spring常用注解,自动扫描装配Bean
- Spring自动扫描和装配bean
- Spring自动扫描无法扫描jar包中bean的解决方法
- Spring学习笔记(9)----让Spring自动扫描和管理Bean
- Spring常用注解,自动扫描装配Bean
- 浅谈Spring装配Bean之组件扫描和自动装配
- Spring、Spring自动扫描和管理Bean
- spring 自动扫描bean进入applicationContext.xml中
- Spring自动扫描和管理bean
- JavaEE_SSH_Spring学习笔记(9)----让Spring自动扫描和管理Bean