我无法访问控制器或服务中的import tensorflow as tf
from tensorflow import keras
import numpy as np
training_inputs = np.array([[0,0,1],
[1,1,1],
[1,0,1],
[0,1,1]])
training_outputs = np.array([[0,1,1,0]])
model = keras.Sequential([
keras.layers.Flatten(input_shape=(1,3)),
keras.layers.Dense(1,activation="sigmoid")
])
model.compile(optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = ["accuracy"])
model.fit(training_inputs,training_outputs,epochs=1)
prediction = model.predict(np.array([[1,1,0]]))
或事件@Value("${app.version}")
或任何属性。
我的项目结构如下
environment.getProperty("app.version")
我的UiAppInitializer非常简单,
src/main/java
-configuration/
AppConfig.java
EnvConfig.java
JpaConfig.java
UiConfig.java
ServicesConfig.java
UiAppInitializer.java
-repositories/
....
-models/
....
-services/
....
-controllers/
....
返回getRootConfigClassess()
,而AppConfig.class
返回getServletConfigClasses()
AppConfig.java
UiConfig.class
EnvConfig
@Configuration
@Import({
EnvConfig.class,
UiConfig.class,
ServicesConfig.class
})
public class AppConfig{}
一个简单的控制器
@Configuration
public class EnvConfig implements InitializingBean {
@Value("${app.version}")
private String appVersion
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
pc.setLocations(new ClassPathResource("application.properties"));
return pc;
}
@Override
public void afterpropertiesSet() throws Exception {
log.debug("App Version is " + appVersion);
}
}
@RestController
@RequestMapping(value = "/version")
public class VersionContoller {
@Value("${app.version}")
private String version;
@GetMapping()
public String getVersion() {
return version;
}
}
控制器仅返回“ $ {app.version}”,但是@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"my.packages.path.ui"
})
public class UiConfig implements WebMvcConfigurer {
....
}
正确地记录了版本。
我在这里做错了什么?我还有其他控制器已成功连接到存储库,该控制器是在JpaConfig中设置的,它也为所有属性使用afterpropertiesSet
注意不使用Spring Boot
答案 0 :(得分:0)
似乎在properties()
bean被调用setLocations()
之前,控制器已被初始化为bean。
您可以删除类路径扫描(我假设您已找到它来找到控制器bean?),然后在EnvConfig中声明一个新方法,该方法是传入String版本的Controller的bean声明。显然也需要更改控制器构造函数
@Configuration
public class EnvConfig implements InitializingBean {
@Value("${app.version}")
private String appVersion
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
pc.setLocations(new ClassPathResource("application.properties"));
return pc;
}
@Bean
public VersionContoller controller() {
return new VersionController(appVersion);
}
@Override
public void afterpropertiesSet() throws Exception {
log.debug("App Version is " + appVersion);
}
}