Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate and decompose ssh

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is about how to integrate and decompose ssh. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Spring+Hibernate integration:

Spring integrates Hibernate and makes a big adjustment, because spring can do all the work of managing Hibernate, get rid of the previous hibernate.cfg.xml files, and leave these contents to spring to manage.

1. The following should be configured in the applicationContext.xml file:

Java code

/ / configure data connection class / / configure session factory class org.hibernate.dialect.MySQLDialect true com/hejianjiao/vo/Person.hbm.xml / / configure data connection class / / configure session factory class org.hibernate.dialect.MySQLDialect true Com/hejianjiao/vo/Person.hbm.xml

2. You can use the HibernateDAOSupport and HibernateTemplate classes in spring for data persistence operations:

A, HibernateDAOSupport class defines the operation method and getHibernateTemplate method for session and sessionFactory to obtain a HibernateTemplate instance

B, HibernateTemplate class defines a variety of encapsulation methods for data persistence, which we can use to manipulate the data.

So when using it, we can inherit the HibernateDAOSupport class, and then instantiate the HibernateTemplate class for data persistence.

Second, Spring+Struts2 integration:

1. The context listener configured by spring in the web.xml file:

Java code

ContextConfigLocation / WEB-INF/applicationContext*.xml org.springframwork.web.content.ContextLoaderListener contextConfigLocation / WEB-INF/applicationContext*.xml org.springframwork.web.content.ContextLoaderListener 2, filter configured by struts2 in web.xml file: Java code struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 / * struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 / *

3. By setting the struts.xml file, you can use the IOC of spring to manage the Action of struts:

Java code

4. After the third step is set, when you configure an action in the struts.xml file later, its class is not a class, but the ID of the class defined in the applicationContext.xml file, and you only need to reference the id of the defined class in the struts.xml file.

Then pay special attention to a problem: action is a request that is an action object, but it is not in spring. It automatically allocates instances of classes, uses singleton mode to produce instances of classes, and does not conform to action. Therefore, when defining each action in the applicationContext.xml file, add the following to the class:

Java code

Scope= "prototype" attribute

Scope= "prototype" attribute three, three combined development:

Generally speaking, there is nothing difficult in combinatorial development, as long as the above two steps are done well, it can be three combinatorial development. The above figure shows the system architecture commonly used for combined development:

1. First develop from the * * layer, first develop POJO classes, and Hibernate mapping files. It is equivalent to the database layer of the system.

2. Re-develop the DAO layer, which is a layer for data persistence, specifically dealing with a variety of data add, delete, change, query functions. And use the DAO factory pattern to ensure that there is no connection with the upper layer, and can facilitate the expansion of classes and interfaces.

3. The third is to develop the manager layer, which is equivalent to the business logic layer of the software, that is, it specializes in dealing with all kinds of business logic. Realize the business processing function of the system. And it isolates transactions so that there is no connection between data persistence at the lower level and data operations at the upper level.

4. The Action layer, that is, the presentation layer of the software, handles the reception and reply of action. Each action is managed by spring.

5. Some problems in combinatorial development:

1. In combinatorial development, a common problem is the management of session. When we use HibernateTemplate to operate the database, we can not display the session, and spring can automatically open and close session.

We can configure a session-managed filter shown in the web.xml file that specifically helps us turn off session:

Java code

LazyLoadingFilter org.springframwork.orm.hibernate3.support.OpenSessionInViewFilter lazyLoadingFilter *. Action Note: it must be in front of the struts2 filter. Because the filters of web.xml files are executed sequentially. And session must be in front of it. LazyLoadingFilter org.springframwork.orm.hibernate3.support.OpenSessionInViewFilter lazyLoadingFilter * .action

Note: it must be in front of the struts2 filter. Because the filters of web.xml files are executed sequentially. And session must be in front of it. It will automatically close the session when all the action is processed and the page is displayed.

VI. Spring transaction processing

1. The transaction is also managed by spring. Configure the transaction management class in the applicationContext.xml file:

Java code

/ / bean for implementing transaction management

/ / bean for implementing transaction management

It is managed through sessionFactory, so a sessionFactory is passed in to take over the transaction.

2. Declarative transaction processing

When managing transactions in spring, you can display the definition of transaction processing:

Java code

/ / propagation represents the propagation characteristics of transactions. When required is used, when a method at the beginning of add is detected, it depends on whether there is a transaction opened at this time. If so, put the method into the transaction. If not, create a new transaction. And then put the method in.

/ / if other methods are detected, give them the properties of the read-only database. That is, when this method is being read, other methods can no longer be written. Guarantee the integrity of a transaction. / / attributes added to the transaction

/ / propagation represents the propagation characteristics of transactions. When required is used, when a method at the beginning of add is detected, it depends on whether there is a transaction opened at this time. If so, put the method into the transaction. If not, create a new transaction. And then put the method in.

/ / if other methods are detected, give them the properties of the read-only database. That is, when this method is being read, other methods can no longer be written. Guarantee the integrity of a transaction.

For other propagation properties of the transaction, you can refer to other documents for relevant understanding.

The previous configuration is the setting of transaction methods for all classes in the package. The following paragraph is the definition of, which ensures that the transaction notification defined by 'txAdvice' bean is executed at the appropriate point in the application. First we define a section that matches all the operations defined by the HibernateDAO interface, which we call 'allManagerMethod'. We then bind this aspect to 'txAdvice'' with an advisor, indicating that when 'allManagerMethod' executes, the notification transaction logic defined by' txAdvice' will be executed. This is the AOP section project:

Java code

The Java code / / calls the transaction attribute configured above, which can be given to this aop pointcut. / / if there are other definitions, you can add pointcut and advisor to define the transaction logic of this pointpoint. / / call the transaction attribute configured above, which can be given to this aop pointcut. / / if there are other definitions, you can add pointcut and advisor to define the transaction logic of this pointpoint.

The content in / / expression is to execute an interface of this aspect, and all the methods in it: for example, if the square method for manipulating data is defined in an interface: com.hejianjiao.hibernate.HibernateDAO, then the content in execution parentheses below is: * com.hejianjiao.hibernate.HibernateDAO.* (..). And if there are other classes in the com.hejianjiao.hibernate package that also have operation methods, if we want to define them together, we can write as: * com.hejianjiao.*.* (..), where (..) Represents a method, and the first one is the interface or class of the operation.

The above configuration will create a proxy object for the bean defined by 'HibernateDAO', which is equipped with transaction notifications, so that when its corresponding method is called, a transaction will be started, suspended, marked as read-only, or other (depending on the transaction semantics configured by the method)

The above is how to integrate and decompose ssh. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report