• Java 5.Spring的依赖注入


    一、set方法注入

    value :值类型的注入
    ref   :对象类型的注入

    pojo

    public class Person {
    	private String name;
    	private Integer age;
    	private Car car;
    	public Person() {
    		super();
    		System.out.println("构造方法调用");
    	}
    	public void init() {
    		System.out.println("Person被初始化了。。。");
    	}
    	public void destroy() {
    		System.out.println("Person被摧毁了。。。");
    	}

    bean配置

            <bean name="car" class="com.Spring.pojo.Car">
            <property name="name" value="BWM"></property>
            <property name="color" value="red"></property>
            
            </bean>
            
            <bean name= "person1" class="com.Spring.pojo.Person">
            <property name="name" value="helen"></property>
            <property name="age" value="18"></property>
            <property name="car" ref="car"></property>
            </bean>
    

    二、构造方法注入

    1.创建Person的有参构造方法

    	public Person(String name, Car car) {
    		super();
    		System.out.println("调用了构造方法Person(String name, Car car)");
    		this.name = name;
    		this.car = car;
    	}
    

    配置bean

            <bean name="person2" class="com.Spring.pojo.Person">
            <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg>
            <constructor-arg name="car" ref="car" index="0"></constructor-arg>
            </bean>
    

    测试

    	@Test
    	@SuppressWarnings("resource")
    	public void testConstructer() {  
    		
    		AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml");		
    		Person person2=(Person) context.getBean("person2");
    		System.out.println(person2);
    	}
    

    2.使用index指定参数的位置

    pojo中有如下两个构造方法

    	public Person(String name, Car car) {
    		super();
    		System.out.println("调用了构造方法Person(String name, Car car)");
    		this.name = name;
    		this.car = car;
    	}
    	
    	public Person(Car car,Integer name) {
    		super();
    		System.out.println("调用了构造方法Person( Car car, int name)");
    		this.name = name+"";
    		this.car = car;
    	}

    配置bean

            <bean name="person2" class="com.Spring.pojo.Person">
            <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg>
            <constructor-arg name="car" ref="car" index="0"></constructor-arg>
            </bean>
    

    3.使用type指定参数的类型

    	public Person(String name, Car car) {
    		super();
    		System.out.println("调用了构造方法Person(String name, Car car)");
    		this.name = name;
    		this.car = car;
    	}
    	
    	public Person(Car car,Integer name) {
    		super();
    		System.out.println("调用了构造方法Person( Car car, int name)");
    		this.name = name+"";
    		this.car = car;
    	}
    

    bean配置

            <bean name="person2" class="com.Spring.pojo.Person">
            <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg>
            <constructor-arg name="car" ref="car" index="0"></constructor-arg>
            </bean>
    

    三、p名称空间注入

    导入名称空间

        xmlns:p="http://www.springframework.org/schema/p"
    

     使用p:属性注入:

    p:属性名          表示值类型

    p:属性名-ref       表示应用类型

            <bean name="person3" class="com.Spring.pojo.Person" 
            p:name="helen" p:age="18" p:car-ref="car">
            </bean> 
    

    典型案例:配置数据源

    四、spl注入

           	<bean name="person4" class="com.Spring.pojo.Car">
            <property name="name" value="#{person1.name}"></property>
        	<property name="age" value="#{person1.age}"></property>
    		<property name="car" ref="car"></property>
            </bean>
    

    测试

    	@Test
    	@SuppressWarnings("resource")
    	public void testSpel() {  
    		
    		AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml");		
    		Person person4=(Person) context.getBean("person4");
    		System.out.println(person4);
    	}
    
  • 相关阅读:
    A “word-wrap” functionality(一个字符串包裹函数)
    First Unique Character in a String 的变种问题返回第一个找到符合条件的字符
    北美一工作搜索引擎公司技术岗面经
    一房地产数据服务初创公司的面经
    Prime numbers from 1 to 100 (打印 100 以内的素数)
    dubbo面试题(1)
    maven工具日常开发常用命令
    BaseMapper和继承
    《计算机是怎样跑起来的》读书笔记(2)
    AEAP工作总结模板套路
  • 原文地址:https://www.cnblogs.com/yuzhenfu/p/12150560.html
Copyright © 2020-2023  润新知