关于使用Spring框架创建实例的问题?

发布于 2024-09-04 23:03:20 字数 2118 浏览 4 评论 0原文

这里有一个需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-09-11 23:03:20

我很确定他的意思是使用 Spring 参考手册第 3 章

唯一的缺点是 需要 no arg方法而不是 MultiActionControllernewCommandObject(Class) 方法。

这可以通过以下方式解决:

public abstract class PersonController extends MultiActionController {

    /**
      * code as shown above
      */

    @Override
    public Object newCommandObject(Class clazz) thorws Exception {
        if(clazz.isAssignableFrom(Person.class)) {
            return newPerson();
        }
    }                          

    public abstract Person newPerson();
}

在上下文文件中:

<bean id="personController" class="org.yourapp.PersonController">
  <lookup-method name="newPerson" bean="personPrototype"/>
</bean>

缺点是使用这种东西是你有点坚持通过 xml 配置控制器 bean 不可能(当然在 <3 中)使用注释来做到这一点。

I'm pretty sure he means using the lookup-method system as documented in chapter 3 of the spring reference manual

Only down side is that <lookup-method> requires a no arg method rather than the newCommandObject(Class) method of MultiActionController.

this can be solved with something like:

public abstract class PersonController extends MultiActionController {

    /**
      * code as shown above
      */

    @Override
    public Object newCommandObject(Class clazz) thorws Exception {
        if(clazz.isAssignableFrom(Person.class)) {
            return newPerson();
        }
    }                          

    public abstract Person newPerson();
}

in the context file:

<bean id="personController" class="org.yourapp.PersonController">
  <lookup-method name="newPerson" bean="personPrototype"/>
</bean>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文