Spring (1) Understand Spring concepts and build environments
Spring is an open-source container framework for Ioc and AOP. It aims to simplify enterprise development.
The so-calledControl reversalThe application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects are the responsibility of external containers. In this way, the control is transferred from the application to the external container, and the transfer of control is called a reversal.
The so-calledDependency InjectionIt refers to the Dynamic Injection of dependent objects into components by external containers during runtime.
Spring function:
Reduce the coupling between components to achieve decoupling between different layers of software;
You can use many services provided by containers, such as transaction management and message service. When we use containers to manage transactions, developers no longer need to manually control the transactions or process complicated transaction propagation;
The container provides support for the singleton mode, so developers no longer need to write their own implementation code;
The container provides the AOP technology, which can easily implement functions such as permission interception and runtime monitoring;
Containers provide many auxiliary classes, which can accelerate application development, such as JDBC Template and Hibernate Template;
Spring provides integration support for mainstream application frameworks, such as Hibernate, JPA, and Struts, to facilitate application development.
Whether an application is lightweight or heavyweight depends on how many services it uses. Spring containers provide many services, but these services are not opened for applications by default. An application requires a certain service and also specifies the Service to use. If the application uses few services, for example, if only the Spring core service is used, we can consider the application to be lightweight at this time, if the application uses most of the services provided by Spring, then the application is a heavyweight. Currently, the EJB container is a heavyweight because it provides all the functions of the EJB specification for applications by default.
Official Spring website
For Spring development, at least two jar packages must be imported: commons-logging.jar, spring. jar
Project directory:
Test code
package test.spring.service;public interface PersonService {public abstract void save();}
package test.spring.service.impl;import test.spring.service.PersonService;public class PersonServiceBean implements PersonService {@Overridepublic void save(){System.out.println(=============);}}Configuration File beans. xml
Package test. spring. jnit; import org. junit. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import test. spring. service. personService; public class SpringTest {@ Testpublic void testInstanceSpring () {// ApplicationContext applicationContext = new // ClassPathXmlApplicationContext (// new String [] {beans. xml}); ApplicationContext applicationContext = new ClassPathXmlApplicationContext (beans. xml); PersonService personService = (PersonService) applicationContext. getBean (personService); // get the Object, which needs to be converted to the personService. save ();}}