Spring 入门之IOC/DI

Spring中主要有两大部分,包括IOC控制反转(或称DI依赖注入)和AOP(面向切面编程)。本文主要说明IOC基本用法。

1. 设值注入
先建立好一个pojo,Student类

package vo;

public class Student {
	private int id;
	private String name;
	private int age;
	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;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
}

之后在src目录下建立配置文件,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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
   
	<!-- name为给一个bean对象所起的名字,class为该bean对象对应的类 -->
	<bean name="s" class="vo.Student">
		<!-- name对应该对象的属性名称,value对应该属性的值 -->
		<property name="id" value="123"/>
		<property name="name" value="clark"/>
		<property name="age" value="66"/>
	</bean>
</beans>

建立测试类

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

import vo.School;
import vo.Student;

public class Test {
	public static void main(String args[]) {
		//取得配置文件
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
			new String("applicationContext.xml"));
			
		//注入的方式实例化Student对象,getBean方法中的"s"与配置文件中的bean对应
		Student stu = (Student)ctx.getBean("s");
		System.out.println(stu.getName());	
	}
}

2. 注入对象
将一个对象注入到另一个对象的属性中。


首先再添加一个pojo类,School类

package vo;

public class School {
	private int number;
	private Student student;
	
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public School(int number, Student student) {
		super();
		this.number = number;
		this.student = student;
	}
	public School() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

}

之后更改配置applicationContext.xml文件,添加如下代码

<bean name="sc" class="vo.School">
		<property name="number" value="12"/>
		<property name="student" ref="s"/>
</bean>

编写测试类

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

import vo.School;
import vo.Student;

public class Test {
	public static void main(String args[]) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
			new String("applicationContext.xml"));

		School sc = (School)ctx.getBean("sc");
		System.out.println(sc.getStudent().getName());
	}

}

3. 基于注解的注入
更改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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
   <!-- 使用注解进行配置 -->
   <context:annotation-config/>
   
   <!-- name为给一个bean对象所起的名字,class为该bean对象对应的类 -->
	<bean name="s" class="vo.Student">
		<!-- name对应该对象的属性名称,value对应该属性的值 -->
		<property name="id" value="123"/>
		<property name="name" value="clark"/>
		<property name="age" value="66"/>
	</bean>
	
	<bean name="sc" class="vo.School">
		<property name="number" value="12"/>
		<!--注释此行,不再手动注入 <property name="student" ref="s"/> -->
	</bean>
  
</beans>

之后在School类中加入@Autowired注解,启动自动装配

package vo;

import org.springframework.beans.factory.annotation.Autowired;

public class School {
	private int number;
	
	@Autowired
	private Student student;
	
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public Student getStudent() {
		return student;
	}
	
	//也可在此处加@Autowired注解,二选一即可
	public void setStudent(Student student) {
		this.student = student;
	}
	public School(int number, Student student) {
		super();
		this.number = number;
		this.student = student;
	}
	public School() {
		super();
		// TODO Auto-generated constructor stub
	}
}

运行,结果相同

4. bean自动装配
将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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
 	 <!-- 开启组件自动扫描 -->
    <context:component-scan base-package="vo"/>
     
</beans>

在Student类和School类前加@Component注解。上一步开启的自动扫描会自动扫描带有@Component注解的类,把此类当作bean。

@Component("s")
public class Student{
}

@Component("sc")
public class School{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值