IOC理解
使用IOC对象的依赖都是在对象创建是由负责协调系统中各个对象的外部实体提供的。
减少耦合的一个通常做法是具体实现隐藏在接口下,使得具体实现类的替换不会影响到引用类。
IOC也被称之为DI,是Spring的两大核心概念之一。
A. 依赖注入的概念
Spring的两大核心概念:一个是控制反转(IOC), 也叫做依赖注入(DI);还有一个是面向切面编程(AOP),IOC和DI虽然不是Spring的首创,但是他没在这两方面都非常优秀,可以说整个Spring框架都是围绕着其IOC实现和Aop实现架设起来的。
控制反转模式的基本概念:当某个java对象需要依赖另一个java对象时,不是自身直接创建依赖对象而是由实现IOC容器来创建,并将它注入到需要这个依赖对象的java对象中
B. Spring的依赖注入
Spring框架带有一个IOC容器它使用的注入方式有构造器注入和setter注入。
a) 构造器注入:通过构造器方法来传入所依赖的对象,从而完成依赖关系的设定
b) Setter注入:通过set方法来传入所依赖的对象,从而完成依赖关系的设定
c) 案例:
Service
package cn.csdn.service;
public interface GreetingService {
void say();
}
ServiceImpl
package cn.csdn.service;
public class GreetingServiceImpl implements GreetingService{
private String say;
private String name;
public GreetingServiceImpl(String say, String name) {
this.say = say;
this.name = name;
}
@Override
public void say() {
System.out.println(name+":say"+say);
}
public void setSay(String say) {
this.say = say;
}
public void setName(String name) {
this.name = name;
}
}
Junit
package cn.csdn.junit;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import cn.csdn.service.GreetingService;
import cn.csdn.service.GreetingServiceImpl;
public class GreetingTest {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
GreetingServiceImpl gsi=(GreetingServiceImpl)ac.getBean("greetingServiceImpl");
gsi.say();
}
@Test
public void test1(){
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
GreetingService gsi=(GreetingService)ac.getBean("greetingServiceImpl");
gsi.say();
}
@Test
public void test2(){
ApplicationContext ac=new FileSystemXmlApplicationContext(new String[]{"src/applicationContext.xml"});
GreetingService gsi=(GreetingService)ac.getBean("greetingServiceImpl");
gsi.say();
}
@Test
public void test3()throws Exception{
Resource resource=new FileSystemResource("src/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
GreetingService greetingService=(GreetingService)factory.getBean("greetingServiceImpl");
greetingService.say();
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<!-- set -->
<!--
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<property name="say" value="你好!"></property>
<property name="name" value="Spring"></property>
</bean>
-->
<!-- 构造器 -->
<!--
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<constructor-arg index="0">
<value>你好!</value>
</constructor-arg>
<constructor-arg index="1">
<value>Spring</value>
</constructor-arg>
</bean>
-->
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<constructor-arg type="java.lang.String">
<value>你好!</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>Spring</value>
</constructor-arg>
</bean>
</beans>