Spring入门——Spring IOC容器

本文介绍了面向接口编程的概念,详细解析了IOC(控制反转)及其在Spring框架中的应用,包括Bean配置、初始化方法以及常见的注入方式。通过实例展示了设值注入和构造注入的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IOC

  • 接口及面向接口编程
  • 什么是IOC
  • Spring的Bean配置
  • Bean的初始化
  • Spring的常用注入方式

接口

  • 用于沟通的中介物的抽象化
  • 实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式
  • 对应Java接口即声明,声明了哪些方法是对外公开提供的
  • 在Java8中,接口可以拥有方法体

面向接口编程

  • 结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,                                                       各层间仅依赖接口而非实现类  (设计模式中的依赖倒转原则)
  • 接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要
  • “面向接口编程”中的“接口”适用于隐藏具体实现和实现多态性的组件
  • 例子
接口
package com.txr.interfaces;

public interface OneInterface {
	public String hello(String str);

}
实现类
package com.txr.interfaces.impl;

import com.txr.interfaces.OneInterface;

public class OneInterfaceImpl implements OneInterface{

	@Override
	public String hello(String str) {
		return str+"  word!!";
	}

}

测试类
package com.txr.test;

import com.txr.interfaces.OneInterface;
import com.txr.interfaces.impl.OneInterfaceImpl;

public class Main {
	public static void main(String[] args) {
		OneInterface oif=new OneInterfaceImpl();
		System.out.println(oif.hello("hello"));
	}
}

运行结果
hello  word!!

什么是IOC

  • IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护
  • DI:(依赖注入)是其一种实现方式
  • 目的:创建对象并且封装对象之间的关系

Spring的Bean配置

刚才的接口在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"
	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-4.2.xsd"
		>
	<bean id="oneInterface" class="com.txr.interfaces.impl.OneInterfaceImpl"/>



</beans>
测试类:

package com.txr.test;

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

import com.txr.interfaces.OneInterface;

public class Main {
	public static void main(String[] args) {
		/*OneInterface oif=new OneInterfaceImpl();
		System.out.println(oif.hello("hello"));*/
		ApplicationContext context=new ClassPathXmlApplicationContext("com/txr/resource/spring-ioc.xml");
		OneInterface oneInterface=(OneInterface)context.getBean("oneInterface");
		System.out.println(oneInterface.hello("word"));
	}
}
测试结果
word  word!!

Bean容器初始化

基础:俩个包

-org.springframework.beans
-org.springframework.context
-BeanFactory提供配置结构和基本功能,加载并初始化Bean
-ApplicationContext保存了Bean对象并在Spring中被广泛使用

方式,ApplicationContext

-本地文件
-Classpath
-Web应用中依赖servelet或Listener

Bean容器的初始化方法



Spring注入

  • Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
  • 常用的俩种注入方式
-设值注入
-构造注入

例子

接口
package com.txr.interfaces;

public interface InjectionDAO {
	public void save(String arg);
}

实现类
package com.txr.interfaces.impl;

import com.txr.interfaces.InjectionDAO;
import com.txr.interfaces.OneInterface;

public class InjectionDAOImpl implements InjectionDAO {

	public OneInterface oneInterface;
	public void setOneInterface(OneInterface oneInterface)
	{
		this.oneInterface=oneInterface;
	}
	@Override
	public void save(String arg) {
		System.out.println("信息为:"+arg);
		System.out.println(oneInterface.hello("hello"));
	}
	public InjectionDAOImpl(OneInterface oneInterface) {
		this.oneInterface=oneInterface;
	}
	public InjectionDAOImpl() {
		// TODO Auto-generated constructor stub
	}
	

}


设值注入

<?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"
	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-4.2.xsd"
		>
	<bean id="oneInterface" class="com.txr.interfaces.impl.OneInterfaceImpl"/>
	<bean id="injectionDAO" class="com.txr.interfaces.impl.InjectionDAOImpl">
		<property name="oneInterface" ref="oneInterface"></property>
	</bean>


</beans>

构造注入
<?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"
	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-4.2.xsd"
		>
	<bean id="oneInterface" class="com.txr.interfaces.impl.OneInterfaceImpl"/>
	<bean id="injectionDAO" class="com.txr.interfaces.impl.InjectionDAOImpl">
		<!-- <property name="oneInterface" ref="oneInterface"></property> -->
		<constructor-arg name="oneInterface" ref="oneInterface"></constructor-arg>
	</bean>


</beans>
运行结果:
信息为:test
hello  word!!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值