Spring (1), Spring (

Source: Internet
Author: User

Spring (1), Spring (

1. What is spring?

We know that struts is a web Framework (jsp/action/actionfrom ). Hibernate is An orm framework in the persistent layer.

Spring isContainer framework, UsedConfigure bean, And dimensionRelationship between bean Protection.

Important Concepts in spring:

Bean(Is any type of java object javabean/service/action/Data Source./dao

Ioc(Control inversion of control)

Di(Dependency injection)

 

II,Quick Start

Develop a spring Project.

1. Introduce the spring development kit (minimum configuration of spring. jar this package includes common jar, but also write the log package common-logging.jar

2. create a core file (container file) applicationContext of spring. xml, [hibernate has the core hibernate. cfg. xml struts Core File struts-config.xml], this file is generally placed in the src directory, the file introduces xsd file: can copy from the given case.

3. Configure bean

<! -- Configure bean (service/dao/domain/action/Data Source) in the container file -->

<! -- The bean element is used to automatically create a bean object and put it into the memory when our spring framework is loaded.

UserService userSerivce = new UserService ();

UserSerivce. setName ("Han shunping ");

-->

<Bean id = "userService" class = "com. service. UserService">

<! -- The concept of injection is shown here. -->

<Property name = "name">

<Value> Han shunping </value>

</Property>

</Bean>

4. In Test. java, how do we use

// Now we use spring to complete the above tasks

// 1. Obtain the spring applicationContext object (container object)

ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext. xml ");

UserService us = (UserService) ac. getBean ("userService ");

Us. sayHello ();

Discussion:

Traditional methods and use of springMethod

1. When spring is used and there is no new object, we hand over the task of creating the object to the spring framework.

2.SpringOperating principle:

Summary:

Spring is actually a container framework. It can configure various bean (action/service/domain/dao) and maintain the relationship between bean and bean. When we need to use a bean, we can use getBean (id.

IocWhat is it?

A: ioc (inverse of controll) controls inversion: the so-called control inversion refers to creating an object (bean) and maintaining an object (bean) from the program to the spring container (applicationContext. xml), and the program itself is not maintained.

DIWhat is it?

A: di (dependency injection) dependency injection: In fact di and ioc are the same concept. spring designers believe that di represents spring's core technologies more accurately.

Note:: Applicationcontext is a heavyweight Object, so you need to make ApplicationcontextSingleton.

 

3. spring and Interface Programming

Spring Development AdvocatesInterface ProgrammingWith di technology, layers can be decoupled.

Example:

Now let's try spring'sDiInterface ProgrammingTo convert uppercase and lowercase letters:

Use the interface call method in the business layer, and change the configuration in the Spring container to switch between different interface implementation methods to change the business logic without changing the business layer code.

Through the above case, We can initially realize that di works with interface programming, which can indeed reduce the coupling between the layer (web layer) and the business layer.

 

4. From ApplicationContextGet bean from application context containerAnd from beanRetrieve bean from factory containers

Case study:

// Retrieve bean from ApplicationContext

ApplicationContext ac = new ClassPathXmlApplicationContext ("com/mK/ioc/beans. xml ");

// When beans. xml is instantiated, the bean configured in this file is retrieved from the bean by the instance (the bean scope is singleton ).

// If we use beanfactory to obtain the bean, when you just instantiate the container, then

// The bean of the container is not instantiated. It is created in real time only when you use a certain bean of getBean.

BeanFactory factory = new XmlBeanFactory (new ClassPathResource ("com/mK/ioc/beans. xml "));

Factory. getBean ("student ");

Conclusion:

1. If ApplicationContext is usedSingleton(Not singletonNo)It is instantiated no matter whether you use it or not. (The advantage is that it can be preloaded, but the disadvantage is that it consumes memory)

2. If it is BeanFactory, the configured bean will not be instantiated immediately when you get beanfacotry. It will only be instance when you use it (the advantage is memory saving, but the disadvantage is speed)

3. Regulations: Generally, ApplicatioContext should be used to complete

 

Three methods for obtaining ApplicationContextObject Reference Method

1. ClassPathXmlApplicationContext-> use the class path

2. FileSystemXmlApplicationContext-> file path

Example:

ApplicationContext ac = new FileSystemXmlApplicationContext ("file path beans. xml/applicationContext. xml ");

D: \ myspringsrc \ com \ MK \ ioc \ beans. xml must use an absolute path.

3. XmlWebApplicationContext load when tomcat is started

 

5. bean Lifecycle

Why is a life cycle always a focus?

Servlet-> servlet lifecycle init () destory ()

① Instantiate (when our program loads the beans. xml file) and instantiate our bean (provided that scope = singleton) to the memory (spring container)

② Call the set Method To set Properties

③ If you implement the BeanNameAware interface, you can get the ID number (bean name) through setBeanName)

④ If you implement the BeanFactoryAware interface, you can obtain the BeanFactory

For the singleton mode bean Factory, each bean is created once.

The bean factory in prototype mode is created only once.

⑤ If you implement the ApplicationContextAware interface, call the Method

⑥If the bean is associated with a postprocessor, the Object postProcessBeforeInitialization method is automatically called.

7. If you implement the InitializingBean interface, afterPropertiesSet will be called.

If the handler is in <bean init-method = "init"/>, you can define your own Initialization method in bean.

BytesIf the bean is associated with a postprocessor, the Object postProcessAfterInitialization method is automatically called.

Using our bean

Among them, the most valuable is the section-Oriented Programming of segment 6. EquivalentFilter. After the association is configured, it will be valid for all beans.

 

11. Container close

12. Call the destory method by implementing the DisposableBean interface.

13. You can call the custom destruction method in <bean destory-method = "fun1"/>

 

Summary:In actual development, we often do not use such a process. The common ones are:

1-> 2-> 6-> 9-> 10-> 11

 

Problem:BeanFactory is used to obtain bean objects. Is the bean lifecycle the same as Applicationcontext?

 

Not the same. bean is the life cycle created in the factory, which is simpler:

 

 

 

 

 

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.