我有2个域模型和一个Spring REST控制器,如下所示:
@Entity
public class Customer{
@Id
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;
// other stuff with getters/setters
}
@Entity
public class Country{
@Id
@Column(name="COUNTRY_ID")
private Integer id;
// other stuff with getters/setters
}
Run Code Online (Sandbox Code Playgroud)
Spring REST控制器:
@Controller
@RequestMapping("/shop/services/customers")
public class CustomerRESTController {
/**
* Create new customer
*/
@RequestMapping( method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public com.salesmanager.web.entity.customer.Customer createCustomer(@Valid @RequestBody Customer customer, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
customerService.saveOrUpdate(customer);
return customer;
}
// other stuff
} …Run Code Online (Sandbox Code Playgroud) 是否可以通过java中的反射向类添加方法?
public class BaseDomain {
public BaseDomain(){
Field[] fields = this.getClass().getDeclaredFields();
for(int i=0; i<fields.length; i++){
String field = fields[i].toString();
String setterMethod = "public void set" + field.toLowerCase();
//Now I want to add this method to this class.
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为DataSource的servive接口和多个实现,如XMLDataSource,DataBaseDataSource等.
我想基于一些用户交互向我的Struts2 Action注入(Spring)适当的实现,比如用户点击XML然后我需要使用XML实现.Spring已被用于DI框架.
@Autowired
private DataSource dataSource;
Run Code Online (Sandbox Code Playgroud)
请建议实现这一目标的最佳方法.