zoukankan      html  css  js  c++  java
  • Spring框架 基础01

    属性注入

    基础注解自动扫描注入

    <!-- 扫描上下文对象中 所有带component等 注解的类 -->
                            <!-- 指定一个包 -->
    <context:component-scan base-package="com.ljk.model" />
    
    <bean id="dog" class="com.ljk.model.Dog">
        <!-- 变量名 属性注入 -->
        <!-- <property name="name" value="小明" /> -->
        
        <!-- 构造方法 属性注入 -->
        <!-- index表明构造函数里的第几个值 -->
        <constructor-arg index="0" value="小花" />
        <constructor-arg index="1" value="10" />
        <constructor-arg index="2" value="猫狗" />
        <!-- <constructor-arg index="3" ref="date"></constructor-arg> -->
        
        <!-- 根据构造方法中的名字来 进行属性注入 -->
        <!-- ref 引用底下的 时间类 -->
        <constructor-arg name="birthday" ref="date" />
        <!-- 注入一个集合 -->
        <constructor-arg name="list">
            <list>
                <value>头</value>
                <value>尾巴</value>
                <value>手</value>
                <value>身体</value>
                <value>脚</value>
            </list>
            
            <!-- map 集合
            <map>
                <entry key="" value=""/>
            </map> 
            -->
        </constructor-arg>
        
    </bean>
    
    <!-- 实例化一个Date类 获取现在时间 -->
    <bean id="date" class="java.util.Date" />
    

    自动扫描到Spring容器中

    @Component // 扫描入spring容器中
    public class Cat {
    	private String name;
    	private int age;
    	private String species; // 种类 物种
    	private Date birthday;
    	private List<String> list;
    	
    	
    	public Cat() {
    		super();
    	}
    }
    

     把扫描到的类 自动注入带有@Autowired注解的变量中去(自动判断类型)

    @Component // 自动扫描
    public class CatZoo {
    	private String name;
    
    	@Autowired // 自动注入
    	private List<Cat> list;
    
    	private String op; // 管理
    	public CatZoo() {
    		super();
    	}
    }
    
  • 相关阅读:
    性能计数器
    SpringBoot_数据访问-整合JPA
    SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis
    SpringBoot_数据访问-整合Druid&配置数据源监控
    SpringBoot_数据访问-JDBC&自动配置原理
    八字克妻口诀
    没想到,我能这么细心地看算法
    辰戌丑未,四库
    The jar of divisors
    分布式系统阅读笔记(十六)-----事务和并发控制
  • 原文地址:https://www.cnblogs.com/bkyljk/p/8384349.html
Copyright © 2011-2022 走看看