开发者

Spring Web Application - How to get from page controllers to business layer

开发者 https://www.devze.com 2023-02-06 05:49 出处:网络
I am writing a Spring web application in Eclipse. In WEB-INF I have a web.xml file that gives a servlet mapping for a myapp-servlet.xml file.

I am writing a Spring web application in Eclipse. In WEB-INF I have a web.xml file that gives a servlet mapping for a myapp-servlet.xml file.

In myapp-servlet.xml, I have a bean for a page controller. I surmise that the Spring DispatcherServlet instantiates these beans from its own ApplicationContext. When I run my project as a server, it displays the index.jsp file - "hello hello hello".

Now I also surmise that were I to put a submit button that sends post data in this index.jsp file, my page controller would be notified of the HTTP request. However, how can I interact with the rest of my application (the business layer) from the page controller? I am writing an LDAP Directory Lookup application, and once the user inputs a name I want to return an email. So, when the user inputs a name and hits submit, I want to communicate with my LDAP business layer. How can I do this?

One way I have thought of doing this is to add my business objects as properties to the page controller in the myapp-servlet.xml file. However, this seems to integrate the business layer and the controllers far too much for my liking - it means that every controller would need properties for business objects.

Another way I have thought of doing this is to create a kind of factory that all contro开发者_StackOverflowllers have listed as a property in their XML. Through this factory, the controllers can access business objects; the factory is the interface between the two layers.

These are both custom ideas, and I suspect that there is a pre-made solution. Is there already a method of going about this? (Integrating business layer with web / page controller layer?) Or is it up to the programmer to concoct a custom solution as I outlined above?

Thanks, (and sorry for the long question - hence the bold)

ktm


Your controller needs to somehow hold a reference to your business objects. To fully benefit of Spring, you will want to inject those dependencies into your controller.

This is not tight coupling, especially if those business objects implement an interface and your controller will only know about that interface.

Dependency injection eliminates the need for factories as you will need to know about the service interfaces with our without a factory:

SomeBusinessServiceInterface service = businessFactory.getBusinessService();

But think about it like this: you must somehow get a reference of some service (using that service interface for low coupling) and most likely you need to cache that -- store it as an instance variable. Since you have that anyway, providing a setter does not couple your web and business tier any more than they already are.

If a setter seems unnatural (and it sometimes is) use constructor injection to implement this as classic OO aggregation.

Regarding the application context config, Spring allows you to define a webapp-wide application context defined using a ContextLoaderListener in web.xml. This is usually where all your business bean definitions should reside. Your -servlet.xml application context is bound to the servlet and should usually contain MVC stuff, referencing the beans in the root context as that context automatically becomes the parent of all the -servlet.xml (therefore all the beans there are visible for your servlets).

0

精彩评论

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

关注公众号