How Will the loading sequence
Do not paste online reply , I have searched a lot
look a bit messy
I am currently aware of
web.xml read cont-param and listener
first contparam spring file reads the classpath
then load the listener, the action introduced bo excuse to get ?
this a little less clear
then automatically injected relevant , I do not know the order
problem : when the application starts it is the first or the first spring struts
In addition there is no specific detail , I want to know is how the various parts of convergence
Or , when injected
join me in a good multi- points , for a good number of spring configuration file with the
layer is called layer
I'm starting to ask when a request is how to find the corresponding entry in the xml file which begins with these app ( injection ) it
or some other mechanism ? This old confused
If each search, efficiency is too low
so sure what I do not understand
people look clear answer , so that the younger one can understand .
not clear , do not mess hope to answer .
grateful
------ Solution ---------------------------------- ----------
easiest way to configure an SSH framework for yourself and then look at how to write a method to go , spring is a container , the key is how to load web struts.xml process has in the course of the action of some of the interceptors, they are responsible for calling the action method to set or get the value of the corresponding property injection , and I had the most confused , after using hibernate, with no database any relationship , and bearing in mind !
------ Solution ---------------------------------------- ----
ah ...... the problem should be checked when the Spring xml parsing . Do not allow the same file the same beanid, which is to check the xml parsing mechanism spring .
two files with the same id of the bean, then the spring will be injected twice. Is a continuous injection of the same object twice , so there is nothing wrong happened .
However, it is easy to get so confusing . Developers themselves easily deceives himself dizzy , changed forgetting to change one another .
If two files have the same id of bean, but injected with different content , then the final content is injected behind the equal is covered.
You can experiment , the two bean, use the same id and class, for the same object inject different content.
order issue involved here spring loaded configuration file.
spring loaded configuration file , if you are pointing out the spring configuration, separated by a comma , then according to the order you specify to load.
However, if the use of a wildcard , then alphabetically loaded .
------ For reference only -------------------------------------- -
by ...... I have dedicated this issue did note ...... count your lucky , free to share to you ......
but had spent
------ For reference only me some strength analysis ------------------------ ---------------
first Struts1.x combined with the Spring configuration using the process:
The program through the " automatic injection " way to get the object , also known as " dependency injection " method .
more commonly used , but under certain circumstances can not be used. If two JVM, a run in Changsha, a run in Shenzhen , we can not achieve the injection , (1) "Spring and Struts support environment to build ."
copy Struts, Spring -related packages and offers Struts, Spring configuration
internationalization support , provide default international resource files.
(2) " Configure web.xml"
configuration
configuration
configuration examples :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Resolution:
When the Web application starts , will do the following:
① created by Tomcat a ServletContext, and automatically loads
② ContextLoaderListener detected ServletContext create, run Spring -related procedures ;
③ Spring contextConfigLocation points in accordance
④ Spring BeanFactory create an instance based on the content applicationContext.xml , and put the ServletContext .
In short , the configuration of the role is: When the Web application starts , Spring will automatically create an instance of BeanFactory , and placed the ServletContext .
(3) " configure Struts"
struts-config.xml file, pay attention to the type attribute
without using Spring, type set com.project.LoginAction and other custom classes ; using Spring, type is set to "org.springframework.web.struts.DelegatingActionProxy".
but " still need to build specific Action", and specific methods of preparation of Action . The Action " referenced in the Spring configuration ," see " ( 4 ) Configure Spring".
equivalent to the Struts configuration transferred to the Spring configuration here that is a key point of integration with the Spring Framework Struts combined .
configuration examples :
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.project.usermgr.web.forms.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="org.springframework.web.struts.DelegatingActionProxy"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
(4) " Configure Spring"
by Spring to create and manage Action, Action injecting Model layer to the Service object .
After setting scope = "prototype" allows each thread has a new Action, thus solving Struts1.x of Action thread safety issues.
Note:
① must use the name attribute , and the value of the name attribute must be the path attribute struts-config.xml file
② must configure the Model layer Service objects, such as userManager so on.
configuration examples :
applicationContext-beans.xml:
<bean name="/login" class="com.project.usermgr.web.actions.LoginAction" scope="prototype">
<property name="userManager" ref="userManager"/>
</bean>
(5) " Build Action"
Model Layer Service object of Spring " automatic injection" , so no need to manually build the object , there is no need to get BeanFactory. Gets an object through the " automatic injection " method , the " dependency injection ."
note the related object to provide a set method to facilitate the Spring injection.
public class LoginAction extends Action {
private UserManager userManager;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
userManager.login(username, password);
return mapping.findForward("success");
}
//须提供set方法,以方便Spring注入。
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
------ For reference only ----------------------------------- ----
page -> SERVLET container configuration forwarding -> STRUTS2 capture request -> SPRING business processes ( including things ) -> HIBERNATE data processing - > Back to business processes - > return STRUTS2-> ; page
------ For reference only ------------------------------------ --- combined
Struts2 with Spring's very simple .
Note Struts2 configuration in web.xml :
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/ *</url-pattern>
</filter-mapping>
Struts2 using a filter. Only in action when used , will read Struts2 configuration , significantly Spring first start .
landlord, asked to build mechanisms and Spring injection mechanism is actually very simple .
Spring after the listener is started by reading the Spring configuration file applicationContext.xml. If you have multiple profiles, will all be read. Note that the " All" read , no matter how many .
Spring configuration file , basically
Here is a little attention to scope = "prototype" of the situation .
① scope = "singleton", the default value. As the name suggests , a single case .
② scope = "prototype", each will generate a new instance of the object obtained from the IoC container every time "different" .
If you use a non- thread-safe class , consider using prototype.
with other objects , prototype at the outset will be instantiate an object out . When this object is used , Spring will then generate an additional object into the map , the other threads for use .
In addition , Spring AOP mechanisms as well . AOP that white is the agent. One is a dynamic proxy java , there is a succession CGLIB proxy . After these proxy classes , and those objects as ioc container , are also completed in the Spring configuration read immediately created.
Spring configuration, things are beginning to be created was good , " repeated find " problem does not exist.
------ For reference only -------------------------------------- -
see a post from the perspective of Spring source code analysis of the operating mechanism of Spring :
http://graykeel.iteye.com/blog/696984
excerpt epilogue reads as follows :
whole bean generation process :
1: generate a configuration file xml format
2: By ResourceLoader reads the configuration file
3: By BeanDefinitionReader configuration information into the process BeanDefinition
4: Create a bean instance in accordance BeanDefinition information through BeanFactory
5: SingletonBeanRegistry placed in the bean instance for use
------ For reference only ---------------------- -----------------
really lucky today , ah, thank you very much brother's wonderful answer . Greatly appreciated. Glance ah. Today is my lucky day ah.
I understand it look right
always regardless of spring with the manner in which the bean is instantiated
regardless of their order , in short, are put away
As for Action layer , coupled with the listener, the class action referenced bo layer also injected into action
inside
then struts responsible request, the request came, went into action, which has been used with the injection of class
may also not injected , to hell with it, execute the line ,
------ For reference only --------------- ------------------------
say injected under ...... object , as long as the configuration is correct, will definitely be injected successfully . Not injected successfully , inevitably error. For example, the bean name wrong , or the target object does not exist , and so on .
In addition , zhaolibin147 brothers right. Struts2 also involves injecting property. But property injection Struts2 is only when accessing the Action execution .
equivalent number of interceptors Filter, which has several interceptors responsible for injecting property. So Struts attributes inject relatively late . Action is
And because each thread will generate an instance , so the properties are being generated with the Action injected .
simply, after being injected Struts2 properties, such as request, session, access to these objects is initiated by the client , before being injected to create action .
------ For reference only -------------------------------------- -
Well thank supplement , but I also found a problem
If the same app_xx. When xml, write two different bean, is the same id, that would be wrong
But if , in two xml file and write it on the inside of the same bean id attributes are the same,
without error , the error will not start
I landed a simple test it, did not feel there is an error
It then creates two bean id of the same ?
------ For reference only -------------------------------------- -
Thank you brother, your answer is very exciting.

------ For reference only ---------------------------------------

------ For reference only ------ ---------------------------------
1 floor to answer the wonderful ah

------ For reference only ---------------------------------------
good, powerful, learned < br> ------ For reference only ---------------------------------------
good brothers, learn ,
------ For reference only -------------------------- -------------
today only see really good answer , so I need a clear answer. . Thank
没有评论:
发表评论