jsc*_*man 6 java spring spring-mvc
我有这个问题,我不知道如何解决.我使用Spring Boot创建了Restful API,我正在实现DTO-Domain-Entity模式,所以在这种特殊情况下我有这个控制器的方法
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<UserResponseDTO> createUser(@RequestBody UserRequestDTO data) {
UserDomain user = this.mapper.map(data, UserDomain.class);
UserDomain createdUser = this.service.createUser(user);
UserResponseDTO createdUserDTO = this.mapper.map(createdUser, UserResponseDTO.class);
return new ResponseEntity<UserResponseDTO>(createdUserDTO, HttpStatus.CREATED);
}
public class UserDomain {
private Long id;
private Date createdDate;
private Date updatedDate;
private String username;
private String password;
@Value("${default.user.enabled:true}") // I have default-values.properties being loaded in another configuration file
private Boolean enabled;
}
Run Code Online (Sandbox Code Playgroud)
我正在将UserRequestDTO对象转换为UserDomain.据我所知,UserRequestDTO是一个正在注入的bean.然后我将其转换为UserDomain,这里的问题是UserDomain对象不是组件,因此enabled属性不会采用默认值.
在我不想将UserDomain作为bean处理的情况下,我怎样才能使spring加载默认值(在这种情况下只启用属性)?
这不是相同的答案,因为我的目标是使用@Value注释完成它.
无论如何,康斯坦丁建议,做这样的事情会更好吗?
public class UserDomain {
@Autowired
private Environment environment;
private Boolean enabled;
UserDomain(){
this.enabled = environment.getProperty("default.user.enabled");
// and all the other ones
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的映射器有一个方法来取代已经准备好的实例Class,那么您可以添加原型范围的UserDomainbean并context.getBean()从控制器方法调用.
调节器
...
@Autowired
private WebApplicationContext context;
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<UserResponseDTO> createUser(@RequestBody UserRequestDTO data) {
UserDomain user = this.mapper.map(data, getUserDomain());
UserDomain createdUser = this.service.createUser(user);
UserResponseDTO createdUserDTO = this.mapper.map(createdUser, UserResponseDTO.class);
return new ResponseEntity<UserResponseDTO>(createdUserDTO, HttpStatus.CREATED);
}
private UserDomain getUserDomain() {
return context.getBean(UserDomain.class);
}
...
Run Code Online (Sandbox Code Playgroud)
弹簧配置
@Configuration
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propConfigurer = new PropertySourcesPlaceholderConfigurer();
propConfigurer.setLocation(new ClassPathResource("application.properties"));
return propConfigurer;
}
@Bean
@Scope("prototype")
public UserDomain userDomain() {
return new UserDomain();
}
...
}
Run Code Online (Sandbox Code Playgroud)
否则,您可以使用@Configurable和AspectJ编译时编织.但是你必须决定是否值得在你的项目中引入编织,因为你有其他方法来处理这种情况.
的pom.xml
...
<!-- additional dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.6</version>
</dependency>
...
<!-- enable compile-time weaving with aspectj-maven-plugin -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<encoding>UTF-8</encoding>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<Xlint>warning</Xlint>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
Run Code Online (Sandbox Code Playgroud)
UserDomain.java
@Configurable
public class UserDomain {
private Long id;
private Date createdDate;
private Date updatedDate;
private String username;
private String password;
@Value("${default.user.enabled:true}")
private Boolean enabled;
...
}
Run Code Online (Sandbox Code Playgroud)
弹簧配置
@EnableSpringConfigured与<context:spring-configured>.相同.
@Configuration
@EnableSpringConfigured
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propConfigurer = new PropertySourcesPlaceholderConfigurer();
propConfigurer.setLocation(new ClassPathResource("application.properties"));
return propConfigurer;
}
...
}
Run Code Online (Sandbox Code Playgroud)
有关AspectJ和@Configurable的更多信息,请参阅Spring文档.
关于你的编辑.
请注意,您@Autowired在那里使用.这意味着UserDomain实例必须由Spring容器管理.容器不知道在其外部创建的实例,因此@Autowired(完全如此@Value)将不会为此类实例解析,例如UserDomain userDomain = new UserDomain()或UserDomain.class.newInstance().因此,您仍然需要将一个原型范围的UserDomainbean 添加到您的上下文中.实际上,这意味着所提出的方法类似于@Value相关方法,除了它将您UserDomain与Spring 联系起来Environment.因此,这很糟糕.
使用Environment和ApplicationContextAware不将域对象绑定到Spring 仍然可以创建更好的解决方案.
ApplicationContextProvider.java
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static <T> T getEnvironmentProperty(String key, Class<T> targetClass, T defaultValue) {
if (key == null || targetClass == null) {
throw new NullPointerException();
}
T value = null;
if (applicationContext != null) {
System.out.println(applicationContext.getEnvironment().getProperty(key));
value = applicationContext.getEnvironment().getProperty(key, targetClass, defaultValue);
}
return value;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
Run Code Online (Sandbox Code Playgroud)
UserDomain.java
public class UserDomain {
private Boolean enabled;
public UserDomain() {
this.enabled = ApplicationContextProvider.getEnvironmentProperty("default.user.enabled", Boolean.class, false);
}
...
}
Run Code Online (Sandbox Code Playgroud)
弹簧配置
@Configuration
@PropertySource("classpath:application.properties")
public class Config {
@Bean
public ApplicationContextProvider applicationContextProvider() {
return new ApplicationContextProvider();
}
...
}
Run Code Online (Sandbox Code Playgroud)
但是,我不喜欢这种方法的额外复杂性和邋iness性.我认为这根本没有道理.
| 归档时间: |
|
| 查看次数: |
9429 次 |
| 最近记录: |