`

spring 注入static属性

 
阅读更多
网上好多方法都是错误的,google之,发现
 
 
http://stackoverflow.com/questions/11324372/how-to-make-spring-inject-value-into-a-static-field 写道

You have two possibilities:

  1. non-static setter for static property/field;
  2. using org.springframework.beans.factory.config.MethodInvokingFactoryBean to invoke a static setter.

In the first option you have a bean with a regular setter but instead setting an instance property you set the static property/field.

publicvoid setTheProperty(Object value){
    foo.bar.Class.STATIC_VALUE = value;}

but in order to do this you need to have an instance of a bean that will expose this setter (its more like an workaround).

In the second case it would be done as follows:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"><property name="staticMethod" value="foo.bar.Class.setTheProperty"/><property name="arguments"><list><ref bean="theProperty"/></list></property></bean>

On you case you will add a new setter on the Utils class:

publicstatic setDataBaseAttr(Properties p)

and in your context you will configure it with the approach exemplified above, more or less like:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"><property name="staticMethod" value="foo.bar.Utils.setDataBaseAttr"/><property name="arguments"><list><ref bean="dataBaseAttr"/></list></property></bean>

 其中方法二 应该比较实用,就是配置xml,配置staticMethod,以及instance

 

 

 

也可以这么玩

http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field 写道
publicclassSample{publicstaticString name;@PostConstructpublicvoid init(){
        name = privateName;}@Value("${my.name}")privateString privateName;publicString getPrivateName(){return privateName;}publicvoid setPrivateName(String privateName){this.privateName = privateName;}}

 但是下面有大神说了  Firs of all, public static non-final fields are evil. Spring does not allow injecting to such fields for a reason.    

 

because   Non-final means you can modify the field value, which, for a static field, implies handling thread concurrency - a.k.a. pain in the stack.

 

 

 

 

 

 

 

0
1
分享到:
评论
2 楼 cainiao1923 2016-12-14  
java-lxm 写道
写文章贴个图也不好好贴


....难道是网站的问题,编辑的时候是OK的啊
1 楼 java-lxm 2016-12-14  
写文章贴个图也不好好贴

相关推荐

    Spring boot工具类静态属性注入及多环境配置详解

    Spring Boot工具类静态属性注入及多环境配置详解 Spring Boot工具类静态属性注入是指使用Spring Boot框架时,如何将配置信息注入到工具类的静态变量中,以便在程序中使用这些配置信息。这种方式可以方便地在不同的...

    spring依赖注入bean

    `ref` 属性指定了要注入的 Bean 的 ID。 **注解驱动的配置** 除了 XML 配置,Spring 也支持使用注解进行配置。这使得代码更加简洁且易于理解。我们可以在类上使用 `@Component` 注解声明一个 Bean,然后使用 `@...

    spring-实例工厂注入

    在Spring框架中,实例工厂注入是一种创建对象的方式,它允许我们通过自定义的工厂方法来控制对象的实例化过程。这种方式对于那些初始化过程复杂或者需要特殊处理的对象特别有用。下面我们将详细探讨Spring中的实例...

    static静态变量使用@Value注入方式.md

    ### static静态变量使用@Value注入方式 #### 一、引言 在Java开发中,特别是基于Spring框架的应用程序中,开发者经常会遇到需要为类中的成员变量注入外部配置值的情况。Spring框架提供了多种注入机制,其中`@Value...

    知识共享-spring属性注入(雷惊风).

    标题与描述中的关键词“知识共享-Spring属性注入”聚焦于Spring框架中的一种核心特性——依赖注入(Dependency Injection,DI)。Spring框架通过DI机制,能够自动地管理对象间的依赖关系,从而简化了Java应用的开发...

    SpringBoot中使用类型安全的配置来注入大量自定义属性示例源码

    Spring Boot提供了类型安全的配置注入机制,使得我们可以方便、准确地管理这些属性,避免了传统方式中可能出现的错误和麻烦。本示例源码将详细介绍如何在Spring Boot中使用这种机制。 首先,让我们理解什么是类型...

    特殊情况(ActionForm,Servlet, Filter, Listener)下Spring如何注入对象

    针对这个问题,一种解决办法是将注入的属性声明为静态,并修改setter方法使其返回非void类型。例如: ```java // ActionForm中注入对象的静态声明 private static IDaoService daoService; // 修改setter方法,...

    Spring依赖注入

    2. **属性注入**:通过setter方法进行依赖注入。这是最常见的依赖注入方式之一。 3. **字段注入**:通过直接在类的字段上添加注解(如`@Autowired`)来实现依赖注入。这种方式虽然简单,但不利于单元测试。 #### 四...

    Spring注入Date类型的三种方法总结

    -- 使用SpEL表达式注入Date对象 --&gt; &lt;bean id="datebean" class="com.springDemo1.Date类型注入.DateBean"&gt; &lt;property name="birthday"&gt; &lt;util:constant static-field="java.util.Calendar.DAY_OF_MONTH" /&gt; ...

    在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息

    2. 如果你的类是Spring的一部分,可以通过`@Autowired`注解注入`ApplicationContext`,然后在需要的地方使用它。 3. 或者,你可以在初始化阶段,通过静态工厂方法或单例模式获取并保存`ApplicationContext`,以便...

    spring.net 资料整理

    Spring.NET 提供了多种依赖注入方式,如构造函数注入、属性注入和方法注入。例如,可以修改 `PersonDao` 类以接受一个依赖的构造参数: ```csharp public class PersonDao : IPersonDao { private readonly ...

    pro spring 中文版

    Spring框架通过其IoC容器实现了依赖注入,极大地简化了Java应用程序的开发过程。通过Spring,开发者可以更专注于业务逻辑的实现,而不用过多关注对象的创建和依赖管理。此外,Spring的XML配置支持使得配置Bean及其...

    Spring的简单实例代码

    Spring框架是Java开发中广泛应用的一个轻量级框架,它提供了依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)等核心特性,极大地简化了企业级应用的开发。本实例将详细介绍...

    Spring Boot技术知识点:如何获取application.yml配置文件里的相关属性(方法2)

    此外,Spring Boot还提供了其他获取配置属性的方式,如`@Value`注解、`Environment`接口、`ConfigurableEnvironment`和`PropertySourcesPlaceholderConfigurer`等。每种方法都有其适用场景,开发者可以根据具体需求...

    spring第一个例子

    4. **运行程序**:最后,我们运行`MainApp`,可以看到程序输出配置的属性值,证明Spring已经成功地创建并注入了bean。 在博客文章"http://blog.csdn.net/u010397369/article/details/16892103"中,作者可能更详细地...

    spring项目启动加载类到静态服务类中的三种方式.zip

    Spring Boot提供了一种更简洁的方式来注入配置属性或bean,即使用`@Autowired`注解。但是,由于静态方法无法直接使用注解,我们可以创建一个非静态的辅助类,该类可以被Spring管理,并在其内部使用`@Autowired`注解...

    spring 在vs控制台程序下的简单使用demo

    Spring以其依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)而闻名。依赖注入允许开发者在运行时动态地将依赖关系注入到对象中,而不是硬编码这些依赖关系。这使得代码更...

    Spring中如何动态注入Bean实例教程

    Spring 中如何动态注入 Bean 实例教程 Spring 框架中提供了多种方式来注入 Bean 实例,但是在实际项目中,基于动态的方式来提取 Spring 管理的 Bean 是非常有价值的。本文将详细介绍动态注入 Bean 实例的方式和...

    SPRING面试宝典

    - **属性值**:如果是setter注入,则需要定义属性值。 - **依赖关系**:Bean与其他Bean之间的依赖关系。 **3.3 如何向Spring容器提供配置元数据?** 配置元数据可以通过以下几种方式提供给Spring容器: - **XML...

    springIOC实例

    这里的`id`是bean的唯一标识,`class`指定了bean的实现类,`ref`属性则表示`Service` bean的`repository`属性应注入`Repository` bean。 Spring容器读取这些配置后,会创建bean实例,并通过依赖注入(Dependency ...

Global site tag (gtag.js) - Google Analytics