码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-04-07 20:34:09      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:1.0   res   system   star   ons   xmla   extends   void   tco   

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.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

 

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

标签:1.0   res   system   star   ons   xmla   extends   void   tco   

原文地址:https://www.cnblogs.com/1446358788-qq/p/10666796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!