spring中非XML定义文件的配置方式

  1. 用.properties文件配制
    java Bean代码
    1. package org.beans;   
    2.   
    3. public class HelloBean {   
    4. private String helloWorld;   
    5.   
    6. public String getHelloWorld() {   
    7.     return helloWorld;   
    8. }   
    9.   
    10. public void setHelloWorld(String helloWorld) {   
    11.     this.helloWorld = helloWorld;   
    12. }   
    13.   
    14. }  

beans-config.properties文件

properties代码
  1. helloBean.class=org.beans.HelloBean   
  2. helloBean.helloWorld=welcome  
  3.   
主程序代码
  1. package org.test;   
  2.   
  3. import org.beans.HelloBean;   
  4. import org.springframework.beans.factory.BeanFactory;   
  5. import org.springframework.beans.factory.support.BeanDefinitionRegistry;   
  6. import org.springframework.beans.factory.support.DefaultListableBeanFactory;   
  7. import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;   
  8. import org.springframework.core.io.ClassPathResource;   
  9. import org.springframework.core.io.FileSystemResource;   
  10.   
  11. public class SpringTest {   
  12.   
  13.     /**  
  14.      * @param args  
  15.      */  
  16.     public static void main(String[] args) {   
  17.         // TODO Auto-generated method stub   
  18. BeanDefinitionRegistry re=new DefaultListableBeanFactory();   
  19. PropertiesBeanDefinitionReader reader=new PropertiesBeanDefinitionReader(re);   
  20. //也可以用reader.loadBeanDefinitions(new ClassPathResource("beans-config.properties"));   
  21. reader.loadBeanDefinitions(new FileSystemResource("org/properties/beans-config.properties"));   
  22. BeanFactory factory=(BeanFactory)re;   
  23. HelloBean hello=(HelloBean)factory.getBean("helloBean");   
  24. System.out.println(hello.getHelloWorld());   
  25.     }   
  26.   
  27. }   

2、在程序中直接编写程序

java 代码
  1. package org.test;   
  2.   
  3. import org.beans.HelloBean;   
  4. import org.springframework.beans.MutablePropertyValues;   
  5. import org.springframework.beans.factory.BeanFactory;   
  6. import org.springframework.beans.factory.support.BeanDefinitionRegistry;   
  7. import org.springframework.beans.factory.support.DefaultListableBeanFactory;   
  8. import org.springframework.beans.factory.support.RootBeanDefinition;   
  9.   
  10. public class springMain {   
  11.   
  12.     /**  
  13.      * @param args  
  14.      */  
  15.     public static void main(String[] args) {   
  16.         // TODO Auto-generated method stub   
  17.         //设置属性   
  18. MutablePropertyValues properties=new MutablePropertyValues();   
  19. properties.addPropertyValue("helloWorld","Hello,Weimin");   
  20. //设置bean定义   
  21. RootBeanDefinition definition=new RootBeanDefinition(HelloBean.class,properties);   
  22. //注册bean定义与bean别名   
  23. BeanDefinitionRegistry re=new DefaultListableBeanFactory();   
  24. re.registerBeanDefinition("helloBean", definition);   
  25.   
  26. BeanFactory factory=(BeanFactory)re;   
  27. HelloBean hello=(HelloBean)factory.getBean("helloBean");   
  28. System.out.println(hello.getHelloWorld());   
  29.     }   
  30.   
  31. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值