Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明

Posted 扈江离与辟芷兮,纫秋兰以为佩。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明相关的知识,希望对你有一定的参考价值。

1    解析

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

 

2    代码演练

2.1  给变量赋值(方法一)

2.2  给变量赋值(方法二)

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

 

 

 

 

 

 

1    解析

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

 

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[email protected]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[email protected]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[email protected]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[email protected]7c27a30c: startup date [Sun Apr 07 20:25:30 CST 2019]; root of context hierarchy
destroy method

 

以上是关于Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明的主要内容,如果未能解决你的问题,请参考以下文章

Spring入门篇——第1章 概述

Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期

Spring课程 Spring入门篇 3-1 Spring bean装配(上)之bean的配置项及作用域

Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互

慕课-《Spring入门篇》学习笔记 专题一 IOC

Spring Boot干货系列:优雅的入门篇