• Spring课程 Spring入门篇 4-2 Spring bean装配(下)之Autowired注解说明1


    课程链接:

    1    解析

    2    代码演练

    1    解析

    1.1  @Required注解

    该注解适用于bean属性的set方法

    1.2  @Autowired

    作用:

    是为了把依赖的对象,自动的注入到bean里

    使用方式:

    1  @autowired required的默认值为false

    2  每个类中只有一个构造器被标记为required=true

    3  @autowired(required=false):如果找不到实体类,不报错。

    4  可以添加到成员变量或者方法上边

     

    2    代码演练

    2.1  autowired作为成员变量注解

    实体类:

    package com.imooc.beanannotation.injection.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.imooc.beanannotation.injection.dao.InjectionDao;
    
    
    @Service
    public class InjectionServiceImpl implements InjectionService {
        
        @Autowired
        private InjectionDao injectionDao;
    
        @Override
        public void save(String sArgs) {
            injectionDao.save(sArgs);
            // TODO Auto-generated method stub
            System.out.println("开始保存!!!"+sArgs);
        }
    
    }

    dao类:

    package com.imooc.beanannotation.injection.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class InjectionDaoImpl implements InjectionDao{
    
        @Override
        public void save(String args) {
            // TODO Auto-generated method stub
            System.out.println("保存======="+args);
        }
    
    }

    测试类:

    package com.imooc.beanannotation.injection2;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    
    import com.imooc.beanannotation.injection.service.InjectionService;
    import com.imooc.test.base.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestInjectionService extends UnitTestBase{
    
        public TestInjectionService() {
            super("classpath*:spring-beanannotation-injection.xml");
            // TODO Auto-generated constructor stub
        }
        
        @Test
        public void testInjectionServiceConstruct(){
            try {
                InjectionService iService = super.getbean("injectionServiceImpl");
                iService.save("12346");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
    }

    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>

    打印结果:

    三月 10, 2019 11:16:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@52633079: startup date [Sun Mar 10 11:16:29 CST 2019]; root of context hierarchy
    三月 10, 2019 11:16:29 上午 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-injection.xml]
    保存=======12346
    三月 10, 2019 11:16:30 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@52633079: startup date [Sun Mar 10 11:16:29 CST 2019]; root of context hierarchy
    开始保存!!!12346

    2.2  autowired作为set方法注解

    实体类:

    package com.imooc.beanannotation.injection.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.imooc.beanannotation.injection.dao.InjectionDao;
    
    
    @Service
    public class InjectionServiceImpl implements InjectionService {
        
        private InjectionDao injectionDao;
        
        @Autowired
        public void setInjectionDao(InjectionDao injectionDao) {
            this.injectionDao = injectionDao;
        }
    
    
        @Override
        public void save(String sArgs) {
            injectionDao.save(sArgs);
            // TODO Auto-generated method stub
            System.out.println("开始保存!!!"+sArgs);
        }
    
    }

    打印结果:

    三月 10, 2019 11:21:25 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:21:25 CST 2019]; root of context hierarchy
    三月 10, 2019 11:21:25 上午 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-injection.xml]
    保存=======12346
    开始保存!!!12346
    三月 10, 2019 11:21:26 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:21:25 CST 2019]; root of context hierarchy

    2.3  autowired作为构造方法注解

    实体类:

    package com.imooc.beanannotation.injection.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.imooc.beanannotation.injection.dao.InjectionDao;
    
    
    @Service
    public class InjectionServiceImpl implements InjectionService {
        
        private InjectionDao injectionDao;
        
        public void setInjectionDao(InjectionDao injectionDao) {
            this.injectionDao = injectionDao;
        }
    
        @Autowired
        public InjectionServiceImpl(InjectionDao injectionDao) {
            super();
            this.injectionDao = injectionDao;
        }
    
        @Override
        public void save(String sArgs) {
            injectionDao.save(sArgs);
            // TODO Auto-generated method stub
            System.out.println("开始保存!!!"+sArgs);
        }
    
    }

    打印结果:

    三月 10, 2019 11:24:33 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:24:33 CST 2019]; root of context hierarchy
    三月 10, 2019 11:24:34 上午 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-injection.xml]
    保存=======12346
    三月 10, 2019 11:24:34 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:24:33 CST 2019]; root of context hierarchy
    开始保存!!!12346
  • 相关阅读:
    WebSocket
    Spring Web MVC
    对象关系映射(ORM)数据访问
    使用JDBC进行数据访问
    【Mysql】【基础综合题解】大一下学期数据库综合训练题解
    【Mysql】【解决问题】[Err] 1055
    【JavaSE】【IO流】复制单级或多级文件夹
    C#使用FFmpeg的总结
    C#一个IM系统简单总结
    重新开启博客
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10504689.html
Copyright © 2020-2023  润新知