Spring学习之路-1

①先来介绍一下spring的配置文件吧。

<?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:util="http://www.springframework.org/schema/util"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
<bean id="一个类的标识,相当于这个类的一个对象" class="该类的包名.类名">
<property name="对象所具有的属性名与类中的属性名一致" value="属性的值">
<!--这是最普通的配置-->
</property>
</bean>
</beans>

一、set注入

建立一个computer类,再建立一个测试类。然后修改配置文件。这里只有一个类,没有产生相关性。

package entity;

public class Computer {
private String cpu;
private String hdd;//硬盘
private String mainborder;//主板



@Override
public String toString() {
	return "Computer [cpu=" + cpu + ", hdd=" + hdd + ", mainborder=" + mainborder + "]";
}
public Computer() {
	System.out.println("我是Computer的构造器");
}
public String getCpu() {
	return cpu;
}
public void setCpu(String cpu) {
	this.cpu = cpu;
}
public String getHdd() {
	return hdd;
}
public void setHdd(String hdd) {
	this.hdd = hdd;
}
public String getMainborder() {
	return mainborder;
}
public void setMainborder(String mainborder) {
	this.mainborder = mainborder;
}

}
package controller;

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

import entity.Computer;

public class TestSpringIOC {
	@Test
	public void test01() {
	//加载配置文件
	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	//1.new ClassPathXmlApplicationContext()时,创建这个容器
	//2.然后加载配置文件,生成对象
	//3.最后通过getBean(id)获取对象。
	//System.out.println(ac);
	Object obj=	ac.getBean("com");//com是applicationContext.xml中的computer类的id
	System.out.println(obj);
	}
}
<?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:util="http://www.springframework.org/schema/util"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<bean id="com" class="entity.Computer">
		<!--给对象的变量赋值的方式是set注入 -->
		<!-- 这里的name就是对象的参数属性,value为参数属性的值。这种方法要求类中必须有set该参数的方法!!! -->
		<property name="cpu" value="英特尔"></property>
		<!-- 这里的name和set方法的方法名相关,相关性为name="set方法的首字母小写的方法名", 例如Computer类中方法名为setHdd,这里的name就等于"hdd",没有set只有Hdd的小写hdd -->
	</bean>
</beans>

上面的例子只有一个computer类,没有产生相关性,现在在增加一个student类,让这两个类产生相关性。

computer的代码还是和上面一样就不在赘述了。

package entity;

public class Student {

	private String name;
	private Computer com;

	@Override
	public String toString() {
		return "Student [name=" + name + ", com=" + com + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCom(Computer com) {
		this.com = com;
	}

	public Student() {
		System.out.println("我是Student的构造器");
		
	}

}
package controller;

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

import entity.Computer;

public class TestSpringIOC {
	@Test
	public void test01() {
	//加载配置文件
	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	//1.new ClassPathXmlApplicationContext()时,创建这个容器
	//2.然后加载配置文件,生成对象
	//3.最后通过getBean(id)获取对象。
	//System.out.println(ac);
	Object obj=	ac.getBean("stu");//stu是applicationContext.xml中的student类的id
	System.out.println(obj);
	}
}

可以看出student类中有computer类的对象,也就是computer的对象在student类中做studen对象的一个属性。这样两个类产生了相关性,因此配置文件需要做一个修改。注意<bean><bean/>中间的代码,每个标签的含义我都注释在了代码上。很详细,包括标签的属性值。

<?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:util="http://www.springframework.org/schema/util"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	

	<!-- class="包名.类名" id为class的一个对象!! -->
	<bean id="stu" class="entity.Student">
	<property name="name" value="#{com.cpu}"></property>
	<!-- #{com.cpu}是springEL表达式,#{id.属性名} -->
		<property name="com" ref="com"></property>
		<!-- computer类的对象是student类的属性,所以name=student类中"set方法的首字母小写的方法名", ref="值",该值为computer类在xml中对应的bean的id值。 -->
	</bean>

	<bean id="com" class="entity.Computer">
		<!--给对象的变量赋值的方式是set注入 -->
		<!-- 这里的name就是对象的参数属性,value为参数属性的值。这种方法要求类中必须有set该参数的方法!!! -->
		<property name="cpu" value="英特尔"></property>
		<!-- 这里的name和set方法的方法名相关,相关性为name="set方法的首字母小写的方法名", 例如Computer类中方法名为setHdd,这里的name就等于"hdd",没有set只有Hdd的小写hdd -->
	</bean>
<beans/>

一定要明白id,class,name,value,ref这几个标签的含义,只有弄得含义才能在代码中灵活使用。

二、构造器注入

这个不是很常用,暂时先不补充了,等我用到了我在仔细补充

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值