Easy-to-use SSH-spring 01 (IOC) and ssh-springioc

Source: Internet
Author: User

Easy-to-use SSH-spring 01 (IOC) and ssh-springioc

This chapter begins to learn about the final framework spring in SSH. Spring is an open source code design framework. It solves the loose coupling problem between the business logic layer and other layers. Therefore, it runs interface-oriented programming ideas throughout the entire system application.

First, let's take a look at IOC. The advantage of IOC is that it reduces coupling, mainly through the dependency between beans and try to replace them with associations.

Configure a spring program based on the maven project.

First, you can download a spring plug-in: You can search for spring in eclipse-> Help-> Eclipse Marketplace, and find spring tools to download it. Other download methods are not described.

Step 1: Import spring dependency to pom. xml in maven project: spring-context. Take version 4.3.10 as an example.

<! -- Spring-context dependency --> <dependency> <groupId> org. springframework </groupId> <artifactId> spring-context </artifactId> <version> 4.3.10.RELEASE </version> </dependency>

Step 2: Create an xml file named applicationContext. xml in the src/main/resources folder. Add the spring header file, as shown in the following figure.

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> </beans> label description: xmlns = XML Name spacexmlns: About initializing bean format file address xmlns: xsi: Auxiliary initializing beanxsi: context: constraints on spring contexts, including loading resource file xmlns: tx transaction tags xmlns: aop (Aspect-Oriented) Labels xsi: schemaLocation: schema document used to declare the target namespace

Step 3: Create a class for a new person, give the name and age attributes, encapsulate the structure, and use tostring.

Step 4: configure a bean to implement IOC. In applicationContext. xml, the configuration is as follows:

<Bean id = "person" class = "com. entity. Person"> </bean>
// Id is defined as the bean name, and class is the fully qualified path of the class to be configured
// Of course, the name attribute can also be used to replace the id attribute. The name attribute can be defined with special characters.

Step 5: create another class and use the main method or @ Test of junit for testing. The Test code is as follows:

// The objects returned by delayed loading (lazy loading) only have one single column mode. // load the file BeanFactory factory = new XmlBeanFactory (new ClassPathResource ("applicationContext. xml "); obtain the bean object Person p = (Person) factory. getBean ("person"); System. out. println (p );
// Load a bean in time to create multiple objects: ApplicationContext aContext = new ClassPathXmlApplicationContext ("applicationContext. xml "); // obtain the bean object Person p = (Person) aContext. getBean ("person"); System. out. println (p );

The configuration of such a simple bean is complete.

We can add the attributes scope and init-method to the bean.

Scope attribute value:

Singleton (default)

A bean definition in each Spring Ioc container corresponds to an object instance (Singleton Mode)
Prototype
A bean defines multiple object instances.
Request
In an HTTP request, a bean corresponds to an instance, that is, each HTTP request will have its own bean instance, which is created based on a bean definition. This scope is only valid in the case of web-based Spring ApplicationContext.
Session
In an HTTP Session, a bean defines an instance. This scope is only valid in the case of web-based Spring ApplicationContext.
Global session
In a global HTTP Session, a bean defines an instance. In typical cases, it is valid only when the portlet context is used. This scope is only valid in the case of web-based Spring ApplicationContext.

 

The init-method attribute indicates the method called during bean initialization. The attribute value is the method name corresponding to bean in this class.

Next, let's look at the injection method:

First, add the property tag in the bean tag. The name corresponds to the property name and the value corresponds to the property value.

1. For example, set the default value for the property of this object.

<Bean id = "person" class = "com. entity. Person"> <! -- Default attribute setting --> <property name = "name" value = "Haha"> </property> <property name = "age" value = "11"> </property> </bean>

2. Inject a set (set, list). Take list as an example.

Add an lsit <string> attribute to the persion class to encapsulate

Injection:

<Bean id = "person" class = "com. entity. person "> // ls variable name for list <string> <property name =" ls "> <list> <value> Google </value> <value> Haha </value> <value> Xi </value> </list> </property> </bean>

3. Construct Injection

// Constructor injection requires corresponding constructor in the object class
<Bean id = "person" class = "com. entity. person "> <constructor-arg name =" name "value =" 1 "> </constructor-arg> <constructor-arg name =" age "value =" 18 "> </constructor-arg> </bean>

4. Injection object

Create an object class card, give the cid and cname attributes, and encapsulate the structure.

Add a bean to applicationContext. xml.

    <bean id="card" class="com.entity.Card" >    </bean>

Add the persion object property to the card class and encapsulate it.

Then inject it into the bean of the card

<Bean id = "card" class = "com. entity. card "> // name object variable name <property name =" person "> // ref ID of the attribute class in bean <ref bean =" person "/>
// The function of the idref element is similar to that of the <value> element, except that the idref element has more verification functions.
<! -- <Idref bean = "person"/> --> </property> </bean>

I did not try it here. In addition to the above injection, you can also look at the static factory method injection and the instance factory method injection.

This chapter ends!

 

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.