Spring:IoC 用法(六、自动扫描Bean用法)

来源:互联网 发布:python try 重试 编辑:程序博客网 时间:2024/06/08 09:22

发现 Bean 扩展

1、定义Bean

定义Controller 控制层

@Controllerpublic class TestController {    // 测试方法    public String testController() {        return "SUCCESS";    }}

定义Service服务层接口

public interface TestService {    // 测试注入:调用方法    String testService();}

定义Service服务层接口实现

// 接口实现类@Servicepublic class TestServiceImpl implements TestService {    @Autowired    private TestRepository repository;    // 测试方法    public String testService() {        return repository.testRepository();    }}

定义Repository数据层接口

public interface TestRepository {    // 测试注入:调用方法    String testRepository();}

定义Repository数据层接口实现

// 接口实现类@Repositorypublic 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@ComponentScanpublic 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>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();    }}


原创粉丝点击