我使用Spring Boot 0.5.0.M5进行项目设置.
在我尝试的其中一个配置文件中,@Autowire Environment
但是失败了NullPointerException
.
这是我到目前为止所拥有的:
Application.java
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
JpaConfig.java我想去的地方@Autowire Environment
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.ui.persistence.repository")
public class JpaConfig {
private static final String DATABASE_DRIVER = "db.driver";
private static final String DATABASE_PASSWORD = "db.password";
private static final String DATABASE_URL = "db.url";
private static final String DATABASE_USERNAME = "db.username";
private static final String HIBERNATE_DIALECT = "hibernate.dialect"; …
Run Code Online (Sandbox Code Playgroud) Properties
我没有在构造函数内部使用,而是看到@Value
注解可以解决问题。
它可以在我拥有的另一个实现中工作,但在这里没有
控制器:
@Autowired
private Customer customer;
Run Code Online (Sandbox Code Playgroud)
我的课
@Service
public class Customer {
/** The soap GW endpoint. */
@Value("${soapGWEndpoint}")
private String soapGWEndpoint;
/** The soap GW app name. */
@Value("${soapGWApplName}")
private String soapGWAppName;
/** The soap GW user. */
@Value("${generic.user}")
private String soapGWUser;
/** The soap GW password. */
@Value("${generic.user.password}")
private String soapGWPassword;
public Customer () {
// All parameters are null:
login(soapGWEndpoint, soapGWAppName, soapGWUser, soapGWPassword);
}
}
Run Code Online (Sandbox Code Playgroud)
但是它们位于application.properties文件中。
为什么在这种情况下我不能使用@Value?