In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the bean statements of Spring IOC". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the bean statements of Spring IOC?"
Introduction to Spring
Spring does not serve the development of web project functionality, or business. Instead, it serves the development of the project, facilitates the decoupling of each layer, facilitates the batch management of classes, and is a framework to improve the efficiency of software development and reduce the cost of maintenance in the later stage.
The core idea of Spring is IOC (inversion of Control) and AOP (faceted programming).
IOC: instead of requiring programmers to explicitly New an object, the Spring framework uses the objects created by the framework. Because they are created by the spring framework, the objects are saved in the spring framework object, also known as the spring container, so that spring knows which objects have been created in the current project, which layer the object belongs to, and how to manage it. The first thing you want to use the other features of spring is to use spring's objects, also known as handing control to spring management.
AOP: uniformly manage all classes under a certain path, or classes or methods with common characteristics, and add new functions before and after the execution of the original task. Make a series of unified accompanying actions such as monitoring, initialization, arrangement, destruction and so on.
If you've been working on Java programming for a while, you may find (and you may actually use it) that many frameworks tie applications to frameworks by forcing applications to inherit their classes or implement their interfaces. This intrusive programming approach can be seen in earlier versions of Struts and countless other Java specifications and frameworks. Spring strives to avoid messing up your application code with its own API. Spring does not force you to implement interfaces to the Spring specification or classes that inherit from the Spring specification. On the contrary, in applications built on Spring, its classes usually have no trace that you are using Spring. In the worst-case scenario, a class might use Spring annotations, but it is still POJO.
Any meaningful application (certainly more complex than the Hello World example) consists of two or more classes that collaborate with each other to complete specific business logic. Traditionally, each object is responsible for managing references to the objects it collaborates with (that is, the objects it depends on), which leads to highly coupled and difficult-to-test code.
IOC declares Bean
The Maven Poject is created first. The detailed package structure is as follows
Among them, AOP will explain in the next article.
Controller_.java
/ * *
* @ see HttpServlet#doGet (HttpServletRequest request, HttpServletResponse response)
* /
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/ / TODO Auto-generated method stub
Response.getWriter () .append ("Served at:") .append (request.getContextPath ())
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext ("/ ApplicationContext.xml")
Service_ s = (Service_) ctx.getBean ("service_Impl1_new")
System.out.println (s)
S.show ()
}
Since Spring cannot be demonstrated alone, Controller_.java creates a Servlet, directly calls the doPost or doGet method, implements Service, outputs the Service_ object s, and executes the show method.
Service_.java
Public interface Service_ {
Public void show ()
}
Create a Service interface to implement Spring.
1. No-parameter constructor declaration bean
Service_Impl1.java
Public class Service_Impl1 implements Service_ {
Public Service_Impl1 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service1- Parametric Construction method")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl1")
}
}
The output of the show method of rewriting Service_ implements Service_Impl1 and writes no-parameter construction methods.
ApplicationContext.xml
You only need to set the Service_Impl1,id corresponding to id and class,class is a parameter in the getBean called by Controller_.java. For the running result, please see the custom constructor injection bean.
two。 Custom constructor declaration bean
Service_Impl2.java
Public class Service_Impl2 implements Service_ {
Public Service_Impl2 (int a) {
/ / TODO Auto-generated constructor stub
System.out.println ("service2- Custom Construction parameters:" + a)
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl2")
}
}
ApplicationContext.xml
This is the parameter setting in the constructor. Index, as its name implies, means index, where a parameter is the 0th and value is the value of the parameter.
3. Single instance lazy loading declaration bean
Service_Impl3.java
Public class Service_Impl3 implements Service_ {
Public Service_Impl3 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service3- lazy load single instance")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl3")
}
}
ApplicationContext.xml
Lazy-init= "true" sets lazy loading, that is, bean is loaded only when called, not automatically; scope= "singleton" scope tag, that is, only one instance is created for a single instance.
4. Parameter reference declaration bean
Service_Impl4.java
Public class Service_Impl4 implements Service_ {
Service_ s3
Public Service_ getS3 () {
Return s3
}
Public void setS3 (Service_ S3) {
This.s3 = S3
}
Public Service_Impl4 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service4- parameter references bean")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl4")
}
}
ApplicationContext.xml
The parameter tag, name, is the bean to be referenced by the parameter s3jineref link in Service_Impl4.
5. Initialization property declaration bean
Service_Impl5.java
Public class Service_Impl5 implements Service_ {
String name
ArrayList list
HashMap map
HashSet set
@ Override
Public int hashCode () {
Final int prime = 31
Int result = 1
Result = prime * result + ((list = = null)? 0: list.hashCode ())
Result = prime * result + ((map = = null)? 0: map.hashCode ())
Result = prime * result + ((name = = null)? 0: name.hashCode ())
Result = prime * result + ((set = = null)? 0: set.hashCode ())
Return result
}
@ Override
Public boolean equals (Object obj) {
If (this = = obj)
Return true
If (obj = = null)
Return false
If (getClass ()! = obj.getClass ())
Return false
Service_Impl5 other = (Service_Impl5) obj
If (list = = null) {
If (other.list! = null)
Return false
} else if (! list.equals (other.list))
Return false
If (map = = null) {
If (other.map! = null)
Return false
} else if (! map.equals (other.map))
Return false
If (name = = null) {
If (other.name! = null)
Return false
} else if (! name.equals (other.name))
Return false
If (set = = null) {
If (other.set! = null)
Return false
} else if (! set.equals (other.set))
Return false
Return true
}
@ Override
Public String toString () {
Return "Service_Impl5 [name=" + name + ", list=" + list + ", map=" + map + ", set=" + set + "]"
}
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
Public ArrayList getList () {
Return list
}
Public void setList (ArrayList list) {
This.list = list
}
Public HashMap getMap () {
Return map
}
Public void setMap (HashMap map) {
This.map = map
}
Public HashSet getSet () {
Return set
}
Public void setSet (HashSet set) {
This.set = set
}
Public Service_Impl5 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service5- initialization Properties")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl5")
}
}
The initialization parameters include list,map,set and normal parameters. The hashCode and equals methods are overridden. For more information, please see HashMap memory leak. The toString method is used to output initialization properties.
ApplicationContext.xml
QQQ
WWW
one hundred and eleven
Which gynecology hospital in Zhengzhou has a good http://www.sptdfk.com/
It is used in the map tag for assignment. Others normally use property and value for assignment.
6. The initialization property reference method returns the value declaration bean
Service_Impl6.java
Public class Service_Impl6 implements Service_ {
String s5_toString
Public String getS5_toString () {
Return s5_toString
}
Public void setS5_toString (String s5_toString) {
This.s5_toString = s5_toString
}
Public Service_Impl6 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service6- calling method return value")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl6 return value" + s5_toString)
}
}
The toString method of Service_Impl5 is called and output is made.
ApplicationContext.xml
Fixed is used to declare the return value of the calling method.
Reference the bean of the target
Method of referencing the target
7. Static factory-declare factory bean
Service_Impl7.java
Public class Service_Impl7 implements Service_ {
Public static Service_ StaticFactory (int num) {
Switch (num) {
Case 1:
Return new Service_Impl1 ()
Case 2:
Return new Service_Impl2 (100)
Case 3:
Return new Service_Impl3 ()
Case 4:
Return new Service_Impl4 ()
Case 5:
Return new Service_Impl5 ()
Default:
Return new Service_Impl6 ()
}
}
Public Service_Impl7 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service7- static factory")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl7")
}
}
The static factory uses the switch statement in the implementation class to simulate, and the static factory adds the static keyword before the method to call the other implementation class methods above.
ApplicationContext.xml
Use constructor injection methods to assign values; factory-method= "StaticFactory" (method name of the factory-method factory)
8. Instance factory-declaration factory bean
Service_Impl8.java
Public class Service_Impl8 implements Service_ {
Public Service_ factory1 (int num) {
Switch (num) {
Case 1:
Return new Service_Impl1 ()
Case 2:
Return new Service_Impl2 (100)
Case 3:
Return new Service_Impl3 ()
Case 4:
Return new Service_Impl4 ()
Case 5:
Return new Service_Impl5 ()
Default:
Return new Service_Impl6 ()
}
}
Public Service_Impl8 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service8- instance Factory")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl8")
}
}
ApplicationContext.xml
To create an instance factory bean, first create an instance factory's bean, and then create a factory method's bean to call the factory's bean.
To call the bean of the factory method when calling, call service_Impl8_new here
9. Comment declaration bean
@ Service: used to annotate business layer components
@ Controller: used to annotate control layer components (such as action in struts)
Repository: used to annotate the data access component, that is, the DAO component
@ Component (value= "*"): generally refers to components. We can use this annotation to annotate components when they are not easy to classify.
Service_Impl9.java
@ Service
Public class Service_Impl9 implements Service_ {
Public Service_Impl9 () {
/ / TODO Auto-generated constructor stub
System.out.println ("service9- comments are injected into bean")
}
@ Override
Public void show () {
/ / TODO Auto-generated method stub
System.out.println ("Service_Impl9")
}
}
@ Service declares bean (comments can only declare no-parameter constructors. Bean declared by default with annotations is lowercase of the class name. The id of the declared bean here should be service_Impl9.
ApplicationContext.xml
Annotations need to be scanned with annotations, where base-package is the directory scanned, and the root directory of the project is generally used. if you use SpringMVC later, you don't have to scan Controller.
Comments are written to bean
@ Resource (name= "" type= "") bean write
@ Autowired/@Qualifier
@ inject/@named
At this point, I believe you have a deeper understanding of "what are the bean statements of Spring IOC?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.