`
zhongxiucheng
  • 浏览: 70539 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring实战之org.springframework.beans.factory.config.MethodInvokingFactoryBean

 
阅读更多

在用spring管理我们的类的时候有时候希望有些属性值是来源于一些配置文件,系统属性,或者一些方法调用的结果,对于前两种使用方式可以使用spring的PropertyPlaceholderConfigurer类来注入,这些内容已经在前面的文章中说过,这里就不在重复了。这里就针对第三种情况做一些说明,其实在spring中是提供了对这种需求的解决方案的,那就是使用org.springframework.beans.factory.config.MethodInvokingFactoryBean类来生成需要注入的bean的属性,下面是一个例子

MyBean.java一个普通的POJO类

  1. importorg.apache.commons.lang.builder.ToStringBuilder;
  2. publicclassMyBean{
  3. privateStringname;
  4. privateStringjavaVersion;
  5. publicStringgetName(){
  6. returnname;
  7. }
  8. publicvoidsetName(Stringname){
  9. this.name=name;
  10. }
  11. publicStringgetJavaVersion(){
  12. returnjavaVersion;
  13. }
  14. publicvoidsetJavaVersion(StringjavaVersion){
  15. this.javaVersion=javaVersion;
  16. }
  17. @Override
  18. publicStringtoString(){
  19. returnToStringBuilder.reflectionToString(this);
  20. }
  21. }

MyBeanNameProvider.java提供一个静态方法用来生成MyBean类的name属性,同理非静态方法也是可以的,参考本例子中javaVersion属性定义

  1. publicclassMyBeanNameProvider{
  2. publicstaticStringgetName(){
  3. return""+System.currentTimeMillis();
  4. }
  5. }

spring.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6. http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
  7. default-lazy-init="true">
  8. <beanid="myBean"class="MyBean">
  9. <propertyname="name"><reflocal="myBeanName"/></property>
  10. <propertyname="javaVersion"><reflocal="javaVersion"/></property>
  11. </bean>
  12. <beanid="sysProps"class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  13. <propertyname="targetClass">
  14. <value>java.lang.System</value>
  15. </property>
  16. <propertyname="targetMethod">
  17. <value>getProperties</value>
  18. </property>
  19. </bean>
  20. <beanid="javaVersion"class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  21. <propertyname="targetObject">
  22. <reflocal="sysProps"/>
  23. </property>
  24. <propertyname="targetMethod">
  25. <value>getProperty</value>
  26. </property>
  27. <propertyname="arguments">
  28. <list>
  29. <value>java.version</value>
  30. </list>
  31. </property>
  32. </bean>
  33. <beanid="myBeanName"class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  34. <propertyname="staticMethod"><value>MyBeanNameProvider.getName</value></property>
  35. </bean>
  36. </beans>

Test.java一个测试类

  1. importorg.springframework.context.ApplicationContext;
  2. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  3. publicclassTest{
  4. publicstaticvoidmain(String[]args){
  5. ApplicationContextctx=newClassPathXmlApplicationContext("spring.xml");
  6. MyBeanmyBean=(MyBean)ctx.getBean("myBean");
  7. System.out.println(myBean);
  8. }
  9. }

运行Test类,就可以看到MyBean的两个属性都通过spring配置的方式注入到了类中

分享到:
评论

相关推荐

    org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    `org.springframework.beans.factory.config.PropertyPlaceholderConfigurer` 是Spring框架中的一个重要组件,主要负责处理配置文件中的占位符替换。这个类是Spring在初始化bean时用来解析和注入环境变量或系统属性...

    asm-3.2.3.jar

    org.springframework.beans.factory.config org.springframework.beans.factory.parsing org.springframework.beans.factory.serviceloader org.springframework.beans.factory.support org.springframework.beans....

    springAOP demo 带错误解决文档

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org....

    springmvc-ibatis

    &lt;bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;!-- 配置數據源 --&gt; &lt;bean id="dataSource" class="org.apache....

    spring_MVC源码

    05. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 06. ...

    spring-spring-framework-4.3.24.RELEASE.zip

    在源码中,`org.springframework.beans.factory.config`包包含了许多关于生命周期的接口和类,如InitializingBean、DisposableBean以及BeanFactoryPostProcessor等。 6. **事件驱动模型**:Spring提供了基于...

    struts2驱动包

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested ...

    Spring项目中怎么配置log4j

    &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:log4j.properties"/&gt; ``` 这使得Spring在启动时...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件2

    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.xml....

    Spring驱动JAR包.zip

    主要涉及的类包括`org.springframework.beans.factory.BeanFactory`和`org.springframework.context.ApplicationContext`。 2. **AOP支持**:Spring的AOP模块允许开发者定义“切面”(Aspects),这些切面可以封装...

    spring3.07

    `org.springframework.beans.factory.annotation.Autowired` 注解用于自动装配依赖,以及 `org.springframework.beans.factory.BeanFactory` 和 `org.springframework.beans.factory.config.DependencyDescriptor` ...

    spring-framework-5.1.x-源碼解析详细注解

    跟踪`org.springframework.beans.factory.config.AutowireCapableBeanFactory`中的`initializeBean`方法,可以看到如何处理懒加载逻辑。 3. 循环依赖处理 Spring框架能够处理各种复杂依赖关系,包括循环依赖。在...

    SPRING:bean配置properties

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/spring/include/dbQuery.properties ``` 这里,`propertyConfigurerForAnalysis`是`...

    Struts2+Spring3+MyBatis3完整实例

    - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e7d503: defining beans [accountBizImpl,accountDaoImpl,baseMapperDaoImpl,org.springframework....

    spring无法读取properties文件数据问题详解

    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties &lt;value&gt;classpath:config.properties ``` 问题二:Service 中无法读取 ...

    金蝶BOSV6.1_业务组件API参考手册

    Packages ...com.kingdee.bos.transaction.springframework.beans.factory com.kingdee.bos.transaction.springframework.core com.kingdee.bos.transaction.springframework.dao ...

    基于springMVC注解的简单例子

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.spring...

    asm5.0.jar

    2013-08-12 14:33:37.672:WARN::Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/E:/cloudwave-core/src/main/...

    SpringBoot整合Redis

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.spring...

Global site tag (gtag.js) - Google Analytics