Spring explain (a)

Spring

Reference herein https://www.cnblogs.com/wmyskxz/p/8820371.html

Learn Spring:

Spring advantage

  • Less intrusive / low coupling  (reduce the coupling between the components, software decoupling between the layers)
  • Declarative transaction management (based on section and practices)
  • To facilitate integration with other frameworks (e.g. MyBatis, Hibernate)
  • Java developers reduce the difficulty
  • Spring Framework J2EE platform comprises a layer of each solution (stop)

Spring What can we do

①.Spring  help us based on the profile creation and assembly dependencies between objects .
②.Spring Oriented Programming can help us uncoupled implementation logging, performance statistics, security control.
③.Spring  can be very simple to help us manage the database transaction .
④.Spring  also provides seamless integration with third-party data access framework (such as Hibernate, JPA) , but also provides its own set of JDBC access templates to facilitate database access.
⑤.Spring  also provides a third party the Web (eg Struts1 / 2, JSF) frame seamlessly integrated , but also provides a set himself Spring MVC framework to facilitate web layer structures.
⑥.Spring  can conveniently and Java EE (such as Java Mail, task scheduling) integration , and more technology integration (such as caching framework) .

Spring Modules

 

  • Data Access / Integration layer comprises a JDBC, ORM, OXM, JMS and Transaction module.
  • Web layer contains the Web, Web-Servlet, WebSocket, Web-Porlet module.
  • AOP module provides an implementation of Union standards in line with AOP Aspect-oriented programming.
  • Core Container (core container): with Beans, Core, Context and SpEL module.
  • Test modules support the use of JUnit and TestNG on Spring components to be tested.

Spring IOC (DI) Inversion of Control

IOC (Inversion of Control): The idea is to reverse the direction of resource acquisition.

The traditional way to find the resources required components to the container initiates a request to find resources.

In response, the timely return of the container resources and the application of the IOC after,

The vessel is actively pushed to the resource components it manages,

Components have to do is just choose an appropriate way to accept resources.

This behavior is also known as a passive form to find

Class A used in the object b Class B, under normal circumstances, the object code requires explicit A in B of a new.

After using DI techniques, the code A only need to define a private objects B, obtained without direct new object, but the object B to the new control program by the associated container out externally and injected into the class A references

Write your first Spring program

1, the first to import the spring necessary jar package:

2. Add profile

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--class: 具体的类       id: 标注该标签的-->
    <bean id="mysql" class="com.zhiyou100.zjc.serivce.MysqlDao"/>
    <bean id="oracle" class="com.zhiyou100.zjc.serivce.OracleDao"/>
    <bean id="s" class="com.zhiyou100.zjc.serivce.Serivce">
    <!--name:要和定义的属性名一样  -->
        <property name="dao" ref="oracle"></property>
    </bean>

</beans>

 

3、测试

package com.zhiyou100.zjc.serivce;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //加载sprig配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //获取指定的类对象
        Serivce s = (Serivce) ac.getBean("s");
        s.show();
    }

}

 

Guess you like

Origin www.cnblogs.com/zjc364259451/p/11478886.html
Recommended