分类: Java
2008-03-20 21:36:02
#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 |
#修改主程序为: 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()); } } |
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 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文件 ""> |
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文件 ""> |
#.properties文件 com.biaoflying.helloWord=Welcome |
#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()); } } |
之前: ""> |
#之后: ""> |