来自自动装配对象的弹簧变量注入

时间:2015-07-29 06:28:52

标签: java spring spring-mvc

我有一个这样的课 -

@Service
public class SomeClass {

@Autowired
Environment env;

private String property;

@Value("${pty}")
public void setPty(String pty) {
    pty = environment.getProperty("pty");
   } 
}

我正在尝试从自动装配的另一个类'Environment'中注入'pty'变量,当我的服务器启动时我得到了这个异常

  

创建名为'someClass'的bean时出错:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire方法:public void   service.facade.ActionFacade.setPty(java.lang.String中);   嵌套异常是java.lang.IllegalArgumentException:不能   解决字符串值“$ {pty}”

中的占位符'pty'

2 个答案:

答案 0 :(得分:1)

异常是因为Spring上下文中没有属性pty@Value查找占位符' pty'在加载的资源文件中。

在您的情况下,它不是必需的,因为您需要从已Environment的{​​{1}}课程中获取它,下面的代码会给您提供这个想法。

Autowired

答案 1 :(得分:0)

尝试@PostConstruct

  

PostConstruct注释用于在完成依赖注入以执行任何初始化之后需要执行的方法。

ConnectionResult cannot be resolved to a variable   AnalyticsMessages.java  /mixpanel-android/java/com/mixpanel/android/mpmetrics      

GooglePlayServicesUtil cannot be resolved   AnalyticsMessages.java  /mixpanel-android/java/com/mixpanel/android/mpmetrics   

IntDef cannot be resolved to a type Tweaks.java /mixpanel-android/java/com/mixpanel/android/mpmetrics   

The attribute value is undefined for the annotation type IntDef Tweaks.java /mixpanel-android/java/com/mixpanel/android/mpmetrics

The container 'Android Dependencies' references non existing library '/Users/myUser/Downloads/mixpanel-android-4.6.2/src/main/bin/mixpanel-android.jar' 

The import android.support cannot be resolved   Tweaks.java /mixpanel-android/java/com/mixpanel/android/mpmetrics   

The import com.google cannot be resolved    AnalyticsMessages.java  /mixpanel-android/java/com/mixpanel/android/mpmetrics

The project cannot be built until build path errors are resolved