zoukankan      html  css  js  c++  java
  • 8. Spring 注解开发(原始注解)

    Spring原始注解

    Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置 文件可以简化配置,提高开发效率。

    Spring原始注解主要是替代的配置:



    注解

    说明

       @Component 【组件】

    使用在类上用于实例化Bean

       @Controller 【控制】

    使用在web层类上用于实例化Bean

       @Service  【服务】

    使用在service层类上用于实例化Bean

       @Repository 【库Dao】

    使用在dao层类上用于实例化Bean

     @Autowired 【自动装配】  

    使用在字段上用于根据类型依赖注入

       @Qualifier

    【配合autowired根据名称注入】

    结合@Autowired一起使用用于根据名称进行依赖注入

       @Resource 【根据名称(bean-Id)自动注入】

    相当于@Autowired+@Qualifier,按照名称进行注入

       @Value

    注入普通属性

       @Scope

    标注Bean的作用范围

       @PostConstruct

    使用在方法上标注该方法是Bean的初始化方法

       @PreDestroy

    使用在方法上标注该方法是Bean的销毁方法



    用注解开发:

    先来个普通的DaoImpl 注入到 ServiceImpl 的原始代码,然后我们吧Spring的配合文件拆分,分别对应注解来写:

    注意: 使用注解进行开发时,需要在Spring配置文件中配置组件扫描作用是指定哪个包及其子包下的Bean 需要进行扫描以便识别使用注解配置的类、字段和方法。【简单点说就是扫描指定包下的所有文件 扫描下看有没有注解 有的话就自动解析配入 】:

    在Spring配置文件中写:

    <!--注解的组件扫描-->
    <context:component-scan base-package="com.包名"></context:componentscan>

    这三句话 很重要:【多种注解配合注入Bena / 参数

    使用@Compont或@Repository标识DaoImpl需要Spring进行实例化。

    使用@Compont或@Service标识ServiceImpl需要Spring进行实例化 

    使用@Autowired或者@Autowired+@Qulifier或者@Resource进行Dao[ Impl 中的 Dao (参数) ]的注入

    代码如下:【把Dao的show方法 注入到 Service的方法中 调用 Service的Dao 然后带动 调用Dao的show方法】

    TestDao.java 和 TestService.java  :

    package com.bihu.TestDao;
    
    public interface Dao{
        public void show();
    }
    package com.bihu.TestService;
    
    public interface Service {
        public void show();
    }

    TestDaoImpl:

    package com.bihu.TestDaoImpl;
    
    import com.bihu.TestDao.Dao;
    import org.springframework.stereotype.Repository;
    
    //<bean id="DaoImpl" class="com.bihu.TestDaoImpl.DaoImpl"></bean>
    @Repository("DaoImpl")  //在DaoImpl中写入Bean实例化注解  参数DaoImpl相当于配置了配置文件中的Bean 的 ID
    public class DaoImpl implements Dao {
        @Override
        public void show() {
            System.out.println("我是DaoImpl 中的 show 方法");
        }
    }

    TestServiceImpl:

    package com.bihu.TestServiceImpl;
    
    import com.bihu.TestDao.Dao;
    import com.bihu.TestService.Service;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    import javax.annotation.Resource;
    
    //<bean id="ServiceImpl" class="com.bihu.TestServiceImpl.ServiceImpl">
    @org.springframework.stereotype.Service("ServiceImpl")   //由于名字冲突 其实 是@Service()    这个参数相当于也是配置了一个配置文件中的Bean的ID
    public class ServiceImpl implements Service {
        private Dao dao;
    ///////////////////////////////////////////////////////////////////////////
    // Spring配置:   <property name="dao" ref="DaoImpl"></property>
    
        /*  // 注解: 也可以这样:
        @Autowired  //使用在字段上用于根据类型自动依赖注入
    @Qualifier("DaoImpl")   //结合@Autowired一起使用用于根据名称进行依赖注入
    */
        @Resource(name = "DaoImpl") //相当于@Autowired+@Qualifier,按照名称进行注入
        public void setDao(Dao dao) {
            this.dao = dao;
        }
    ////////////////////////////////////////////////////////////////////////////////////
    
    
        @Override
        public void show() {
            this.dao.show();
        }
    
    }

    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扫描com.bihu下全部的注解 然后解析 -->
            <context:component-scan base-package="com.bihu"></context:component-scan>
    
    </beans>

    Demo.java : 【测试文件】

    import com.bihu.TestServiceImpl.ServiceImpl;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Demo {
        @Test
        public void test(){
            //配置文件名叫 app
            ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
            ServiceImpl service = (ServiceImpl) app.getBean("ServiceImpl");
            service.show();
        }
    }

    输出:

    信息: Loading XML bean definitions from class path resource [app.xml]
    我是DaoImpl 中的 show 方法

    所以呢 这就是用 最原始的注解去进行 实例化Bean 和 注入参数

    注意!!!! 有些注解(例如实例化Bean的注解)的参数相 其实相当于配置了配置文件中的一个ID,而有些注解(例如 注入参数)的参数,相当于读取id。  【这点一定得懂!】

    还有就是尽量可以用一个的注解就用一个 ,贪图方便是我们程序员✖!!!  是我最爱的!

     

     注意! 这里并没有讲Web层的注解 因为被Test测试代替了!!! 实际的

    @Controller

     用在SpringMVC的控制层上!

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/14922084.html

  • 相关阅读:
    db2 SQL3055N 报错分析解决
    db2之load报错SQL3107W解决
    db2报错之ERRORCODE=-4220 SQLSTATE=NULL
    mybatis-generator自动生成工具配置
    db2 表空间backup pending
    DB2
    Redis
    Linux安装Redis
    vue2.0学习(五)-Vue-router
    JavaScript基础-数据类型
  • 原文地址:https://www.cnblogs.com/bi-hu/p/14922084.html
Copyright © 2011-2022 走看看