zoukankan      html  css  js  c++  java
  • Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明

    课程链接:

    1    解析

    1.1  疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗?

    1.2  @resource注解版本支持

    1.3  没有显式指定@resource的那么,默认名称从何获得?

    1.4  @postConstruct 和@predestroy注解版本 支持

    1.5  @named 和 @inject 依赖注入注解的 版本支持

    1.6  @named解惑

    2    代码演练

    2.1  给变量赋值(方法一)

    2.2  给变量赋值(方法二)

    2.3  注解PostConstruct 和注解 PreDestroy(前置注解和后置注解)

    2.4  注解@named和注解@inject的使用

    2.5  注解@named

    1    解析

    1.1  疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗?

    1.2  @resource注解版本支持

    支持版本 JSR-250  java567都支持

    1.3  没有显式指定@resource的那么,默认名称从何获得?

    属性名或者方法得出,详细参考2.1

    1.4  @postConstruct 和@predestroy注解版本 支持

    Spring2.5之后支持

    1.5  @named 和 @inject 依赖注入注解的 版本支持

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

    1.6  @named解惑

    你首先要理解@Named注解是干嘛的?我们使用这个注解是来给我们注入对象的,那么@Named是JSR330中的注解,在这里它相当于@Qualifier,是用来注入在IOC容器已经存在的名称为jsrDao的bean,然而在IOC容器中并没有注册你说的jsr,自然就获取不到将会报错!

    2    代码演练

    2.1  给变量赋值

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class JsrService {
        
        @Resource
        private JsrDao jsrDao;
    
    //    public void setJsrDao(JsrDao jsrDao) {
    //        this.jsrDao = jsrDao;
    //    }
        
        public void save(){
            jsrDao.save();
        }
    }

    测试类:

    package com.imooc.test.beanannotation;
    
    import org.junit.Test;
    
    import com.imooc.beanannotation.javabased.JsrService;
    import com.imooc.test.base.UnitTestBase;
    
    public class TestJsr extends UnitTestBase{
        
        public TestJsr(){
            super("classpath*:spring-beanannotation.xml");
        }
        
        @Test
        public void testJsr(){
            JsrService jsrService =super.getbean("jsrService");
            jsrService.save();
        }
    }

    dao类:

    package com.imooc.beanannotation.javabased;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class JsrDao {
        public void save(){
            System.out.println("JsrDao is saving!");
        }
    }

    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="com.imooc.beanannotation"></context:component-scan>
    
    </beans>

    打印日志:

    四月 07, 2019 8:03:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5fa34e31: startup date [Sun Apr 07 20:03:26 CST 2019]; root of context hierarchy
    四月 07, 2019 8:03:27 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
    四月 07, 2019 8:03:27 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [config.xml]
    JsrDao is saving!
    四月 07, 2019 8:03:28 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5fa34e31: startup date [Sun Apr 07 20:03:26 CST 2019]; root of context hierarchy

    2.2  给变量赋值(方法二)

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class JsrService {
        
    //    @Resource
        private JsrDao jsrDao;
    
        @Resource
        public void setJsrDao(JsrDao jsrDao) {
            this.jsrDao = jsrDao;
        }
        
        public void save(){
            jsrDao.save();
        }
    }

    2.3  注解PostConstruct 和注解 PreDestroy(前置注解和后置注解)

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class JsrService {
        
    //    @Resource
        private JsrDao jsrDao;
    
        @Resource
        public void setJsrDao(JsrDao jsrDao) {
            this.jsrDao = jsrDao;
        }
        
        @PostConstruct
        public void init(){
            System.out.println("init method");
        }
        
        @PreDestroy
        public void destroy(){
            System.out.println("destroy method");
        }
        
        public void save(){
            jsrDao.save();
        }
    }

    打印日志:

    四月 07, 2019 8:25:30 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c27a30c: startup date [Sun Apr 07 20:25:30 CST 2019]; root of context hierarchy
    四月 07, 2019 8:25:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
    四月 07, 2019 8:25:31 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [config.xml]
    init method
    JsrDao is saving!
    四月 07, 2019 8:25:31 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c27a30c: startup date [Sun Apr 07 20:25:30 CST 2019]; root of context hierarchy
    destroy method

    2.4  注解@named和注解@inject的使用

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.inject.Inject;
    import javax.inject.Named;
    
    
    //@Service
    @Named
    public class JsrService {
        
    //    @Resource
        @Inject
        private JsrDao jsrDao;
    
    //    @Resource
        public void setJsrDao(JsrDao jsrDao) {
            this.jsrDao = jsrDao;
        }
        
        @PostConstruct
        public void init(){
            System.out.println("init method");
        }
        
        @PreDestroy
        public void destroy(){
            System.out.println("destroy method");
        }
        
        public void save(){
            jsrDao.save();
        }
    }

    pom.xml

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

    2.5  注解@named

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.inject.Inject;
    import javax.inject.Named;
    
    
    //@Service
    @Named
    public class JsrService {
        
    //    @Resource
    //    @Inject
        private JsrDao jsrDao;
    
    //    @Resource
        @Inject
        public void setJsrDao(@Named("jsrDao")JsrDao jsrDao) {
            this.jsrDao = jsrDao;
        }
        
        @PostConstruct
        public void init(){
            System.out.println("init method");
        }
        
        @PreDestroy
        public void destroy(){
            System.out.println("destroy method");
        }
        
        public void save(){
            jsrDao.save();
        }
    }
  • 相关阅读:
    冒泡排序
    python isinstance 函数
    2.x与3.x差异、条件语句、数据类型、其他
    区间结构
    配置文件监听器
    Enumerable扩展方法
    EsayUI + MVC + ADO.NET(项目架构)
    vue.js 官网及组件网站记录
    vue.js 二维码生成组件
    分享一些学习资料-大量PDF电子书
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10666796.html
Copyright © 2011-2022 走看看