Spring for beginners in Java ssh article 4, sshspring

Source: Internet
Author: User
Tags logitech keyboard

Spring for beginners in Java ssh article 4, sshspring

Today, I will study Bean in Spring.

In Spring, there are still <beans... /> tag. It is equivalent to <bean... /> tag's dad can have many sons, but he only has one.

That is to say, a <beans.../> label can contain multiple <bean...> labels, and each <bean.../> label represents a java instance.

When defining <bean...>, pay attention to two attributes: id and class.

Id is the unique identifier of the <bean.../> label. The access, management, and injection operations of containers are completed by this attribute, so it is also unique.

The class is the concrete implementation class of the bean. Because Spring needs to instantiate the bean through the class, it must be a complete class name, not an interface!

You can also use the name attribute to add an alias to it.

When Spring creates a bean instance, it also specifies a specific scope for it:

Singleton: singleton mode. The bean defined by singleton has only one instance.

Prototype: prototype. An instance is created every time the bean defined by prototype is called through the getbean method of the container.

Request: Each http request generates a bean instance.

Seesion: similar to request, a bean instance is generated every time an http session request is sent.

Global session: (this is not quite understandable) each global http session corresponds to a bean instance.

If no scope is specified, the default mode is singleton.

For example: <bean id = "test" class = "..." scope = "singleton"/>

The scope of the session and request is very similar. The difference is that the session indicates that each Http Session request will be valid, while the request indicates that each Http request is valid.

Therefore, we need two Configuration Methods: Listener or Filter.

When using a Web Container with the specifications above Servlet2.4, you can add the following configuration in the Web. xml file of the web container. The Listerner is responsible for the request scope:

1 <web-app>2      ...3       <listener>4             <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>5       </listener>6      ...7 </web-app>

If the following 2.4 specification is used, this configuration cannot be used. You can only change it to Filter configuration:

 1 <web-app> 2     ... 3     <filter> 4         <filter-name>requestContextFilter</filter-name> 5         <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> 6         <filter-mapping> 7             <filter-name>requestContextFilter</filter-name> 8             <url-pattern>/*</url-pattern> 9         </filter-mapping>10     </filter>11     ...12 </web-app>

Once either of the above is configured, the request and session scopes can be used in the Spring configuration file.

  

If you want to set the Bean property value to another bean instance in the container, you must use the <ref...> element. Using this element has two attributes:

Bean: reference the id attribute value of another bean instance that is not in the same XML file.

Local: reference the id attribute values of other bean instances in the same XML file. For example:

1 <bean id = "A" class = "com. basic. test. A "/> 2 <bean id =" B "class =" com. basic. test. B "> 3 <property name =" c "> 4 <! -- Reference another bean in the container --> 5 <ref local = "A"/> 6 </property> 7 </bean>

It also has a simple Syntax:

1 <bean id="A" class="com.basic.test.A"/>2 <bean id="B" class="com.basic.test.B">3     <property name="c" ref="A"/>4 </bean>

Here, the ref element is concise and incorporated into the property of the property. Of course, this write will not distinguish the local and bean attributes in the ref element.

In addition, if the bean property value is a set, you need to use the set element, <list... />, <set... />, <map... /> and <props... /> Set the Set attribute values of the type List, Set, Map, and Properties respectively.

Next we will transform a previously written com/sep/basic/service/impl/Lenovo class, which will contain multiple set attributes.

1 package com. sep. basic. service. impl; 2 3 import java. util. list; 4 import java. util. map; 5 import java. util. properties; 6 import java. util. set; 7 8 import com. sep. basic. service. computer; 9 import com. sep. basic. service. key; 10 11 public class Lenovo implements Computer {12 // below is the Set attribute 13 private List <String> size; 14 private Set price; 15 private Map <String, Key> key; 16 private Properties health; 17 // private Key key; 18 19 // setter Method 20 public void setSize (List <String> size) {21 this. size = size; 22} 23 24 25 public void setPrice (Set price) {26 this. price = price; 27} 28 29 30 public void setKey (Map <String, Key> key) {31 this. key = key; 32} 33 34 35 public void setHealth (Properties health) {36 this. health = health; 37} 38 39 @ Override40 public void useKey () {41 System. out. println (size); 42 System. out. println (price); 43 System. out. println (key); 44 System. out. println (health); 45} 46 47}

Let's take a look at the configuration code of applicationContext. xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans 3 xmlns = "http://www.springframework.org/schema/beans" 4 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 5 xmlns: p = "http://www.springframework.org/schema/p" 6 xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 <! -- Configure the Lenovo instance --> 8 <bean id = "lenovo" class = "com. sep. basic. service. impl. lenovo "> 9 <property name =" size "> 10 <! -- Configure attribute values for the list attribute --> 11 <list> 12 <! -- Configure a List element for each value, ref, and bean --> 13 <value> 17' </value> 14 <value> 21' </value> 15 <value> 30' </value> 16 </list> 17 </property> 18 <property name = "key"> 19 <! -- Set the property value for set --> 20 <Set> 21 <! -- Configure a Set element for each value, ref, and bean --> 22 <value> common string </value> 23 <bean class = "com. sep. basic. service. impl. logicTech "/> 24 <ref local =" logicTech "/> 25 </set> 26 </property> 27 <property name =" cpu "> 28 <! -- Set the attribute value for the Map type --> 29 <map> 30 <! -- Configure a key-value pair for each entry --> 31 <entry key = "cpu1" value = "intel"/> 32 <entry key = "cpu2" value = "amd "/> 33 <entry key = "cpu3" value = "other"/> 34 </map> 35 </property> 36 <property name = "keyType"> 37 <! -- Set the attribute value for the Map type --> 38 <map> 39 <! -- Configure a key-value pair for each entry --> 40 <entry key = "Logitech keyboard" value-ref = "logicTech"/> 41 </map> 42 </property> 43 <property name = "health"> 44 <! -- Set the property value for the properties type --> 45 <! -- Configure an attribute for each prop element, the key specifies the property name --> 46 <props> 47 <prop key = "temperature"> normal </prop> 48 <prop key = "access speed"> normal </prop> 49 </props> 50 </property> 51 </bean> 52 <! -- Configure the LogicTech instance --> 53 <bean id = "logicTech" class = "com. sep. basic. service. impl. LogicTech"/> 54 55 </beans>

In this way, all set attributes are assigned successfully.


Struts hibernate spring SSH learning sequence?

There is no sequence in learning this framework. Only prerequisite.
Hibernate must learn JDBC well
Spring must be good at java
Struts mainly controls the UI Layer.
It will be much better to grasp these prerequisites and learn more.
These three frameworks are not necessarily associated, so as long as the foundation is good, they will be the same after learning.
Hibernate: Background and database
Spring: used for encapsulation and injection.
Struts: foreground Control
This is probably the case.
 
How to Understand ssh in java

With the gradual maturity and improvement of Java technology, as a standard platform for building enterprise-level applications, the J2EE platform has been greatly developed. Using multiple technologies included in the J2EE specifications: Enterprise JavaBean (EJB), Java Servlets (Servlet), Java Server Pages (JSP), Java Message Service (JMS), etc, many application systems have been developed. However, some problems also occur during the development of traditional J2EE applications: 1) Conflicts between data models and logical models. Currently, the databases used are basically relational databases, while Java is essentially an object-oriented language. When objects are stored and read, SQL and JDBC are used for database operations, reduces programming efficiency and system maintainability; 2) Traditional J2EE applications mostly adopt the EJB-based heavyweight framework, which is suitable for developing large-scale enterprise applications, however, it takes a lot of time to use the EJB container for development and debugging. To reduce code coupling and improve system development efficiency, this paper proposes a J2EE application development strategy based on the Struts and Hibernate frameworks.

Data Persistence Layer and Hibernate

Hibernate is a data persistence layer framework. It is a tool that implements O/R Mapping between objects and relationships. It encapsulates JDBC lightweight objects, this allows programmers to use object programming to operate databases. It not only provides ing from Java classes to data tables, but also provides data query and recovery mechanisms. Compared with using JDBC and SQL to operate databases, using Hibernate can greatly improve the implementation efficiency. The Hibernate framework defines the ing between a Java object and a data table in the form of a configuration file. At the same time, it further interprets the relations between data tables as inheritance and inclusion relationships between Java objects. Using HQL statements to describe complex relational algorithms in the way of objects, greatly simplifying data query and accelerating development efficiency. In Hibernate, there is a simple and intuitive API used to query the Objects represented by the database. To create or modify these objects, you only need to interact with them in the program and then tell Hibernate to save them. In this way, a large number of business logic for encapsulating persistent operations no longer need to write cumbersome JDBC statements, which greatly simplifies the data persistence layer.
Use Struts to implement the MVC Architecture

Proposed by Trygve Reenskaug, MVC (Model-View-Controller) is first applied in the SmallTalk-80 environment and is the basis of interaction and interface systems. According to the requirement of Interface Design variability, MVC divides the composition of the Interaction System into three parts: model, view, and controller.

Model is an internal abstraction of the problem logic processed by the software independent of the content and form displayed externally. It encapsulates the computing relationship between the core data, logic, and functions of the problem, independent of specific interface expressions and I/O operations. View displays information and specific forms of model data and logical relationships and States to users. It obtains the display information from the model and can have multiple different display forms or views for the same information. A Controller is used to process interactions between users and software. Its responsibility is to control the transmission of any changes in the model and ensure the corresponding connection between the user interface and the model; it accepts user input and reports the input to the model to achieve computing control over the model. It is a component that coordinates the model and view. A view usually corresponds to a controller. The separation of models, views, and controllers allows a model to have multiple display views. If you change the model data through the controller of a view, all other views dependent on the data should reflect the changes. Therefore, the Controller will notify all views of the changes at any time, resulting in display updates. This is actually a model change-propagation mechanism.
The Struts framework was first launched as an integral part of the Apache Jakarta project. It inherits the full text...>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.