• spring(3)


    package com.cn.pojo;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Book {
    
    }
    package com.cn.dao;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Repository;
    
    @Repository
    @Scope("prototype")
    public class BookDao {
    
    }
    package com.cn.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class BookExt extends BookDao{
    
    }
    package com.cn.controller;
    
    import org.springframework.stereotype.Controller;
    
    @Controller
    public class BookController {
    
    }
    package com.cn.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    
    import com.cn.dao.BookDao;
    
    @Service
    public class BookService {
        
    /**
         * @Autowired 实现自动注入<br/>
         *     1、先按类型查找并注入<br/>
         *  2、如果找到多个,就接着按属性名做为id继续查找并注入<br/>
         *  3、如果找到多个。但是属性名做为id找不到,可以使用@Qualifier("bookDao")注解指定id查找并注入<br/>
         *  4、可以通过修改@Autowired(required=false)允许字段值为null
         */
        @Autowired(required=false)
        @Qualifier("bookDaoExt")
        private BookDao bookDao1;
        
        /**
         * @Autowired标注在方法上,那么此方法会在对象创建之后调用。
         *         1、先按类型查找参数并调用方法传递<br/>
         *         2、如果找到多个,就接着按参数名做为id继续查找并注入<br/>
         *        3、如果找到多个。但是参数名做为id找不到,可以使用@Qualifier("bookDao")注解指定id查找并调用<br/>
         *      4、可以通过修改@Autowired(required=false)允许不调用此方法也不报错
         */
        @Autowired(required=false)
        public void setBookDao(@Qualifier("bookExt")BookDao abc) {
            System.out.println("BookDao进来啦 --->>> " + abc);
        }
        
    
        @Override
        public String toString() {
            return "BookService [bookDao=" + bookDao1 + "]";
        }
    }
    package com.cn.test;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Configurable;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        @Test
        public void test1() throws Exception {
    
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            /*
             * System.out.println(applicationContext.getBean("bookDao"));
             * System.out.println(applicationContext.getBean("bookDao"));
             * System.out.println(applicationContext.getBean("bookDao"));
             * 
             * 
             * System.out.println(applicationContext.getBean("book"));
             */
            System.out.println(applicationContext.getBean("bookService"));
    
        }
    
    }
    <?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-4.3.xsd">
    
    
        <!-- 配置包扫描 base-package 设置需要扫描的包名(它的子包也会被扫描) use-default-filters="false" 
            去掉包扫描时默认包含规则 -->
        <context:component-scan base-package="com.cn"   >
    
        <!--      排除 按照注解
            <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Service" />
            排除 按类排除
    
            <context:exclude-filter type="assignable"
                expression="com.cn.controller.BookController" />
     -->
    
    
    <!-- 包含 按照注解 -->
            <!-- <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Service" />
     -->
    
    <!-- 包含按类排除
     -->
            <!-- <context:include-filter type="assignable"
                expression="com.cn.controller.BookController" />
     -->
        </context:component-scan>
    </beans>
  • 相关阅读:
    自定义WordPress文件上传路径
    PHP Warning: preg_match(): JIT compilation failed: no more memory in
    Mac下PHP7.1+Nginx安装和配置
    bootstrap modal插件弹出窗口如何限制最大高度,并且在内容过多时可以滚动显示
    Language Tool ,a plugin for TeXStudio
    平均值mean,众数mode,中值median 和 标准差stddev
    LaTeX Software & Manuals
    MAFFT多重序列比对--(附比对彩标方法)
    Markdown语法 (中文版)
    在64位系统上不能安装Matlab notebook的解决方案
  • 原文地址:https://www.cnblogs.com/ywqtro/p/12263398.html
Copyright © 2020-2023  润新知