Spring学习(3)---Spring的依赖注入(set方法注入、构造函数注入和P名称空间注入)

1、set方法注入

  • 创建一个Person类
package com.springdemo;

public class Person {
	private int id;
	private String name; 
	
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + "]";
	}	
}
  • applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 
        	id:给Person类指定一个person的对象。
        	   一个bean可以有多个id值,但是id的值不能重复,id的值具有唯一性。
        	   
        	class:Perosn类的全路径名
         -->
        <bean id="person" class="com.springdemo.Person">
        <!-- 
        	name:需要赋值的属性名
        	value:需要给属性赋的值
         -->
        	<property name="id" value="001"></property>
        	<property name="name" value="张三"></property>
        </bean>
        
 </beans>
  • 创建一个MyTest测试类
package com.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springdemo.Person;

public class MyTest {
	@Test
	public void test(){
	
		//获取spring上下文对象
		//ClassPathXmlApplicationContext从类路径下加载配置文件
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p=(Person) context.getBean("person");
		System.out.println(p);
	}
}

运行结果为:

2、构造函数注入

  • 创建一个Person类
package com.springdemo;

public class Person {
	private int id;
	private String name; 
	
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Person(int id, String name) {     //创建构造函数
		super();
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + "]";
	}	
	
}
  • applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 
        	id:给Person类指定一个person的对象。
        	   一个bean可以有多个id值,但是id的值不能重复,id的值具有唯一性。
        	   
        	class:Perosn类的全路径名
         -->
        <bean id="person" class="com.springdemo.Person">
        <!-- 
        	给构造函数赋值就用constructor-arg标签进行赋值
        	value:需要给属性赋的值
        	以下方法是按照构造方法里public Person(int id, String name)的属性顺序进行赋值的
         -->
		<constructor-arg value="1111"></constructor-arg>
		<constructor-arg value="张三"></constructor-arg>
        </bean>
                <bean id="person2" class="com.springdemo.Person">
        <!-- 
        	如果想给value指定顺序赋值,可以使用index进行指定
         -->
		<constructor-arg value="李四" index="1"></constructor-arg>
		<constructor-arg value="2222" index="0"></constructor-arg>
        </bean>
        
 </beans>
  • 创建一个MyTest类
package com.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springdemo.Person;

public class MyTest {
	@Test
	public void test(){
	
		//获取spring上下文对象
		//ClassPathXmlApplicationContext从类路径下加载配置文件
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p=(Person) context.getBean("person");
		Person p2=(Person) context.getBean("person2");
		System.out.println(p);
		System.out.println(p2);
	}
}

输出的结果为:

3、P名称空间注入

  • 要在applicationContext.xml的<beans>中插入xmlns:p="http://www.springframework.org/schema/p"
  • applicationContext.xml的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean name="car1" class="com.springdemo.Car">
    	<property name="name" value="法拉利"></property>
    	<property name="color" value="红色"></property>
    </bean>
    <!-- 
    	p:id    给person的id属性赋值
    	p:name  给person的name属性赋值
    	p:car-ref  引用的其它bean
     -->
	<bean name="person" class="com.springdemo.Person" p:id="1" p:name="张三" p:car-ref="car1">
	</bean>
        
 </beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值