如果您的映射器具有采用已准备好的实例而不是 的方法,则可以添加原型范围的 Bean 并从控制器方法进行调用。Class
UserDomain
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);
}
...
弹簧配置
@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();
}
...
}
否则,您可以使用@Configurable
和AspectJ编译时编织。但是你必须决定是否值得在你的项目中引入编织,因为你有其他方法来处理这种情况。
啪.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>
...
用户域.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;
...
}
弹簧配置
@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;
}
...
}
有关AspectJ和@Configurable的更多信息,请参阅Spring文档。
编辑
关于您的编辑。
请注意,您在那里使用@Autowired
。这意味着实例必须由Spring容器管理。容器不知道在其外部创建的实例,因此对于此类实例,@Autowired
(与@Value
完全相同)将无法解析,例如 或。因此,您仍然必须将原型范围的Bean添加到您的上下文中。实际上,这意味着所提出的方法类似于@Value
相关的方法,除了它将您与Spring Environment
联系起来。因此,这是不好的。UserDomain
UserDomain userDomain = new UserDomain()
UserDomain.class.newInstance()
UserDomain
UserDomain
仍然可以使用环境
和应用程序上下文Aware
来制定更好的解决方案,而无需将域对象绑定到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;
}
}
用户域.java
public class UserDomain {
private Boolean enabled;
public UserDomain() {
this.enabled = ApplicationContextProvider.getEnvironmentProperty("default.user.enabled", Boolean.class, false);
}
...
}
弹簧配置
@Configuration
@PropertySource("classpath:application.properties")
public class Config {
@Bean
public ApplicationContextProvider applicationContextProvider() {
return new ApplicationContextProvider();
}
...
}
但是,我不喜欢这种方法的额外复杂性和草率性。我认为这根本没有道理。