开发者

Spring, create a bean from deserializing

开发者 https://www.devze.com 2023-01-25 15:13 出处:网络
I\'ve not used Spring too much, so I might be missing something obvious. Anyway, this is the question:

I've not used Spring too much, so I might be missing something obvious. Anyway, this is the question:

Say you have a Spring managed bean that is a networking client. After you call a method on it you get some object back, say it is a List

Now I want to process that List into something more useful for the rest of my application, say it is of a MyBusinessBean type.

How do I avoid doing a

new MyBusinessBean(List<String> info) 

?? If I do this it becomes an object that is not part of the context.

I'm doing something like:

MyB开发者_Python百科usinessBean myBean = (MyBusinessBean) applicationContext.getBean("myBusinessBean", info);

However I keep reading everywhere that this is a signal of something not being done properly, since I am making my code context aware. What is the proper Spring way of doing this?


The second way is what you do in Spring. Calling "new" means you're breaking the model.

But don't assume that every call to "new" is eliminated by Spring. Every object need not be under Spring's control. Sometimes you just gotta call "new", usually in the scope of a single method. Create the object, use it, GC it - no Spring bean factory needed.

If you want to give an object in your app access to that List after serialization, simply pass the reference. You don't need the Spring bean factory in the situation you've described, because you've already got the serialized the object from the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号