说说个人spring入门3--ioc经常使用注解

今天我会在spring入门1的基础上,对1中的bean.xml进行改进,采用注解配置,实现与1中一样的功能。java

经常使用注解

曾经XML的配置:spring

  • <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
  • scope="" init-method="" destroy-method="">
  • <property name="" value="" | ref=""></property>
  • </bean>

用于建立对象的api

  • 他们的做用就和在XML配置文件中编写一个<bean>标签实现的功能是同样的
  • Component:
  • 做用:用于把当前类对象存入spring容器中
  • 属性:
  • value:用于指定bean的id。当咱们不写时,它的默认值是当前类名,且首字母改小写。
  • Controller:通常用在表现层
  • Service:通常用在业务层
  • Repository:通常用在持久层
  • 以上三个注解他们的做用和属性与Component是如出一辙。
  • 他们三个是spring框架为咱们提供明确的三层使用的注解,使咱们的三层对象更加清晰

*
*
用于注入数据的框架

  • 他们的做用就和在xml配置文件中的bean标签中写一个<property>标签的做用是同样的
  • Autowired:
  • 做用:自动按照类型注入。只要容器中有惟一的一个bean对象类型和要注入的变量类型匹配,就能够注入成功
  • 若是ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
  • 若是Ioc容器中有多个类型匹配时:
  • 出现位置:
  • 能够是变量上,也能够是方法上
  • 细节:
  • 在使用注解注入时,set方法就不是必须的了。
  • Qualifier:
  • 做用:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用。可是在给方法参数注入时能够(稍后咱们讲)
  • 属性:
  • value:用于指定注入bean的id。
  • Resource
  • 做用:直接按照bean的id注入。它能够独立使用
  • 属性:
  • name:用于指定bean的id。
  • 以上三个注入都只能注入其余bean类型的数据,而基本类型和String类型没法使用上述注解实现。
  • 另外,集合类型的注入只能经过XML来实现。

*测试

  • Value
  • 做用:用于注入基本类型和String类型的数据
  • 属性:
  • value:用于指定数据的值。它可使用spring中SpEL(也就是spring的el表达式)
  • SpEL的写法:${表达式}

*
用于改变做用范围的prototype

  • 他们的做用就和在bean标签中使用scope属性实现的功能是同样的
  • Scope
  • 做用:用于指定bean的做用范围
  • 属性:
  • value:指定范围的取值。经常使用取值:singleton prototype

*
和生命周期相关 (了解)指针

  • 他们的做用就和在bean标签中使用init-method和destroy-methode的做用是同样的
  • PreDestroy
  • 做用:用于指定销毁方法
  • PostConstruct
  • 做用:用于指定初始化方法

*/code

项目工程

配置bean.xml
为了使用注解配置,必定要改变版本信息,以下文件头所示(能够在spring帮助文档中,点击core,ctrl+f查找xmlns:cont,复制看到的版本信息到bean.xml便可)。component

<?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在建立容器时要扫描的包,配置所须要的标签不是在bean.xml的约
    束中,而是一个名称为context名称空间和约束中-->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>

将类加入容器
咱们在要添加到spring容器的类前面加@Component注解,便可将其加入容器。可是你觉得这样就好了吗?想多了。spring怎么知道哪一个类被注解了呢 ?以咱们须要告诉spring到哪里扫描注解。在bean.xml的</context:component-scan>标签中配置base-package属性便可,咱们选择com.itheima,就会扫描这个包下全部类上的注解。xml

@Component
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;

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

    }
}

固然也能够为上述注解设置一个Id

@Component(value = "AccountService")
public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao;

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

    }
}

持久层注解

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    public  void saveAccount(){
        System.out.println("保存了帐户");
    }
}

对其测试(这里同时用了第二种获取容器对象的方法)

IAccountDao addo = ac.getBean("accountDao",IAccountDao.class);
        System.out.println(addo);

注入数据
上述代码调用咱们调用as.saveAccount()方法会报空指针异常,这是由于as中的private IAccountDao accountDao没有注入对象。解决方式以下

*Autowired注解
使用了该注解后,须要注入对象的类会在容器中寻找相同类型的类,找到一个就直接把它注入。找到多个,按名字相同的注入,不然报错

@Component(value = "AccountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    
    private IAccountDao accountDao;
    public void  saveAccount(){
        accountDao.saveAccount();
    }
}
@Repository("accountDao2")
public class AccountDaoImpl2 implements IAccountDao {
    public  void saveAccount(){
        System.out.println("保存了帐户222");
    }
}

多个Bean类型的解决方式
1 Autowired配合Qualifier使用

@Component(value = "AccountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    @Qualifier(value = "accountDao1")
    private IAccountDao accountDao;
    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

2 Resource注解

@Service(value = "AccountService")
public class AccountServiceImpl implements IAccountService {
    @Resource(name="accountDao1")
    private IAccountDao accountDao;
    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

不过使用该注解须要导包(导包时可能发生不知名错误,但就是这个包,重导几回就行)

<dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
相关文章
相关标签/搜索