Spring中常用的IoC注解

注解分为以下几个类型:

  • 用于创建对象的:它们的作用就和在XML文件中编写一个标签实现的功能是一样的。
  • 用于注入数据的:它们的作用就和在XML文件中的bean标签中写一个property标签的作用是一样的。
  • 用于改变作用范围的:它们的作用就和在bean标签中使用scope属性实现的功能是一样的。
  • 和生命周期相关的:它们的作用就和bean标签中使用init-method和destroy-method的作用是一样的。

用于创建对象的注解

@Component

作用:用于把当前类对象存入spring容器中。
属性:

  • value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写。

示例:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qublog</groupId>
    <artifactId>spring02_anno_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

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.qublog"></context:component-scan>
</beans>

AccountDao接口:

package com.qublog.dao;

//账户的持久层接口
public interface AccountDao {

    //模拟保存账户
    void saveAccount();
}

AccountDaoImpl类:

package com.qublog.dao.impl;

import com.qublog.dao.AccountDao;

//账户的持久层实现类
public class AccountDaoImpl implements AccountDao {

    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

AccountService接口:

package com.qublog.service;

//账户业务层接口
public interface AccountService {
    //模拟保存账户
    void saveAccount();
}

AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.dao.AccountDao;
import com.qublog.dao.impl.AccountDaoImpl;
import com.qublog.service.AccountService;
import org.springframework.stereotype.Component;

//账户的业务层实现类
@Component
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao = new AccountDaoImpl();

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

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

Client类:

package com.qublog.ui;

import com.qublog.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//模拟一个表现层,用于调用业务层
public class Client {
    //获取spring的IoC核心容器,并根据id获取对象
    public static void main(String[] args) {
        //获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取bean对象,下面两种方式都可以
        AccountService as = (AccountService)ac.getBean("accountServiceImpl");
        System.out.println(as);
        //as.saveAccount();
    }
}

由@Component衍生出的注解:

  • @Controller:一般用在表现层
  • @Service:一般用在业务层
  • @Repository:一般用于持久层

以上三个注解的作用和属性与@Component是一模一样,它们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰。

用于注入数据的注解

@Autowired

作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。如果IoC容器中没有任何bean的类型和要注入的变量类型匹配,则报错。如果IoC容器中有多个类型匹配时,同时变量名称与@Component的value相同时,则匹配,如果无相同名称,则会报错 。
出现位置:可以是成员变量上,也可以是方法上。
细节:在使用注解注入时,set方法就不是必须的了。

示例:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qublog</groupId>
    <artifactId>spring02_anno_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

AccountDao接口:

package com.qublog.dao;

//账户的持久层接口
public interface AccountDao {

    //模拟保存账户
    void saveAccount();
}

AccountDaoImpl类:

package com.qublog.dao.impl;

import com.qublog.dao.AccountDao;
import org.springframework.stereotype.Repository;

//账户的持久层实现类
@Repository(value = "accountDao1")
public class AccountDaoImpl implements AccountDao {

    public void saveAccount() {
        System.out.println("保存了账户1111");
    }
}

AccountService接口:

package com.qublog.service;

//账户业务层接口
public interface AccountService {
    //模拟保存账户
    void saveAccount();
}

AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//账户的业务层实现类
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao1;

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

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

Client类:

package com.qublog.ui;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//模拟一个表现层,用于调用业务层
public class Client {
    //获取spring的IoC核心容器,并根据id获取对象
    public static void main(String[] args) {
        //获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取bean对象,下面两种方式都可以
        AccountService as = (AccountService)ac.getBean("accountService");
//        System.out.println(as);
//
//        AccountDao ad = (AccountDao)ac.getBean("accountDao");
//        System.out.println(ad);
        as.saveAccount();
    }
}
@Qualifier

作用:在按照类型注入的基础之上,再按照名称注入,它在给类成员注入时,不能单独使用,但是在给方法参数注入时可以。
属性:

  • value:用于指定注入bean的id。

示例:
基于上面项目的基础上进行修改。
AccountDaoImpl2类:

package com.qublog.dao.impl;

import com.qublog.dao.AccountDao;
import org.springframework.stereotype.Repository;

//账户的持久层实现类
@Repository(value = "accountDao2")
public class AccountDaoImpl2 implements AccountDao {

    public void saveAccount() {
        System.out.println("保存了账户2222");
    }
}

AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

//账户的业务层实现类
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    @Qualifier(value = "accountDao2")
    private AccountDao accountDao;

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

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

作用:直接按照bean的id注入,可以独立使用。
属性:

  • name:用于指定bean的id。

示例:
AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

//账户的业务层实现类
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

//    @Autowired
//    @Qualifier(value = "accountDao2")
    @Resource(name = "accountDao2")
    private AccountDao accountDao;

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

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

以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现,另外集合类型的注入只能通过xml实现。

@Value

作用:用于注入基本类型和String类型的数据。
属性:

  • value:用于指定数据的值,它可以使用spring中的SpEL(也就是spring中的el表达式)。

SpEL的写法:${表达式}

用于改变作用范围的注解

@Scope

作用:用于指定bean的作用范围
属性:

  • value:指定范围的取值。常用取值:singleton、prototype

示例:
AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

//账户的业务层实现类
@Service(value = "accountService")
@Scope(value = "prototype")
public class AccountServiceImpl implements AccountService {

//    @Autowired
//    @Qualifier(value = "accountDao2")
    @Resource(name = "accountDao2")
    private AccountDao accountDao;

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

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

和生命周期相关的注解

@PreDestroy

作用:用于指定销毁方法。

@PostConstrust

作用:用于指定初始化方法。

示例:
AccountServiceImpl类:

package com.qublog.service.impl;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

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

//账户的业务层实现类
@Service(value = "accountService")
//@Scope(value = "prototype")
public class AccountServiceImpl implements AccountService {

//    @Autowired
//    @Qualifier(value = "accountDao2")
    @Resource(name = "accountDao2")
    private AccountDao accountDao;

    @PostConstruct
    public void init() {
        System.out.println("init");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("destroy");
    }

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

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

Client类:

package com.qublog.ui;

import com.qublog.dao.AccountDao;
import com.qublog.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//模拟一个表现层,用于调用业务层
public class Client {
    //获取spring的IoC核心容器,并根据id获取对象
    public static void main(String[] args) {
        //获取核心容器对象
//        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取bean对象,下面两种方式都可以
        AccountService as = (AccountService)ac.getBean("accountService");
//        AccountService as2 = (AccountService)ac.getBean("accountService");
//        System.out.println(as);
//
//        AccountDao ad = (AccountDao)ac.getBean("accountDao");
//        System.out.println(ad);
//        System.out.println(as == as2);
        as.saveAccount();
        ac.close();
    }
}

注意:spring对多例对象不负责销毁

发布了56 篇原创文章 · 获赞 0 · 访问量 526

猜你喜欢

转载自blog.csdn.net/qq_41242680/article/details/105626493