Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203212
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-20 21:36:02

  Bean高级管理

  一,非XML定义文件的配置方式
  1,使用.properties文件

#beans-config.properties
helloBean.class=com.biaoflying.HelloWorld
helloBean.helloWorld=Welcome

#HelloWorld.java
package com.biaoflying;

public class HelloWorld {
    private String helloWorld;
    
    public String getHelloWorld(){
        return helloWorld;
    }
    public void setHelloWorld(String helloWorld){
        this.helloWorld=helloWorld;
    }
}

#主程序
package com.biaoflying;

import org.springframework.beans.factory.*;
import org.springframework.beans.factory.support.*;
import org.springframework.core.io.*;

public class SpringDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BeanDefinitionRegistry reg=
            new DefaultListableBeanFactory();
        PropertiesBeanDefinitionReader reader=
            new PropertiesBeanDefinitionReader(reg);
        reader.loadBeanDefinitions(
                new FileSystemResource(
                        "beans-config.properties"));
        BeanFactory factory=(BeanFactory)reg;
        HelloWorld hello=(HelloWorld)factory.getBean("helloBean");
        System.out.println(hello.getHelloWorld());
   
    }
}
#将输出:Welcome

  2,直接在主程序中实现依赖关系的注入。

#修改主程序为:
package com.biaoflying;

import org.springframework.beans.factory.*;
import org.springframework.beans.factory.support.*;
import org.springframework.beans.*;

public class SpringDemo2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MutablePropertyValues properties=
            new MutablePropertyValues();
        properties.addPropertyValue(
                "helloWorld","Hello!biaoflying");
       
        RootBeanDefinition definition=
            new RootBeanDefinition(
                    HelloWorld.class,properties);
       
        BeanDefinitionRegistry reg=
            new DefaultListableBeanFactory();
        reg.registerBeanDefinition("helloBean",definition);
       
        BeanFactory factory=(BeanFactory)reg;
        HelloWorld hello=(HelloWorld)factory.getBean("helloBean");
       
        System.out.println(hello.getHelloWorld());
    }
}

  二,使用BeanPostProcessor

package com.biaoflying;

public class HelloBean {
    private String helloWord;
    
    public String getHelloWord(){
        return helloWord;
    }
    public void setHelloWord(String helloWorld){
        this.helloWord=helloWorld;
    }
}

package com.biaoflying;

import java.lang.reflect.Field;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

public class UppercaseModifier implements BeanPostProcessor{
    public Object postProcessBeforeInitialization(
            Object bean,String name)throws BeansException{
        Field[] fields=
            bean.getClass().getDeclaredFields();
        
        for(int i=0;i            if(fields[i].getType().equals(String.class)){
                fields[i].setAccessible(true);
                try{
                    String original=(String)fields[i].get(bean);
                    fields[i].set(bean,original.toUpperCase());
                }catch(IllegalArgumentException e){
                    e.printStackTrace();
                }catch(IllegalAccessException e){
                    e.printStackTrace();
                }
            }
        }
        return bean;
    }
    public Object postProcessAfterInitialization(
            Object bean,String name)throws BeansException{
        return bean;
    }
}
 
#主程序
package com.biaoflying;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
FileSystemXmlApplicationContext;

public class SpringDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext(
                    "beans-config.xml");
       
        HelloBean hello=
            (HelloBean)context.getBean("helloBean");
        System.out.println(hello.getHelloWord());
    }
}

#XML文件

 "">
 

        
        
        
         
              hello.properties
         

     

        
        
            
                ${com.biaoflying.helloWord}
            

        


  TIP:Spring会检查定义文件中是否有实现了BeanPostProcessor接口的类,并在每个bean初始化之前,之后执行相应的postProcessBeforeInitialization与postProcessAfterInitialization方法。

  三,PropertyPlaceholderConfigurer类用于将部分经常变动的配置信息移除至一个或者多个.properties文件中。

package com.biaoflying;

public class HelloWord {
    String name,hello;
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setHello(String hello){
        this.hello=hello;
    }
    public String getHello(){
        return hello;
    }
}

package com.biaoflying;

import org.springframework.context.*;
import org.springframework.context.support.*;

public class SpringDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext(
                    "beans-config.xml");
        
        HelloWord hello=(HelloWord)context.getBean("helloBean");
        
        System.out.println("Name: "+hello.getName());
        System.out.println("Hello: "+hello.getHello());
    }
}

#XML文件

 "">
 

    
        
            hello.properties
        

    

    
        
            biaoflying
        

        
            ${com.biaoflying.helloWord}
        

    


#.properties文件
com.biaoflying.helloWord=Welcome

  四,使用 CustomEditorConfigurer简化XML文件。

#HelloBean.java
package com.biaoflying;

public class HelloBean {
    private String helloWord;
    private User user;
    
    public void setHelloWord(String helloWord){
        this.helloWord=helloWord;
    }
    public String getHelloWord(){
        return helloWord;
    }
    
    public void setUser(User user){
        this.user=user;
    }
    public User getUser(){
        return user;
    }
}
#User.java
package com.biaoflying;

public class User {
    private String name;
    private int number;
   
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
   
    public int getNumber(){
        return number;
    }
    public void setNumber(int number){
        this.number=number;
    }
}

package com.biaoflying;

import java.beans.PropertyEditorSupport;

public class UserEditor extends PropertyEditorSupport{
    public void setAsText(String text){
        String[] strs=text.split(",");
        
        int number=Integer.parseInt(strs[1]);
        User user=new User();
        user.setName(strs[0]);
        user.setNumber(number);
        
        setValue(user);
    }
}

package com.biaoflying;

import org.springframework.context.*;
import org.springframework.context.support.*;

public class SpringDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext(
                    "beans-config2.xml");
        HelloBean bean=(HelloBean)context.getBean("helloBean");
        
        System.out.println(bean.getHelloWord()+" to:");
        System.out.println(bean.getUser().getName());
        System.out.println(bean.getUser().getNumber());
    }
}

  使用CustomEditorConfigurer前后XML文件的比较:

之前:

 "">
 

   
       
            biaoflying
       

       
            55482
       

   

   
   
       
            Welcome
       

       
           
       

   



#之后:

 "">
 

    
        
            
                
                    
                

            

        

    

    
    
        
            Welcome
        

        
            biaoflying,558248
        

    


  TIP:这里管理的bean很少,在数量比较多的情况下,CustomEditorConfigurer的优势将体现出来。
阅读(727) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~