如何使春天注入值转换为静态字段如何使春天注入值转换为静态字段(How to make spring

2019-06-14 14:05发布

我知道这可能看起来像以前问的问题,但我在这里面临着不同的问题。

我有一个只有静态方法的实用程序类。 我不知道,我不会从它带到一个实例。

public class Utils{
    private static Properties dataBaseAttr;
    public static void methodA(){

    }

    public static void methodB(){

    }
}

现在我需要Spring来填补dataBaseAttr与数据库属性Properties.Spring配置为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<util:properties id="dataBaseAttr"
        location="file:#{classPathVariable.path}/dataBaseAttr.properties" />
</beans>

我已经做了它在其他豆类但在这个类(utils的)这里的问题是不是豆腐,如果我让一个bean没有什么变化我现在还不能使用变量,因为该类将不能被实例化和可变始终等于空。

Answer 1:

你有两种可能性:

  1. 非静态设置器为静态属性/场;
  2. 使用org.springframework.beans.factory.config.MethodInvokingFactoryBean调用静态的制定者。

在第一个选项,你有定期的setter豆而是设置实例属性设置静态属性/场。

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

但为了做到这一点,你需要有一颗豆,将暴露此setter的一个实例(它更像是一个解决办法 )。

在第二种情况下,将进行如下:

 <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> 

在你的情况下,您将添加对新二传Utils类:

public static setDataBaseAttr(Properties p)

并在您的环境下,你将与上述举例,或多或少类似的方法进行设置:

 <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> 


Answer 2:

我也有过类似的要求:我需要一个Spring管理仓库的bean注入到我的Person (如“一些与身份”,例如JPA实体“实体”)的实体类。 一个Person的实例有朋友,这Person实例返回它的朋友的,应当委托给它的存储库和查询朋友。

@Entity
public class Person {
    private static PersonRepository personRepository;

    @Id
    @GeneratedValue
    private long id;

    public static void setPersonRepository(PersonRepository personRepository){
        this.personRepository = personRepository;
    }

    public Set<Person> getFriends(){
        return personRepository.getFriends(id);
    }

    ...
}

@Repository
public class PersonRepository {

    public Person get Person(long id) {
        // do database-related stuff
    }

    public Set<Person> getFriends(long id) {
        // do database-related stuff
    }

    ...
}

所以,我怎么会是注入PersonRepository单到的静态字段Person班?

我创建了一个@Configuration ,它获取在Spring的ApplicationContext施工时间回升。 这@Configuration获取与所有那些我需要为静态字段注入到其他类豆类注入。 然后用@PostConstruct注解,我抓住一个钩子做的所有静态字段注入逻辑。

@Configuration
public class StaticFieldInjectionConfiguration {

    @Inject
    private PersonRepository personRepository;

    @PostConstruct
    private void init() {
        Person.setPersonRepository(personRepository);
    }
}


文章来源: How to make spring inject value into a static field