关于使用Spring框架创建实例的问题?
这里有一个需要从 Spring 表单填充的命令对象
public class Person {
private String name;
private Integer age;
/**
* on-demand initialized
*/
private Address address;
// getter's and setter's
}
和 Address
public class Address {
private String street;
// getter's and setter's
}
现在假设以下 MultiActionController
@Component
public class PersonController extends MultiActionController {
@Autowired
@Qualifier("personRepository")
private Repository<Person, Integer> personRepository;
/**
* mapped To /person/add
*/
public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Person person) throws Exception {
personRepository.add(person);
return new ModelAndView("redirect:/home.htm");
}
}
因为 Person 的 Address 属性需要按需初始化,所以我需要重写 newCommandObject 来创建一个实例初始化地址属性的人。否则,我会得到 NullPointerException
@Component
public class PersonController extends MultiActionController {
/**
* code as shown above
*/
@Override
public Object newCommandObject(Class clazz) thorws Exception {
if(clazz.isAssignableFrom(Person.class)) {
Person person = new Person();
person.setAddress(new Address());
return person;
}
}
}
好吧,专家 Spring MVC 和 Web Flow 说
替代对象创建的选项包括从 BeanFactory 中提取实例或使用方法注入透明地返回新实例。
第一个选项
- 从 BeanFactory 中提取实例的
可以写为
@Override
public Object newCommandObject(Class clazz) thorws Exception {
/**
* Will retrieve a prototype instance from ApplicationContext whose name matchs its clazz.getSimpleName()
*/
getApplicationContext().getBean(clazz.getSimpleName());
}
但是他想通过使用方法注入透明地返回一个新实例来表达什么???你能展示我如何实现他所说的吗???
ATT:我知道这个功能可以通过 SimpleFormController 而不是 MultiActionController 来实现。但是它只是作为示例显示的,没有其他内容
Here goes a command object which needs to be populated from a Spring form
public class Person {
private String name;
private Integer age;
/**
* on-demand initialized
*/
private Address address;
// getter's and setter's
}
And Address
public class Address {
private String street;
// getter's and setter's
}
Now suppose the following MultiActionController
@Component
public class PersonController extends MultiActionController {
@Autowired
@Qualifier("personRepository")
private Repository<Person, Integer> personRepository;
/**
* mapped To /person/add
*/
public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Person person) throws Exception {
personRepository.add(person);
return new ModelAndView("redirect:/home.htm");
}
}
Because Address attribute of Person needs to be initialized on-demand, i need to override newCommandObject to create an instance of Person to initiaze address property. Otherwise, i will get NullPointerException
@Component
public class PersonController extends MultiActionController {
/**
* code as shown above
*/
@Override
public Object newCommandObject(Class clazz) thorws Exception {
if(clazz.isAssignableFrom(Person.class)) {
Person person = new Person();
person.setAddress(new Address());
return person;
}
}
}
Ok, Expert Spring MVC and Web Flow says
Options for alternate object creation include pulling an instance from a BeanFactory or using method injection to transparently return a new instance.
First option
- pulling an instance from a BeanFactory
can be written as
@Override
public Object newCommandObject(Class clazz) thorws Exception {
/**
* Will retrieve a prototype instance from ApplicationContext whose name matchs its clazz.getSimpleName()
*/
getApplicationContext().getBean(clazz.getSimpleName());
}
But what does he want to say by using method injection to transparently return a new instance ??? Can you show how i implement what he said ???
ATT: I know this funcionality can be filled by a SimpleFormController instead of MultiActionController. But it is shown just as an example, nothing else
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定他的意思是使用 Spring 参考手册第 3 章
唯一的缺点是
需要 no arg方法而不是MultiActionController
的newCommandObject(Class)
方法。这可以通过以下方式解决:
在上下文文件中:
缺点是使用这种东西是你有点坚持通过 xml 配置控制器 bean 不可能(当然在 <3 中)使用注释来做到这一点。
I'm pretty sure he means using the
lookup-method
system as documented in chapter 3 of the spring reference manualOnly down side is that
<lookup-method>
requires a no arg method rather than thenewCommandObject(Class)
method ofMultiActionController
.this can be solved with something like:
in the context file:
The down side is that using this sort of thing is you are kinda stuck with configuring the controller bean via xml it's not possible (certainly in < 3) to do this with annotations.