02 Spring框架 简单配置和三种bean的创建方式

上一节学习了如何搭建SpringIOC的环境,下一步我们就来讨论一下如何利用ioc来管理对象和维护对象关系。

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> 
</beans>

这个是applicationContext.xml的 dtd。(补充上一节)

我们知道有以下集几种方法来创建对象:

  • 采用new关键字创建对象
  • 静态工厂方法,例如:Calendar cal=Calendar.getInstance();
  • 对象(现有)工厂方法 

所以ioc中在aplicationContext.xml中也对应了三种创建对象的途径:

<bean id="" class=""><bean id="" class="" factory-method=""><bean id="" factory-bean="" factory-method="">

 

id可以自定义 
Class需要自己在工程中创建 
接下来写一个简单的demo

① 
applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> 
<bean id="c1" class="com.Spring.Demo.HelloWorld">
</beans>

HelloWorld.java :

package com.Spring.Demo;

public class HelloWorld {
    public void show(){
        System.out.println("欢迎Spring!");
    }

}

接下来写一个测试 
TestHelloWorld.java :

package com.Spring.Demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHelloWorld {
    public static void main(String[] args){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld hw=(HelloWorld)context.getBean("c1");
        hw.show();
    }

}

运行后控制台输出为:

欢迎Spring!

这是第一种创建对象的方式,下面我们继续看看怎么使用动态工厂创建对象。

 

<!--静态工厂创建对象配置文件-->
<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> 

<bean id="service" class="dynamicFactory.ServiceFactory" factory-method="getService"/>
</beans>
//需要创建的对象,实现的接口(接口化编程,降低耦合度)
package staticFactory;

public interface SomeServices {
    String doFirst();
    void doSecond();
}
//实现类,实现上面的接口
package staticFactory;

public class SomeServiceImp implements SomeServices{

    @Override
    public String doFirst() {
        System.out.println("print first");
        return null;

    }

    @Override
    public void doSecond() {
        System.out.println("print second");

    }

}
//静态工厂,产生上面类的实例
package staticFactory;

public class ServiceFactory {
    public static SomeServiceImp getService() {
        return new SomeServiceImp();
    }
}
//测试类(Junit)
public class test {

    @Test
    public void Test01() {
        String resource = "staticFactory/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
        SomeServices service = (SomeServices)ac.getBean("service");
        service.doFirst();
        service.doSecond();
    }
}

控制台输出:

print first
print second

 

上面的是静态工厂bean的创建方式,接下来我们来看普通工厂对象创建方式:

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns:util="http://www.springframework.org/schema/util"  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> 


<bean id="serviceFactory" class="dynamicFactory.ServiceFactory"></bean>
<!--普通工厂bean,factory-bean填的是上面的serviceFactory-->
<bean id="service" factory-bean="serviceFactory" factory-method="getService"/>


</beans>
//需要创建的对象,实现的接口(接口化编程,降低耦合度)
package dynamicFactory;

public interface SomeServices {
    String doFirst();
    void doSecond();
}
//实现类,实现上面的接口
package dynamicFactory;

public class SomeServiceImp implements SomeServices{

    @Override
    public String doFirst() {
        System.out.println("print first");
        return null;

    }

    @Override
    public void doSecond() {
        System.out.println("print second");

    }

}
//这个和第二种不同,这里的工厂类方法是非静态的
package dynamicFactory;

public class ServiceFactory {
    public SomeServiceImp getService() {
        return new SomeServiceImp();
    }
}
//测试类(Junit)
public class test {

    @Test
    public void Test01() {
        String resource = "dynamicFactory/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
        SomeServices service = (SomeServices)ac.getBean("service");
        service.doFirst();
        service.doSecond();
    }
}

控制台输出:

print first
print second

这样我们的三种创建方式就说完了。

 

 

 

 

版权声明:本文为博主原创文章,如需转载请表明出处。 https://blog.csdn.net/qq_39266910/article/details/77726614

 

posted @ 2018-10-12 08:11  一纸流年  阅读(321)  评论(0编辑  收藏  举报