spring系列 3-基于注解的IOC

配置文件的格式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"
       scope="" init-method="" destroy-method="">
     <property name="" value="" | ref=""/>
 </bean>
</beans>

那么注解中该怎么写呢?
首先我们看下spring中ioc的常用注解

Componment

用于创建对象,作用和在xml配置文件中编写一个<bean>标签实现的功能是一样的

  • Component:
    作用:用于把当前类对象存入spring容器中
    属性:value-用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母小写
  • Controller:一般用在表现层
  • Service:一般用在业务层
  • Repository:一般用在持久层
    以上三个注解的作用、属性和Componment相同
    他们三个是spring框架为我们提供明确的三层使用的注解,使三层对象更加清晰

注意使用注解,需要把配置文件中头部信息更换为:

<?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.mantishell"/>
</beans>

持久层
IAccountDao.java

package com.mantishell.dao;
public interface IAccountDao {
    void saveAccount();
}

AccountDaoImpl.java

package com.mantishell.dao.impl;
import com.mantishell.dao.IAccountDao;
import org.springframework.stereotype.Repository;
//@Repository未命名时,那么bean的id就是accountDaoImpl
@Repository("accountDao")//使用指定的bean的id名称
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

业务层
IAccountService.java

package com.mantishell.service;
public interface IAccountService {
    void saveAccount();
}

AccountServiceImpl.java

package com.mantishell.service.impl;
import com.mantishell.dao.IAccountDao;
import com.mantishell.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao = null;
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

表现层

package com.mantishell.ui;
import com.mantishell.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        as.saveAccount();
    }
}

Autowired

用于注入数据
作用和xml配置文件中的<bean>标签中写的<property>作用相同

  • Autowired:
    • 作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
      • 如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
      • 如果Ioc容器中有多个类型匹配时:首先按照类型,圈定出匹配的对象,然后使用要注入变量名的名称作为bean的id,在圈定的对象里查找,如果有id相同则成功,如果没有则报错。这时最好使用Qualifier注解。
    • 出现位置: 可以是变量上,也可以是方法上
    • 细节: 在使用注解注入时,set方法就不是必须的了。
  • Qualifier:
    • 作用:在按照类型注入的基础之上再按照名称注入。它在给类成员注入时必须和Autowired一起使用。但是在给方法参数注入时可以单独使用。
    • 属性: value:用于指定注入bean的id。
  • Resource
    • 作用:直接按照bean的id注入。它可以独立使用
    • 属性: name:用于指定bean的id。
      以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。
      另外,集合类型的注入只能通过XML来实现。
  • Value
    • 作用:用于注入基本类型和String类型的数据
    • 属性: value:用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)
      SpEL的写法:$
    @Autowired
    @Qualifier("accountDao1")//指定使用id=accountDao1的bean对象
    private IAccountDao accountDao = null;

或者

    @Resource(name="accountDao2")//指定使用id=accountDao2的bean对象
    private IAccountDao accountDao = null;

Scope

改变作用范围
作用和<bean>标签中scope属性功能相同,常用取值:singleton prototype

@Scope("singleton")
public class AccountServiceImpl implements IAccountService {

PreDestry和PostConstruct

作用和bean标签中的init-method和destroy-methode一样
PreDestroy:用于指定销毁方法
PostConstruct:用于指定初始化方法

    @PostConstruct
    public void init(){
        System.out.println("初始化方法执行了");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("销毁方法执行了");
    }

如果全部采用注解的方式,那么需要加一个配置类

package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class CDPlayerConfig {
}
posted @ 2020-03-24 22:41  mantishell  阅读(266)  评论(0编辑  收藏  举报