zoukankan      html  css  js  c++  java
  • Spring学习笔记(五)基于注解的Bean操作管理

    接下来来学习注解

    什么是注解

    • 注解是代码特殊标记,格式:@注解名称(属性名称=属性值)

    • 注解作用在类,方法,属性上

    • 使用注解目的:简化xml配置

    Spring针对Bean管理创建对象提供的注解种类

    • @Component

    • @Service

    • @Controller

    • @Repository
      注解的实现需要引入spring-aop-5.2.6.RELEASE

    实现步骤

    1. 开启组件扫描(引入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"
    >
    
        <!--
            开启组件扫描
               1 如果扫描多个包,多个包用逗号隔开
               2 写上需要扫描包的上层目录
                 扫描 com.day6 包下所有的类
        -->
        <context:component-scan base-package="com.day6"></context:component-scan>
    
    </beans>
    
    1. 创建UserService类和测试类
      UserService类
    @Service
    public class UserService {
        public void add(){
            System.out.println("service add...");
        }
    }
    

    测试类

    public class test {
        @Test
        public void test(){
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("com/day6/spring5/zhujie/bean.xml");
            // 这里getBean的name属性值,默认是需要创建对象的类的首字母小写
            UserService userService = context.getBean("userService", UserService.class);
            userService.add();
        }
    }
    
    //输出
    service add...
    

    3.开启组件扫描细节配置

    <!--示例1
            use-default-filters="false" 表示现在不适用默认filter,自己配置filter
            context:include-filter , 设置扫描哪些内容
    -->
    <context:component-scan base-package="com.day6" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!--示例2
            下面配置扫描包的所有内容
            context:exclude-filter , 设置哪些内容不扫描
    -->
    <context:component-scan base-package="com.day6">
        <context:exclude-filter type="annotation" 
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    

    基于注解方式实现属性注入

    用到四个注解:@Autowired @Qulifier @Resource @Value

    • @AutoWired:根据属性类型进行自动注入
      使用到的类: Service类、Dao类

    第一步:把Service和Dao对象创建 (在Service类上添加@Service,在Dao类上添加@Repository)

    第二步:在Service中注入Dao对象,在Service中添加dao类型属性,在属性上添加注解@Autowired

    //Service类
    public class Service{
      @Autowired
      private Dao dao;
      public void add(){
        System.out.println("service add ...");
        dao.add();
      }
    }
    
    //Dao类
    public class Dao{
      public void add(){
        System.out.println("dao add ...");
      }
    }
    
    

    xml配置文件

     <context:component-scan base-package="com.qy"></context:component-scan>
    

    测试类

    //测试类
    public class test {
        @Test
        public void test(){
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("com/qy/spring5/zhujie/bean.xml");
    
            // 这里getBean的name属性值,默认是需要创建对象的类的首字母小写
            UserService userService = context.getBean("userService", UserService.class);
            userService.add();
        }
    }
    
    //输出结果
    service add...
    dao add...
    

    2.@Qualifier:根据属性名称进行注入,需要和@Autowired配合使用
    它用于何处?
    比如说现在有 Service类,Dao接口,DaoImpl1实现类(实现Dao接口),DaoImpl2实现类(实现Dao接口),这时候我们再使用@Autowired根据类型进行注入的话,因为有两个实现类,那么就不知道要注入哪个实现类了,所以要使用@Qualifier注解。
    演示

    //UserService类
    @Service(value = "userService")
    public class UserService {
        @Autowired
        @Qualifier(value = "userDaoImpl1")
        private UserDao userDaoImpl1;
        @Autowired
        @Qualifier(value = "userDaoImpl2")
        private UserDao userDaoImpl2;
    
        public void add(){
            System.out.println("service add ...");
            userDaoImpl1.add();
            userDaoImpl2.add();
        }
    }
    
    //UserDao接口
    public interface UserDao {
        public void add();
    }
    
    //UserDaoImpl1实现类
    @Repository(value = "userDaoImpl1")
    public class UserDaoImpl1 implements UserDao{
        @Override
        public void add() {
            System.out.println("UserDaoImpl1 第一个实现类 add ...");
        }
    }
    
    //UserDaoImpl2实现类
    @Repository(value = "userDaoImpl2")
    public class UserDaoImpl2 implements UserDao{
        @Override
        public void add() {
            System.out.println("UserDaoImpl2 第二个实现类 add ...");
        }
    }
    
    //XML配置文件
    <context:component-scan base-package="com.qy"></context:component-scan>
    
    //测试类
    public class test {
        @Test
        public void test(){
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("com/qy/spring5/qualifier/bean.xml");
            // 这里getBean的name值默认是UserService首字母小写,可以在UserService上通过注解设置 @Service(value = "")
            UserService userService = context.getBean("userService", UserService.class);
            userService.add();
        }
    }
    
    //输出
    service add ...
    UserDaoImpl1 第一个实现类 add ...
    UserDaoImpl2 第二个实现类 add ...
    

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

    • 根据类型注入
    @Resource
    private UserDao userDaoImpl;
    
    • 根据名称注入
    @Resource(name = "userDaoImpl2")
    private UserDao userDaoImpl2;
    

    4.@Value:注入普通类型属性

    @Value(value = "abc")
    private String name;  // 将abc注入到属性name中
    

    完全注解开发

    1.创建配置类(添加@Configuration注解),替代xml配置文件

    @Configuration // 作为配置类,替代xml配置文件
    @ComponentScan(basePackages = {"com.day6"})
    public class SpringConfig {
    }
    

    2.测试类中也需要替换

    public class test {
        @Test
        public void test(){
            // 加载配置类
            AnnotationConfigApplicationContext context = 
                    new AnnotationConfigApplicationContext(SpringConfig.class);
            
            UserService userService = context.getBean("userService", UserService.class);
            userService.add();
        }
    }
    

    总结

    我个人认为注解是一种作用于类上的注入方式,很大程度上提升了程序员对类的管理,通过观察就可以知道类的主要信息,但是却难以整理,和xml方式相比,只能说各有千秋吧.

  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/Emuaer/p/15093311.html
Copyright © 2011-2022 走看看