zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Oracle常用命令大全(很有用,做笔记)
    表格驱动编程在代码中的应用
    mac 利用svn下载远程代码出现Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
    FAILURE: Build failed with an exception.
    There is an internal error in the React performance measurement code.Did not expect componentDidMount timer to start while render timer is still in progress for another instance
    react native TypeError network request failed
    Android向系统相册中插入图片,相册中会出现两张 一样的图片(只是图片大小不一致)
    react-native Unrecognized font family ‘Lonicons’;
    react-native SyntaxError xxxxx/xx.js:Unexpected token (23:24)
    Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.
  • 原文地址:https://www.cnblogs.com/chengshun/p/9776092.html
Copyright © 2011-2022 走看看