spring 使用注解注入

要使用注解,首先要给bean.xml配置一下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--告知spring在创建容器时要扫描的包,
            配置所需要的标签不是在beans的约束中,而是一个名称为context名称空间和约束中-->
    <context:component-scan base-package="com.itheima">
    </context:component-scan>

</beans>
package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 曾经xml的配置
 * <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
 *      scope="" init-method="" destroy-method="">
 *          <property name="" value/ref=""></property>
 *      </bean>
 *
 * 注解的功能分类:
 * 用于创建对象的注解
 *      和xml配置文件里的<bean>标签类似
 *      @Component:
 *          作用:用于把当前类对象存入spring容器中
 *          属性:
 *              value:用于指定bean的id,当不写时,默认值是当前类名且首字母改小写
 *      @Controller:一般用在表现层
 *      @Service:一般用在业务层
 *      @Repository:一般用在持久层
 *      以上三个注解的作用和属性与Component是一模一样的
 *
 * 用于注入数据的
 *      和xml配置文件里的<bean>里的<property>标签作用类似
 *      @Autowired:
 *          作用:自动按照类型注入,只要容器中有唯一的bean对象类型和要注入的变量类型匹配,就可以注入成功
 *                  如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错
 *                  如果有多个匹配:首先按类型圈定可匹配的bean对象,再使用变量名称作为bean的id,在已经圈定出来的对象里继续查找
 *          出现位置:
 *              可以是变量上,也可以是方法上
 *          细节:
 *              在使用注解注入时,set方法就不是必须的
 *      @Qualifier:
 *          作用:在按照类中注入的基础上,再按照名称注入。他在给类成员注入时不能单独使用,但在给方法参数注入时可以
 *          属性:
 *              value:用于指定注入bean的id
 *      @Resource:(好像用不了)
 *          作用:直接按照bean的id注入。可以独立使用
 *          属性:
 *              name:用于指定bean的id
 *      三个注入都只能注入其他bean类的数据,而基本类型和String类型无法使用上述注解注入
 *      集合类型的注入只能通过xml来实现
 *       @Value
 *          作用:用于注入基本类型和String类型的数据
 *          属性:
 *              value:用于指定数据的值,同时可以使用spring中的SpEL(spring中的el表达式)
 *                    SpEL的写法:${表达式}
 *
 *  用于改变作用范围的
 *      和<bean scopr="">类似
 *      Scope
 *          作用:用于指定bean的作用范围
 *          属性:
 *              value:指定范围的取值。常用取值:singleton prototype
 *
 * 和生命周期相关
 *      和<bean init-method="" destroy-method="">类似
 *      PreDesTory
 *          作用:用于指定销毁方法
 *      PostConstruct
 *          作用:用于指定初始化方法
 *
 *
 *
 * */
@Component(value="accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    @Qualifier("accountDao") //必须要和Autowired配合
    private IAccountDao accountDao ;

    public AccountServiceImpl(){
        System.out.println("service对象创建了");
    }

    @PostConstruct
    public void init(){
        System.out.println("初始化方法");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("销毁方法");
    }

    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

 

注意:使用PostConstruct,以及PreDestroy,要在pom配置里导入依赖

<dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

 

posted on 2020-04-02 17:54  zsben  阅读(698)  评论(0编辑  收藏  举报

导航