SSH Integration (ii)--using spring to assemble the action class

Source: Internet
Author: User



In the previous article (SSH integration (i)-direct access to ApplicationContext),in theActionclass by getting theApplicationContextobject, calling the Getbean method to instantiate theBeanclass, this method willActionclass andSpringThe binding assemblyJavaBeanThe tightening of the bindings greatly increases their coupling degree. If the contents of the assembly file change, it is likely to be modifiedActionthe code for the class.


So we'll put the method of assembling the action class and its properties into spring as well.


One, the action object is created by spring


For example, the action code is as follows:

Package Net.blogjava.nokiaguy.models;public class Secondsshaction {private Mapservice service;public void Setservice ( Mapservice service) {this.service = service;} Public String Execute () {System.out.println (Service.validate ("LHC")); return null;}}

Where Mapservice is a business logic bean.


Configure this type of assembly in spring:

<!--assembly Action class--><!--spring default scope is a singleton mode, each time an action object is created, each access is the same action, the data is unsafe, and after adding scope= "prototype", To ensure that the party has a request, create an Action object--><bean id= "test" class= "Net.blogjava.nokiaguy.models.SecondSSHAction" scope= " Prototype "><property name=" service "><ref bean=" Mapservice "/></property></bean>

configure this action in Struts.xml:


<action name= "test" class= "test"/><!--here the class value is configured as the ID of the corresponding bean in the spring configuration file--

Advantages:

make full use of Spring of the IOC of the features, the more perfect will Action class and ApplicationContext the object is decoupled.

Disadvantages:

This approach requires simultaneous Spring in the configuration file and Struts.xml file, configure the same Action class, which will cause the code to be bloated and difficult to maintain.


Two, automatically assemble the properties of the Action object


In this configuration, we only need to configure the Struts.xml file.

For example:

<!--assemble this class using the Mapservice from the spring configuration file to assemble--><action name= "thirdssh" class= " Net.blogjava.nokiaguy.models.ThirdSSHAction "/>  


When we access the http://localhost:8099/SSHDemo02/thirdssh.action in the browser, because we have previously configured:


<!--assembly Business Logic Layer components--><bean id= "Mapservice" class= "Net.blogjava.nokiaguy.models.MapService" >< constructor-arg><!--The constructor method's parameter Mapdao object into the business logic layer--><ref bean= "Mapdao"/></constructor-arg></ Bean>


When assembling the Mapservice property of the thirdsshaction, the bean used here will come from the assembly.

This approach changes the configuration of the two configuration file to maintain only one profile, and the components configured in spring can be used by multiple actions.








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SSH Integration (ii)--using spring to assemble the action class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.