Spring -- 注解配置Bean

通过注解配置Bean

特定组件包括:
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件
上面的组件可以混用,因为IOC容器并无法区分当前类是否为业务、持久、还是表现层。

对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

使用注解配置Bean前,我们需要引入spring-aop-4.1.7.RELEASE.jar包,否则会报异常。

我们先来通过注解创建几个Bean
组件类

@Component
public class Test {

}

Controllor类

@org.springframework.stereotype.Controller
public class Controller {

}

Service类

@org.springframework.stereotype.Service
public class Service {

}

Repository类

@Repository
public class Respository {

}

我们来看下,工程目录
这里写图片描述

配置IOC容器

<context:component-scan base-package="com.gp.spring.annotation"></context:component-scan>

指定我们要扫描的包,spring会为我们自动扫描此包及当前包的子包。

测试

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring_annotation.xml");
        Test t = (Test) context.getBean("test");
        System.out.println(t);
        Controller c = (Controller) context.getBean("controller");
        System.out.println(c);
        Service s = (Service) context.getBean("service");
        System.out.println(s);
        Respository r = (Respository) context.getBean("respository");
        System.out.println(r);
    }

输出结果

com.gp.spring.annotation.Test@bddc6
com.gp.spring.annotation.controller.Controller@10cf230
com.gp.spring.annotation.service.Service@f032fa
com.gp.spring.annotation.respository.Respository@1a2fc31

成功完成注解配置Bean。


通过注解配置类之间的关系

我们有过spring基础的同学都知道,spring为我们省去了new 的操作,而是通过IOC容器为我们new 一个目标对象,之前是通过xml配置的,现在我们通过注解的方式,来演示这个配置。

下面主要介绍以下几个注解
@Autowired

  • 如他的名字一样,自动注入,为我们将类注入到IOC容器,也就是new了一个对象。
  • 另外还有2个注解与@Autowired功能类似,@Resource 和 @Inject
  • @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
  • @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
  • @Autowired提供reqired 判断,如果未在IOC容器中找到目标Bean则创建一个将对象指向NULL

@Qualifier

  • 在开发过程中我们通常会用到接口,然后是实现类,当同一接口有多个实现类的时候,再通过@Autowired注入对象的时候,就会出现异常,因为spring不知道你要注入的是哪个实现类
  • 这时,我们就可以通过@Qualifier指定目标实现类

下面我们来看看代码

工程目录结构
这里写图片描述

Controller代码

public class Controller {

    @Autowired
    @Qualifier("animalServiceImpl")
    public UserService service;

    public void execute(){
        System.out.println("Controller class execute method");
        service.save();
    }
}

代码中,我们定义了public UserService service;然后通过@Autowired注入实现类
@Qualifier(“animalServiceImpl”),使用此注解指定在同一接口多个实现类情况下,要调用的目标实现类

Service接口

public interface UserService {
    public void save();
}

UserServiceImpl实现类

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private Respository respository;

    public void save() {
        System.out.println("service class save method");
        respository.save();
    }
}

AnimalServiceImpl实现类

@Service
public class AnimalServiceImpl implements UserService {

    @Autowired
    private Respository respository;

    @Override
    public void save() {
        respository.save();
        System.out.println("AnimalServiceImpl class save method");
    }

}

Respository持久化类

@Repository
public class Respository {

    public void save(){
        System.out.println("Respository class save method");
    }
}

测试方法

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring_annotation.xml");
        Controller c = (Controller) context.getBean("controller");
        c.execute();
    }
}

输出结果

Controller class execute method
Respository class save method
AnimalServiceImpl class save method
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值