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

Spring + SpringMVC + Druid + JPA (Hibernate impl) gives you a secure back-end solution

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1. Random talk on open source projects adopted

Spring's fascinating dependency injection feature makes it firmly at the top of the list of open source projects referenced by JavaEE projects.

Adhering to the principle of low coupling and high cohesion, the idea of object factory decoupling class relationship advocated by Spring has gone deep into the mind of every engineer.

SpringMVC as the godson of Spring, what fascinates me most is her strong ability to expand, as deep as the sea.

The front end, whether it is the freemarker/velocity/jsp..., back-end DAO layer, whether it is the traditional ORM or the new domain model.

Her attitude is consistent, giving you 360 degrees of the most intimate care, there is a person to you like this, this life is enough.

Official website address: http://projects.spring.io/spring-framework/

Dependency on SpringMVC + Spring in the project:

Org.springframework spring-webmvc 4.3.3.RELEASE org.springframework spring-orm 4.3.3.RELEASE org.aspectj aspectjweaver 1.8.9

What I want to say above is AspectJ. AspectJ is one of the earliest and more powerful AOP implementations.

In the Java domain, many of the syntax structures in AspectJ have basically become the standard in the AOP domain.

Spring also has its own Spring-AOP,Spring-AOP to generate proxy classes at run time, and the underlying layer can choose JDK or CGLIB dynamic proxy.

Generally speaking, AspectJ enhances the classes you want to cut into at compile time, while Spring-AOP is a class that is enhanced by proxy classes at run time, so you can imagine its efficiency and performance.

So Spring has been supporting AspectJ since 2.0, and now it's perfectly integrated with AspectJ in the 4.X era.

If you are interested, you can read on: https://www.oschina.net/translate/comparative_analysis_between_spring_aop_and_aspectj?cmp

Druid is written by Alibaba's technical team, and I think it is one of the better database connection pools, especially the monitoring part is my favorite.

Official github address: https://github.com/alibaba/druid/wiki/ FAQ

Web.xml configuration, monitoring configuration and monitoring interface in the project:

DruidStatView com.alibaba.druid.support.http.StatViewServlet DruidStatView / druid/*

As an ORM specification introduced by Sun, JPA is like JDBC to all kinds of database-driven Jar.

Never mind what kind of database you use, just use the canonical method provided by JDBC to play with the code.

JPA formulates the persistence layer specification, with the same and abstract interface, and has a specific implementation layer of the ORM framework.

Sun wants to achieve the unification of ORM technology, maybe in the near future, you don't have to worry about choosing what kind of ORM framework.

The existing popular ORM framework will gradually lose its luster, which is, after all, a long process, let's wait and see.

Back to the top.

two。 An overall overview of the plan

All the classes in the scenario are located in SpringContext and are uniformly managed by Spring.

The premise of unified management of Spring is that you have to tell such a class that it needs to be managed. At present, there are two ways of telling me.

Traditional xml configuration and annotation methods, xml configuration and annotation methods have their own advantages and disadvantages, such as the advantages of xml configuration:

a. If your company's project is referencing another company's jar, the only viable option is xml configuration.

b. If the dependencies between classes change frequently, the xml configuration is excellent, change the code and change the configuration file, both technically and at risk, xml is sure to win the comments.

The advantage of annotated declaration: the code and declaration are together, and you don't have to cut back and forth during development, which is much simpler and clearer than the xml configuration declaration.

Now many mainstream frameworks have introduced annotations, but they can't get rid of xml configuration declaration.

In this scenario, I use clean and simple annotations. Under the controller package, use the annotations @ controller,dao-impl package. Under the @ Repository,service package, use @ service.

The control layer injects the service instance, the service layer injects the data access layer object, the persistence layer object is annotated by JAP, and the page transmits and acquires data through the control layer.

Web.xml:

View Code

Maven pom.xml:

View Code

Back to the top.

3. Various design ideas of DAO layer

The Controller and Service layers are very easy to understand, so I won't go into detail here.

BasePo in the DAO layer wants to abstract some common attributes into the parent class (attributes are determined by specific project requirements).

BasePO

BaseDaoImpl wants to implement some common data access methods in the parent class (I may have a few methods here, which can be added by specific projects).

BaseDaoImpl

Use JAP annotations to write persistence layer objects used by the business.

@ Entity@Table (name = "t_user") public class User extends BasePO {@ Column (nullable = false) String name; @ Column (nullable = false) String pwd

.... getter/setter}

Configure the action of scanning POJO at startup. There are configuration options for new or updated. You can consult the relevant documentation yourself.

${hibernate.hbm2ddl.auto} ${hibernate.dialect} ${hibernate.show_sql} ${hibernate.format_sql} com.rambo.sdh.pojo

Manipulate the most important transaction management of the database, using AOP declaration to intercept before and after the execution of methods with data changes.

The advantage of using AOP declaration to intercept is not to pay attention to the opening and closing of database transactions, but to focus on the business logic.

The general meaning of the above configuration file is that what is executed under the package com.rambo.sdh.service..*Impl.* has been add/save/update. The way to start.

The method is intercepted by HibernateTransactionManager before and after execution, and the transaction is opened and closed.

Of course, there are some other things, you can debug source code to find out if you are interested.

It seems that this solution is a javaweb backend solution, and the front end can use the rendering technology you want to use.

The open source GIT address of the project has been given at the top. If you are interested, you can check out and run locally. In fact, this solution is suitable for small and medium-sized companies, with fast start and development speed.

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

Wechat

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

12
Report