IOC的三种注射方式:
1、 第一种是通过接口注射,这种方式要求我们的类必须实现容器给定的一个接口,然后容器会利用这个接口给我们这个类来注射他所依赖的类
2、 第二种是通过setter方法来注射,这种方式也是Spring推荐的方式,等会我们就用setter方式来讲解一下Spring 的IOC
3、 第三种是通过构造方法来注射类,这种方式Spring 同样给于了实现,他和通过setter方式都在类里无任何侵入性,但是不是没有侵入性,只是把侵入性转移了,显然第一种方式要求实现特定的接口,侵入性非常强,不方便以后移植。
Sample:有一个HelloParent类要打印出”Hello,World!”,但是HelloParent并没有这个功能,Hello类有这个功能,按照常理只需HelloParent继承Hello类就可完成,但是以后HelloParent类不满意的话,想换用其它的类打印这个字符串,此时必须修改源代码!它违背了《java与模式》所提到的开闭原则,所以这种设计不可取。===》引入Spring:
1.导包:Spring.jar和commons-logging.jar;
2.建立Hello接口:
[code]public interface Hello {
public void sayHello();
}[/code]
3.建立Hello接口的实现类HelloImpl:
[code]public class HelloImpl implements Hello{
public void sayHello(){
System.out.println("Helloword");
}
}[/code]
4.建立HelloParent类,这个类要依赖于Hello这个接口的具体实现类,建立setter方法,等会Spring就是靠这个方法来注入Hello的具体实现类的。然后建立sayHello()方法,把具体的操作委托给Hello的实现类来完成。
[code]public class HelloParent {
public Hello hello;
/**
* @param hello The hello to set.
*/
public void setHello(Hello hello)
{
this.hello = hello;
}
public void sayHello()
{
hello.sayHello();
}
public void init()
{
System.out.println("init......");
}
public void destroy()
{
System.out.println("destroy......");
}
}[/code]
5.建立一个Bean.xml文件
[code]<beans>
<bean id="helloParent" class="com.haha.HelloParent" init-method="init" destroy-method="destroy" singleton="true">
<property name="hello"><ref bean="helloBean"/></property>
</bean>
<bean id="helloBean" class="com.haha.HelloImpl"/>
</beans>[/code]
6.建立一个Client调用:
[code]import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloClient {
XmlBeanFactory factory;
public HelloClient()
{
Resource cr = new ClassPathResource("bean.xml");
factory = new XmlBeanFactory(cr);
}
public void sayHello()
{
HelloParent helloParent = (HelloParent)factory.getBean("helloParent");
helloParent.sayHello();
factory.destroySingletons();
}
public static void main(String[] args)
{
HelloClient hc = new HelloClient();
hc.sayHello();
}
}[/code]
注意:bean.xml文件中init-method="init"这个是在容器注入类后会调用的一个初始化方法,destroy-method="destroy"这个是容器在销毁对象的时候会调用的一个销毁方法,singleton="true"这个确保建立的helloParent这个对象是唯一的,所以的线程共享这一个对象。init()方法,在容器生成HelloImpl的对象后注入到helloParent之后就会调用。
Spring的优点:现在,HelloParent讨厌HelloImpl这个实现类了,好,我们现在换用其他的实现类,来完成这个操作,所以在定义一个实现类HelloImpl2,这个时候我们不需要修改任何源代码,只需要修改xml配置文件即可。
1、 第一种是通过接口注射,这种方式要求我们的类必须实现容器给定的一个接口,然后容器会利用这个接口给我们这个类来注射他所依赖的类
2、 第二种是通过setter方法来注射,这种方式也是Spring推荐的方式,等会我们就用setter方式来讲解一下Spring 的IOC
3、 第三种是通过构造方法来注射类,这种方式Spring 同样给于了实现,他和通过setter方式都在类里无任何侵入性,但是不是没有侵入性,只是把侵入性转移了,显然第一种方式要求实现特定的接口,侵入性非常强,不方便以后移植。
Sample:有一个HelloParent类要打印出”Hello,World!”,但是HelloParent并没有这个功能,Hello类有这个功能,按照常理只需HelloParent继承Hello类就可完成,但是以后HelloParent类不满意的话,想换用其它的类打印这个字符串,此时必须修改源代码!它违背了《java与模式》所提到的开闭原则,所以这种设计不可取。===》引入Spring:
1.导包:Spring.jar和commons-logging.jar;
2.建立Hello接口:
[code]public interface Hello {
public void sayHello();
}[/code]
3.建立Hello接口的实现类HelloImpl:
[code]public class HelloImpl implements Hello{
public void sayHello(){
System.out.println("Helloword");
}
}[/code]
4.建立HelloParent类,这个类要依赖于Hello这个接口的具体实现类,建立setter方法,等会Spring就是靠这个方法来注入Hello的具体实现类的。然后建立sayHello()方法,把具体的操作委托给Hello的实现类来完成。
[code]public class HelloParent {
public Hello hello;
/**
* @param hello The hello to set.
*/
public void setHello(Hello hello)
{
this.hello = hello;
}
public void sayHello()
{
hello.sayHello();
}
public void init()
{
System.out.println("init......");
}
public void destroy()
{
System.out.println("destroy......");
}
}[/code]
5.建立一个Bean.xml文件
[code]<beans>
<bean id="helloParent" class="com.haha.HelloParent" init-method="init" destroy-method="destroy" singleton="true">
<property name="hello"><ref bean="helloBean"/></property>
</bean>
<bean id="helloBean" class="com.haha.HelloImpl"/>
</beans>[/code]
6.建立一个Client调用:
[code]import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloClient {
XmlBeanFactory factory;
public HelloClient()
{
Resource cr = new ClassPathResource("bean.xml");
factory = new XmlBeanFactory(cr);
}
public void sayHello()
{
HelloParent helloParent = (HelloParent)factory.getBean("helloParent");
helloParent.sayHello();
factory.destroySingletons();
}
public static void main(String[] args)
{
HelloClient hc = new HelloClient();
hc.sayHello();
}
}[/code]
注意:bean.xml文件中init-method="init"这个是在容器注入类后会调用的一个初始化方法,destroy-method="destroy"这个是容器在销毁对象的时候会调用的一个销毁方法,singleton="true"这个确保建立的helloParent这个对象是唯一的,所以的线程共享这一个对象。init()方法,在容器生成HelloImpl的对象后注入到helloParent之后就会调用。
Spring的优点:现在,HelloParent讨厌HelloImpl这个实现类了,好,我们现在换用其他的实现类,来完成这个操作,所以在定义一个实现类HelloImpl2,这个时候我们不需要修改任何源代码,只需要修改xml配置文件即可。