zoukankan      html  css  js  c++  java
  • Spring实例化bean的三种方法

    1.用构造器来实例化

    1. <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 

    2.使用静态工厂方法实例化

    要写一个bean,bean中定义一个静态方法,生成bean,配置factory-method指定静态方法,运行时容器就会自动调用静态方法生成实例

    bean

    1. package com.hsit.hello.impl;  
    2.   
    3. import com.hsit.hello.IHello;  
    4.   
    5. public class CHhello implements IHello {  
    6.   
    7.     private String msg;  
    8.   
    9.     public void sayHello() {  
    10.         System.out.println("中文" + msg);  
    11.     }  
    12.   
    13.     public String getMsg() {  
    14.         return msg;  
    15.     }  
    16.   
    17.     public void setMsg(String msg) {  
    18.         this.msg = msg;  
    19.     }  
    20.       
    21.     @Override  
    22.     public String toString() {  
    23.         // TODO Auto-generated method stub  
    24.         return "Chinese";  
    25.     }  
    26.   
    27.     public static CHhello createInstance() {  
    28.         System.out.println("jingtai");  
    29.         return new CHhello();  
    30.     }  

    配置文件

    1. <bean id="hello1" class="com.hsit.hello.impl.CHhello" factory-method="createInstance" lazy-init="true">  
    2.     <!-- setter注入 -->  
    3.         <property name="msg" value="哈哈">  
    4.         </property>  
    5.     </bean>  

    3.使用实例工厂方法实例化

    要写两个bean,一个是要实例化的bean,另一个是工厂bean。容器先实例化工厂bean,然后调用工厂bean配置项factory-method中指定的方法,在方法中实例化bean

    工厂bean:

    1. package com.hsit.hello.impl;  
    2.   
    3. public class ENhelloFactory {  
    4.       
    5.     public ENhello createInstance() {  
    6.         System.out.println("ENhello工厂");  
    7.         return new ENhello();  
    8.     }  
    9.       
    10.     public ENhelloFactory() {  
    11.         System.out.println("chuanjian");  
    12.     }  
    13.   
    14. }  

    要实例化的bean:

    1. package com.hsit.hello.impl;  
    2.   
    3. import com.hsit.hello.IHello;  
    4.   
    5. public class ENhello implements IHello {  
    6.   
    7.     @Override  
    8.     public void sayHello() {  
    9.         // TODO Auto-generated method stub  
    10.         System.out.println("hello");  
    11.     }  
    12.       
    13.     @Override  
    14.     public String toString() {  
    15.         // TODO Auto-generated method stub  
    16.         return "我是ENhello";  
    17.     }  
    18. }  

    配置文件

    1. <bean id="eHelloFactory" class="com.hsit.hello.impl.ENhelloFactory" />  
    2. <!-- factory-bean填上工厂bean的id,指定工厂bean的工厂方法生成实例,class属性不填 -->  
    3. <bean id="example" factory-bean="eHelloFactory" factory-method="createInstance"/>  

    测试代码

    1. BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
    2.           
    3.         ENhello eNhello = (ENhello) factory.getBean("example");  
    4.         System.out.println(eNhello.toString());  
    5.           
    6.          factory.getBean("hello1"); 
  • 相关阅读:
    git Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
    git error: failed to push some refs to 'git@github.com:xxx/xxx.git'
    git LF will be replaced by CRLF in hellogit.txt
    为什么要买保险,并且如何配置保险,以及家庭保险的配置
    Molile App(HTTP/HTML)—Analyze Traffic
    Molile App(HTTP/HTML)—Record and Analyze Traffic
    清空KindEditor富文本编辑器里面的内容方法
    图片上传和显示——上传图片——上传文件)==ZJ
    页面静态化实现——根据模板动态创建静态页
    通过Ajax实现增删改查
  • 原文地址:https://www.cnblogs.com/liwendeboke/p/6228129.html
Copyright © 2011-2022 走看看