天天看点

【Spring IOC容器】Bean管理的操作方式之基于注解

文章目录

  • ​​Bean管理的操作方式之基于注解方式​​
  • ​​一、 注解概念​​
  • ​​二、 注解方式之创建对象​​
  • ​​@Component​​
  • ​​▶xml配置文件​​
  • ​​▶UserService类​​
  • ​​▶测试类​​
  • ​​@Service​​
  • ​​@Controller​​
  • ​​@Repository​​
  • ​​三、 开启组件扫描细节处理​​
  • ​​Ⅰ.配置context空间​​
  • ​​Ⅱ.开启组件自动扫描​​
  • ​​Ⅲ.开启组件自定义扫描(include)​​
  • ​​Ⅳ.开启组件自定义扫描(exclude)​​
  • ​​四、注解方式之注入属性​​
  • ​​@Autowired:根据属性类型进行自动装配​​
  • ​​@Qualifier:根据名称进行注入​​
  • ​​@Resource:可以根据类型注入,可以根据名称注入​​
  • ​​@Value:注入普通类型属性​​
  • ​​五、完全注解开发​​
  • ​​Ⅰ.创建配置类,替代 xml 配置文件​​
  • ​​Ⅱ.编写流程类​​
  • ​​Ⅲ.编写测试类​​

Bean管理的操作方式之基于注解方式

一、 注解概念

  • 1.定义:注解是指代码特殊标记。
  • 2.格式:​

    ​@注解名称(属性名称=属性值,属性名称=属性值...)​

  • 3.使用范围:​

    ​类上面​

    ​、​

    ​方法上面​

    ​、​

    ​属性上面​

  • 4.使用目的:​

    ​简化xml配置​

二、 注解方式之创建对象

  • 第一步:引入依赖
  • 【Spring IOC容器】Bean管理的操作方式之基于注解
  • 第二步:开启组件扫描
通过配置组件扫描,可以使得Spring自动扫描指定的包(package),而不必在Spring的配置文件中逐一声明各个​

​<bean>​

  • 如果扫描多个包:

    1.包之间使用逗号隔开:​

    ​base-package="Bean管理_注解方式.a,Bean管理_注解方式.b"​

    ​ 2.扫描包的上层目录:​

    ​base-package="Bean管理_注解方式"​

  • 【Spring IOC容器】Bean管理的操作方式之基于注解
<?xml version="1.0" encoding="UTF-8"?>
<!--  1.配置context名称空间  -->
<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">

    <!--  2.开启组件扫描  -->
    <context:component-scan base-package="Bean管理_注解方式.a,Bean管理_注解方式.b"></context:component-scan>
</beans>      

@Component

▶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">

    <!--  开启组件扫描  -->
    <context:component-scan base-package="Bean管理_注解方式"></context:component-scan>
</beans>      

▶UserService类

package Bean管理_注解方式.b;

import org.springframework.stereotype.Component;

// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Component(value = "userService")
public class UserService {

    public void add(){
        System.out.println("UserService add .................");
    }

}      

▶测试类

public class Test {
    @org.junit.Test
    public void test_userService(){
        try {
            ApplicationContext context
                    = new ClassPathXmlApplicationContext("Bean管理_注解方式/bean_注解.xml");
            UserService userService = context.getBean("userService", UserService.class);
            System.out.println(userService);
            userService.add();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}      
【Spring IOC容器】Bean管理的操作方式之基于注解

​​返回顶部​​

@Service

UserService类

package Bean管理_注解方式.b;

import org.springframework.stereotype.Component;

// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Service(value = "userService")
public class UserService {

    public void add(){
        System.out.println("UserService add .................");
    }

}      
【Spring IOC容器】Bean管理的操作方式之基于注解

​​返回顶部​​

@Controller

UserService类

package Bean管理_注解方式.b;

import org.springframework.stereotype.Component;

// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Controller(value = "userService")
public class UserService {

    public void add(){
        System.out.println("UserService add .................");
    }

}      
【Spring IOC容器】Bean管理的操作方式之基于注解

​​返回顶部​​

@Repository

UserService类

package Bean管理_注解方式.b;

import org.springframework.stereotype.Component;

// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Repository(value = "userService")
public class UserService {

    public void add(){
        System.out.println("UserService add .................");
    }

}      
【Spring IOC容器】Bean管理的操作方式之基于注解

Spring 针对 Bean 管理中创建对象提供注解:

(1)​

​@Component​

​ (2)​

​@Service​

(3)​

​@Controller​

(4)​

​@Repository​

由测试结果可知,上面四个注解功能是一样的,都可以用来创建 bean 实例。

​​返回顶部​​

三、 开启组件扫描细节处理

Ⅰ.配置context空间

<?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">      

Ⅱ.开启组件自动扫描

  • 使用默认的filter,自动扫描base-package下的所有子包及其类
<!--  开启组件扫描  -->
    <!--  使用默认的filter,自动扫描base-package下的所有子包及其类  -->
    <context:component-scan base-package="Bean管理_注解方式"></context:component-scan>      

Ⅲ.开启组件自定义扫描(include)

  • ​use-default-filters="false"​

    ​ 表示不使用默认的filter,使用自定义的filter
  • ​ontext:include-filter​

    ​ 表示扫描包含哪些内容
  • ​expression="org.springframework.stereotype.Controller"​

    ​ 表示扫描组件的时候,仅扫描包中用Controller注解方式的类。
<!--  use-default-filters="false" 表示不使用默认的filter,使用自定义的filter  -->
    <context:component-scan base-package="Bean管理_注解方式" use-default-filters="false">
        <!--  context:include-filter 表示扫描包含哪些内容    -->
        <!--  type=“annotation” 表示注解方式    -->
        <!--  expression="org.springframework.stereotype.Controller 表示扫描含有Controller注解方式的类    -->
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>      

Ⅳ.开启组件自定义扫描(exclude)

  • ​context:exclude-filter​

    ​ 表示扫描不包含哪些内容
<!--  use-default-filters="false" 表示不使用默认的filter,使用自定义的filter  -->
    <context:component-scan base-package="Bean管理_注解方式" use-default-filters="false">
        <!--  context:exclude-filter 表示扫描不包含哪些内容    -->
        <!--  type=“annotation” 表示注解方式    -->
        <!--  expression="org.springframework.stereotype.Controller 表示扫描除了含有Controller注解方式的类    -->
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>      

​​返回顶部​​

四、注解方式之注入属性

import org.springframework.beans.factory.annotation.​

​Autowired​

​;

import org.springframework.beans.factory.annotation.​

​Qualifier​

​;

import org.springframework.beans.factory.annotation.​

​Value​

​;

import ​

​javax.annotation.Resource​

​;

注意:在以上的属性注解中Resource是唯一Spring框架外部的包。

@Autowired:根据属性类型进行自动装配

  • 在这里我们定义UserDao接口及其实现类UserDaoImpl,我们要在UserService类中添加UserDao对象属性,并在其add()方法中去调用UserDao的add()方法。
  • 第一步 把 service 和 dao 对象创建,​

    ​在 service 和 dao 类添加创建对象注解​

    ​。
  • 第二步 在 service 注入 dao 对象,​

    ​在 service 类添加 dao 类型属性,在属性上面使用注解@Autowired​

    ​。
public interface UserDao {
    public void add();
}

import org.springframework.stereotype.Repository;

@Repository //  1.添加注解,创建对象
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("UserDaoImpl add ............");
    }
}      
import Bean管理_注解方式.a.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 1.注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
//   注解里value的值可以不写,默认值就是类名称首字母小写
@Component(value = "userService")
public class UserService {

    // 2.定义对象属性并添加上注解
    @Autowired
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add .................");
        userDao.add();
    }

}      
public class Test {
    @org.junit.Test
    public void test_userService(){
        try {
            ApplicationContext context
                    = new ClassPathXmlApplicationContext("Bean管理_注解方式/bean_注解.xml");
            UserService userService = context.getBean("userService", UserService.class);
            System.out.println(userService);
            userService.add();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}      
【Spring IOC容器】Bean管理的操作方式之基于注解

​​返回顶部​​

@Qualifier:根据名称进行注入

  • @Qualifier 注解的使用,和上面@Autowired 一起使用。
  • 根据名称进行注入,是为了防止一个接口有多个实现类,根据类型自动注入的时候无法判定是哪一个具体的实现类。这样我们可以通过在创建类对象的时候对对象注释的value值进行设置,然后通过​

    ​value值​

    ​进行判断注入属性。
import org.springframework.stereotype.Repository;

@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("UserDaoImpl add ............");
    }
}

// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Component(value = "userService")
public class UserService {

    // 定义对象属性并添加上注解
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add .................");
        userDao.add();
    }

}      

​​返回顶部​​

@Resource:可以根据类型注入,可以根据名称注入

  • 只需要更改属性注入的类型为​

    ​@Resource​

    ​,默认就是根据类型注入,相当于​

    ​@AutoWired​

  • 添加属性​

    ​name=" "​

    ​,就是根据名称注入,这里的name值就相当于value值。
@Resource  // 1.根据类型注入
@Resource(name = "UserDaoImpl1")  // 2.根据名称注入
private UserDao userDao;      

​​返回顶部​​

@Value:注入普通类型属性

  • 上面的几种属性的注入方式都是针对对象类型的属性而言的,而​

    ​@Value是针对于普通类型的属性,如:String、int等​

    ​。
// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Component(value = "userService")
public class UserService {

    // 普通属性注入
    @Value(value = "zyx")
    private String name;
    // 定义对象属性并添加上注解
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

//    @Resource(name = "UserDaoImpl1")
//    private UserDao userDao;

    public void add(){
        System.out.println("UserService add ................."+name);
        userDao.add();
    }

}      
【Spring IOC容器】Bean管理的操作方式之基于注解
以上就是基本的注解方式进行Bean管理的操作,整个过程简化了xml文件配置,只使用到了一次配置组件扫描!

​​返回顶部​​

五、完全注解开发

在开发的过程中,为了简化xml文件配置,我们还可以做到完全注解开发。就是不需要配置任何的xml文件,纯使用注解的方式进行。

Ⅰ.创建配置类,替代 xml 配置文件

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

@Configuration // 添加注解作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"Bean管理_注解方式"}) // 配置组件扫描
public class SpringConfig {
    
}      

Ⅱ.编写流程类

UserDao接口:
public interface UserDao {
    public void add();
}

UserDaoImpl实现类:
import org.springframework.stereotype.Repository;

@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("UserDaoImpl add ............");
    }
}

UserService类:
import Bean管理_注解方式.a.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

// 注解方式创建对象 ---- 等共同于<bean id="" class=""></bean>
// 注解里value的值可以不写,默认值就是类名称首字母小写
@Component(value = "userService")
public class UserService {

    // 普通属性注入
    @Value(value = "zyx")
    private String name;
    // 定义对象属性并添加上注解
    @Autowired
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;

    public void add(){
        System.out.println("UserService add ................."+name);
        userDao.add();
    }

}      

Ⅲ.编写测试类

  • 由于更改了完全注解方式开发,所以原先的配置注解xml配置文件的方式就不行了;现在,使用​

    ​AnnotationConfigApplicationContext(SpringConfig.class)​

    ​的方式来进行加载注解配置类,实现与xml配置文件相同的效果。
@org.junit.Test
public void test_userService() {
    try {
        // 加载注解配置类
        ApplicationContext context
                = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    } catch (Exception e) {
        e.printStackTrace();
    }
}