1.1 Professional Terminology understanding
Components/ Frame Design
Intrusive design
Introduces a framework that affects the structure of existing classes, that is, the need to implement or inherit some particular class.
For example: Struts framework inherits Actionsupport
Non-intrusive design
The framework is introduced and has no effect on the existing class structure.
Example: Hibernate frame/Spring frame
Control inversion:
control inversion , iocinversion on control
The creation of the object is given to the external container to complete, and this is controlled inversion.
Originally in the Business logic layer, need to get the DAO implementation, either in the business logic layer directly new, or use the factory to get one. This is called "Active acquisition Dependency".
In the IOC, the container decides which instance to inject into the business logic layer and no longer needs to manually new or call factory get.
Dependency Injection , Dependency injection
Working with objects ' dependencies
For example, now our business logic layer is implemented, need to use database, there must be DAO interface.
This interface, in the implementation of different persistence, the implementation of the interface is not the same!
Inside the container, you can find the right implementation and inject it into the business logic layer to avoid new! in the business logic layer. The implementation can be replaced at any time, with the aim of "decoupling".
Difference:
Control reversal: Solve object creation problem "object creation to others"
Dependency Injection: After the object is created, the processing of the object's relationship is dependent injection "through set method dependency Injection"
Aop
plane-oriented programming. Facets, simply speaking, can be understood as a class, a class that is formed by a lot of repeating code.
Section Examples: Transactions, logs, permissions;
2.2 Spring Framework A. Overview
The Spring framework resolves a framework for object creation and dependency relationships between objects.
And can be used with other frameworks; Spring and struts, spring and hibernate
(A framework for the integration (bonding) function)
Spring provides a one-stop solution:
1) core features of Spring Core spring: IOC container, resolving object creation and dependencies
2) Spring Web Spring support for Web modules.
-You can integrate with struts so that struts action is created to spring
-->spring MVC pattern
3) Spring DAO Spring support for JDBC Operations "jdbctemplate Template Tool Class"
4) Spring ORM Spring support for ORM:
-You can integrate with Hibernate, "session"
-You can also use spring to encapsulate hibernate operations
5) Spring AOP slicing programming
6) Springee Spring support for other Java EE modules
3.3 Spring Development Step 3.1, guide package
Using the download installation method to get the spring, the least required dependency package
Spring-context-4.3.6.release.jar
---One of the core jar files inside the >spring, responsible for reading and parsing the XML configuration file
Spring-aop-4.3.6.release.jar
---> Aspect-oriented Programming support, also one of the core jar files
Spring-beans-4.3.6.release.jar
---> is responsible for the processing of beans elements, the entire XML configuration specification is defined here
Spring-core-4.3.6.release.jar
---> Core container, responsible for managing the bean (instance)
Spring-expression-4.3.6.release.jar
--->sp el Support package that simplifies the configuration of XML directly using expressions in spring
-------------The following packages are optional---------------------
Commons-logging-1.2.jar
--->spring is required to use the logging dependency package, by default, directly using the System.err output log, the default level is info
Log4j-jcl-2.7.jar
---> commons-logging output, bridge to log4j 2 output, all log levels, output targets are all controlled by log4j 2.
Log4j-api-2.7.jar
--->log4j 2 API, a bunch of interfaces
Log4j-core-2.7.jar
Implementation of--->log4j 2
3.2. Core configuration file Applicationcontext.xml
Spring configuration file: Applicationcontext.xml/bean.xml
Constraint reference: spring-framework-4.3.6.release\docs\spring-framework-reference\html
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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" >
<!--IOC container configuration: All objects to be created are configured here-- <bean id= "U1" class= "Org.fkjava.spring.domain.User" ></bean></Beans>
3.3. Spring API
Create an entity class user
PackageOrg.fkjava.spring.domain; Public classUser {PrivateString UserName; //no parameter constructor PublicUser () {System.out.println ("User's parameterless constructor is called"); } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) {System.out.println ("Injected value:" +userName); This. UserName =UserName; }
Simple test of Dependency injection
Packageorg.fkjava.test;ImportOrg.fkjava.spring.dao.UserDao;ImportOrg.fkjava.spring.domain.User;ImportOrg.fkjava.spring.service.UserService;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classsimple test of dependency injection { Public Static voidMain (string[] args) {
//get the IOC container object, read "Applicationcontext.xml"file. Direct access to IOC container objects
classpathxmlapplicationcontext CPC = new classpathxmlapplicationcontext ("Applicationcontext.xml");
//user U = new user (); ////Before acquiring an object you need to create an object, now using the Spring IOC container, get the IOC container first, then get the bean from the container
Get the bean from the IOC container and get the object
User user = (user) Cpc.getbean ("U1");
}
}
Output:
The user's parameterless constructor is called
[Email protected]
Getting Started with spring framework