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

发现 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>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();
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值