In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Guice integrates Struts2. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Brief introduction to Guice
Guice is a DI framework launched by Google, which won the Jolt Award for its excellence. Compared with Spring, it is much lighter. The runtime only needs to specify a configuration class, implement its com.google.inject.Module interface, specify the interface and the implementation class.
The required basic jar packages are
The jar package required by Guice. If it is a web application, guice-servlet-2.0.jar is required, and guice-struts2-plugin-2.0.jar is needed to integrate Struts2.
Aopalliance.jar
Guice-2.0.jar
Guice-servlet-2.0.jar
Guice-struts2-plugin-2.0.jar
Jar package required by Struts2
Commons-fileupload-1.2.1.jar
Commons-io-1.3.2.jar
Commons-logging-1.0.4.jar
Freemarker-2.3.13.jar
Ognl-2.6.11.jar
Struts2-core-2.1.6.jar
Xwork-2.1.2.jar
Comparison between Guice and Spring
Spring
Guice
Use XML
Using to isolate the relationship between classes into xml, the container is responsible for injecting the called object, so it is called dependency injection
Do not use xml, isolate the relationship between classes to Module, where the name needs to be injected, the container injects the called object according to the description in Module.
Use Annotation
Use
Support custom Annotation annotations, for the same interface definition of object references, for them marked with different custom Annotation comments, you can achieve the same class within the same interface reference, injection to different implementations, in Module with annotations to make a distinction, greatly increased flexibility.
Using Annotation is not necessarily a good thing, nor are new features such as paradigms. At present, most servers do not support jdk1.5,wls until 9 years ago, and current customers seldom choose wls9 due to price reasons, at least not in the projects we have done. No matter how powerful the function is, customers don't need it, so what's the use?
Operation efficiency
When loading spring configuration files, you need to parse xml, which is inefficient and getBean is not efficient. However, getBean is not involved in the use environment, getBean is only used in production environment, and all injections have been completed when spring applications are loaded, so the problem of low efficiency is not a problem.
Using Annotation,cglib, one of the most obvious differences between high efficiency and spring is that spring injects all the places that should be injected when loading the spring configuration file, while Guice injects it when it is used, with high efficiency and flexibility.
Class coupling degree
The degree of coupling is low, emphasizing that the class is non-intrusive, dealing with dependencies in an externalized way, the inside of the class is very clean, writing in the configuration file, the dependence on the class is very low.
High, code-level annotations, DI tags @ inject intrude into the code, coupled to the class level, not only intrusive, simply aggressive, the code is coupled with too much guice, which greatly deviates from the original intention of dependency injection, and is disadvantageous to the maintainability and readability of the code.
When the class is written
Need to write xml, configure Bean, configure injection
Just declare it @ inject and wait to be injected
* declare the injection method in a unified Module
Only IOC is supported
No, spring has dabbled in a lot of parts so far
Yes, it's just a DI container right now.
Is it easy for code refactoring?
Unified xml configuration portal, easy to change
The configuration work is done in Module, which is similar to spring.
Support multiple injection methods
Constructor, setter method
Field, constructor, setter method
Flexibility
1. If references to the same interface definition need to be injected into different implementations, you have to write different Module, which is cumbersome.
2, dynamic injection
If you want to inject an implementation, you still don't know, what to do? spring has no way to write dead in the configuration file beforehand, while Guice can do it, that is to say, I don't know who to inject the object I want to inject. I can only get the implementation of this interface at run time, so this greatly improves the flexibility of dependent injection, dynamic injection.
Integration with existing framework
1, Gao, many existing excellent frameworks (such as struts1.x, etc.) provide the integration entrance of spring, and spring is not only dependency injection, but also many aspects.
2. Spring also provides the integration of Hibernate, etc., which can greatly simplify the difficulty of development.
3. There are many interfaces for orm,rmi,webservice and so on, and the system is huge.
1. You can integrate with the existing framework, but it's a bit difficult to replace spring just by relying on a slightly more efficient DI.
Configuration complexity
It is not difficult to locate the relationship between classes in xml.
It is a little more difficult to locate the relationship between classes at the code level.
2Helloworld
The author wrote a demo, which is much more convenient than Spring for simple applications.
└─ com
└─ greysh
└─ guice
├─ action
│ UserAction.java
│
├─ config
│ ModuleConfig.java
│
├─ model
│ User.java
│
└─ service
│ UserService.java
│
└─ impl
UserServiceImpl.java
For the code, see attachment.
The principle of Guie loading is to first call the class that implements the Module interface, which is similar to Spring's applicationcontext. Then call the binder of the configure function to bind, which is equivalent to the ref binding in spring, and then inject the specified @ Inject that needs to be injected after binding, including constructor injection, method injection and field injection. Generally speaking, method injection is used.
The User for this tutorial is an entity class with only one field userName
Public interface UserService {void invoke (User user);}
For interface implementation
Public void invoke (User user) {user.setUserName ("Name:" + user.getUserName ())
Of course, the most important thing is action
@ Injectprivate UserService userService;@Injectprivate User user
This tutorial uses field injection
Since Struts2's own DI is not used, it should be specified as guice
Thank you for reading! This is the end of the article on "how Guice integrates Struts2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.