Spring入门——Spring IOC容器
`
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!!
还没有评论,来说两句吧...