Spring框架学习之IOC(二) Spring框架学习之IOC(二)

接着昨天的内容,下面开始IOC基于注解装配相关的内容

在 classpath 中扫描组件

  <context:component-scan>

特定组件包括:
–@Component: 基本注解, 标识了一个受 Spring 管理的组件
–@Respository: 标识持久层组件
–@Service: 标识服务层(业务层)组件
–@Controller: 标识表现层组件
对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
用一个简单的代码演示一下
 1 package per.zww.spring.beans.annotation;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component
 6 public class TestObject {
 7     public void test(){
 8         System.out.println("testObject...");
 9     }
10 }
 1 package per.zww.spring.beans.annotation.respository;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository("userRepository")
 6 public class UserRepository {
 7     public void repository() {
 8         System.out.println("repository...");
 9     }
10 }

xml:

    <context:component-scan base-package="per.zww.spring.beans.annotation.*"></context:component-scan>

测试:

package per.zww.spring.beans.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("annotation.xml");
        TestObject testObject=(TestObject) applicationContext.getBean("testObject");
        testObject.test();
        UserRepository userRepository=applicationContext.getBean(UserRepository.class);
        userRepository.repository();
    }
}

另外我们可以过滤掉一些类:

–<context:include-filter> 子节点表示要包含的目标类
–<context:exclude-filter> 子节点表示要排除在外的目标类
 
组件装配:Bean属性的依赖
  <context:component-scan> 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.
  当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
用代码来演示一下组件装配
package per.zww.spring.beans.annotation.service;

public interface UserService {
    void service();
}
package per.zww.spring.beans.annotation.service;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImp implements UserService{

    @Override
    public void service() {
        System.out.println("service...");
    }

}
package per.zww.spring.beans.annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import per.zww.spring.beans.annotation.service.UserService;

@Controller
public class UserController {
    @Autowired
    @Qualifier("userService") //可以用Qualifier来精确匹配
    public UserService userService;
    
    public void controller() {
        System.out.println("controller...");
        userService.service();
    }
}

测试:

package per.zww.spring.beans.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import per.zww.spring.beans.annotation.controller.UserController;
import per.zww.spring.beans.annotation.service.UserService;

public class Main {
    public static void main(String[] args) {

        UserController userController=(UserController) applicationContext.getBean("userController");
        userController.controller();
    }
}
Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似,但推荐使用@Autowired注解。