Spring Learning Notes 1

1. Introduction to Spring Framework and Official Compressed Packet Directory
introduce
1. Major Inventor: Rod Johnson
2. Advocates of wheel theory:
2.1 Wheel Theory: No Repeated Invention of Wheels
2.2 IT Industry: Direct Use of Written Code
3.Spring Framework Aim: Do not reinvent technology, make the original technology more convenient to use
4. Several core functions of Spring
4.1 IoC/DI Control Inversion/Dependency Injection
4.2AOP Face-Oriented Programming
4.3 Declarative Transactions
5.Spring framework runtime
5.1 test: spring provides test functionality
5.2 Core Container: Core Container, the most basic condition for Spring to start
5.2.1 Beans: Spring is responsible for creating class objects and managing them
5.2.2 Core: Core class
5.2.3Context: Context parameters, access to external resources or management annotations, etc.
5.2.4SpEl: expression.jar
5.3 aop: Implementing aop functionality requires dependency
5.4Aspects: Aspect AOP-dependent packages
5.5 Data Access/Integration: spring encapsulates data access layer related content
5.5.1 JDBC: Spring encapsulated JDBC code
5.5.2ORM: Code encapsulating persistence layer frameworks, such as Hibernate
5.5.3 transactions: corresponding to spring-tx.jar, declarative transaction usage
5.6WEB: When spring is needed to complete web-related functions
5.6.1 example: when loading spring configuration file by tomcat, spring-web package is needed

6. Important concepts in the Spring framework
6.1 Container: Spring as a large container
6.2 BeanFactory interface, old version
The ApplicationContext interface in the new version of 6.2.1 is a BeanFactory sub-interface. The functions of BeanFactory are available in the ApplicationContext.
7. Starting with Spring 3, the Spring framework has been split into multiple jar s
7.1 Spring 2 and previous jar
II. IoC
1. Chinese Name: Control Inversion
2. English Name: (Inversion of Control)
3. What is IoC?
3.1 What IoC did was originally initiated by programmers to instantiate object things through new and hand them over to Spring.
3.2 In control inversion, control refers to the object of control class.
3.3 Inversion of control refers to the transfer of responsibility to Spring
3.4 IoC's greatest role: decoupling
3.4.1 Programmers do not need to manage objects, and decouple between object management and programmers.
3. Spring Environment Construction
1. Import jar
1.1 Four core packages and one log package
2. Create new applicationContext.xml under src
2.1 File Name and Path Customization
2.2 Keep in mind the Spring container ApplicationContext, and the information about the configuration of applicationContext.xml is finally stored in the ApplicationContext container.
2.3 spring configuration file is schema-based
2.3.1 schema file extension. xsd
2.3.2 Understanding schema as an upgraded version of DTD
2.3.2.1 has better scalability than DTD
2.3.3 Each time an xsd file is introduced, it is a namespace(xmlns)
Only the basic schema needs to be introduced in the 2.4 configuration file
2.4.1 Create objects through <bean/>.
2.4.2 Objects are created when the default configuration file is loaded

<?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">
	<!-- 
		id Represents getting the object identifier
		class Which class of objects to create
	-->
	<bean id="peo" class="edu.swpu.pojo.People"/>
</beans>

3. Writing test methods
3.1 getBean ("< bean > label id value", return value type);
If there is no second parameter, the default is Object3.2
getBeanDefinitionNames(), all currently managed objects in the Spring container

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = ac.getBean("peo",People.class);
System.out.println(people);
// String[] names = ac.getBeanDefinitionNames();
// for (String string : names) {
// System.out.println(string);
// }

Fourth, Three Ways to Create Objects in Spring
1. Create by Constructing Method
1.1 Parametric Construction Creation: Default
1.2 Parametric Construction Creation: Need to be Configured Clearly
1.2.1 Need to provide a parametric construction method in the class
1.2.2 Set in applicationContext.xml which constructor to call to create the object
1.2.2.1 If the set conditions match multiple constructors, the final constructor is executed.
1.2.2.2 index: Index of parameters, starting from 0
1.2.2.3 name: parameter name
1.2.2.4 type: Type (distinguishing switch keywords from encapsulation classes int and Integer)

<bean id="peo" class="edu.swpu.pojo.People">
	<!-- ref Quote another bean value Basic data type or String etc. -->
	<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
	<constructor-arg index="1" name="name" type="java.lang.String" value="Zhang San"></constructor-arg>
</bean>

2. Example Factory
2.1 Factory Design Patterns: Help create class objects, one factory can produce multiple objects
2.2 Instance Factory: You need to create factories before you can produce objects
2.3 Implementation steps:
2.3.1 There must be an example factory

public class PeopleFactory {
	public People newInstance(){
		return new People(1,"test");
	}
}

2.3.2 Configure factory objects and objects to be created in applicationContext.xml

<bean id="factory" class="edu.swpu.pojo.PeopleFactory"></bean>
<bean id="peo1" factory-bean="factory" factory-method="newInstance"></bean>

3. Static factories
3.1 Do not need to create factories, create objects quickly
3.2 Implementation steps
3.2.1 Write a static factory (add static in method)

public class PeopleFactory {
	public static People newInstance(){
		return new People(1,"test");
	}
}

3.2.2 in applicationContext.xml

<bean id="peo2" class="edu.swpu.pojo.PeopleFactory" factory-method="newInstance"></bean>

5. How to assign attributes to beans (injection)
1. Setting values by constructing methods
2. Setting injection (via set method)
2.1 If an attribute is a basic data type or a String, it's simple.

<bean id="peo" class="edu.swpu.pojo.People">
	<property name="id" value="222"></property>
	<property name="name" value="Zhang San"></property>
</bean>

2.1.1 is equivalent to

<bean id="peo" class="edu.swpu.pojo.People">
	<property name="id">
		<value>456</value>
	</property>
	<property name="name">
		<value>zhangsan</value>
	</property>
</bean>

2.2 If the attribute is Set <?>.

<property name="sets">
	<set>
		<value>1</value>
		<value>2</value>
		<value>3</value>
		<value>4</value>
	</set>
</property>

2.3 If the attribute is List<?>

<property name="list">
	<list>
		<value>1</value>
		<value>2</value>
		<value>3</value>
	</list>
</property>

2.3.1 If there is only one value in the list

<property name="list" value="1"></property>

2.4 If the attribute is an array
2.4.1 If there is only one value in the array, it can be assigned directly through the value attribute.

<property name="strs" >
	<array>
		<value>1</value>
		<value>2</value>
		<value>3</value>
	</array>
</property>

2.5 If the attribute is map

<property name="map">
	<map>
		<entry key="a" value="b" ></entry>
		<entry key="c" value="d" ></entry>
	</map>
</property>

2.6 If the attribute Properties type

<property name="demo">
	<props>
		<prop key="key">value</prop>
		<prop key="key1">value1</prop>
	</props>
</property>

VI. DI
1.DI Chinese Name: Dependency Injection
2. Dependency Injection
3. What is DI?
3.1DI is the same as IoC
3.2 When one class (A) needs to depend on another class () object, the process of assigning B to A is called dependency injection.
4. Code manifestation:

<bean id="peo" class="edu.swpu.pojo.People">
	<property name="desk" ref="desk"></property>
</bean>
<bean id="desk" class="edu.swpu.pojo.Desk">
	<property name="id" value="1"></property>
	<property name="price" value="12"></property>
</bean>

7. Simplify MyBatis with Spring
1. Import all jar and spring basic packages of mybatis, spring-jdbc, spring-tx, spring-aop, spring-web, spring integrated mybatis packages, etc.
2. Configure web.xml first

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc
	e"
	xsi:schemaLocation="http://java.sun.com/xml/ns/java
	ee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<!-- Context parameters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- spring configuration file -->
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- Encapsulated a listener,Help Loading Spring Configuration File Love -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

2. Write spring configuration file applicationContext.xml

<?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/sc
hema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- Data source encapsulation class .data source:Get the database connection,spring-jdbc.jar in-->
	<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
	<!-- Establish SqlSessionFactory object -->
	<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- Database connection information comes from dataSource -->
		<property name="dataSource" ref="dataSouce"></property>
	</bean>
	<!-- Scanner equivalent mybatis.xml in mappers lower package Label,scanning edu.swpu.mapper Objects are created for the corresponding interface after the package-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- Which package to scan -->
		<property name="basePackage" value="edu.swpu.mapper"></property>
		<!-- and factory Generative relationship -->
		<property name="sqlSessionFactory" ref="factory"></property>
	</bean>
	<!-- from spring Administration service Implementation class -->
	<bean id="airportService" class="edu.swpu.service.impl.AirportServiceImpl">
		<property name="airportMapper" ref="airportMapper"></property>
	</bean>
</beans>

4. Coding
4.1 Write pojo normally
4.2 When writing mapper packages, you must use interface binding or annotation schemes (you must have interfaces)
4.3 Writing Service Interface and Service Implementation Class Normally
3.3.1 You need to declare Mapper interface objects in Service implementation classes and generate get/set methods
4.4 Spring cannot manage Servlet s and fetches Servie objects from the service

@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
private AirportService airportService;
@Override
public void init() throws ServletException {
	//Instantiation of service
	// ApplicationContext ac = new
	ClassPathXmlApplicationContext("applicationContext.xml");
	//After spring and web integration, all information is stored in
	webApplicationContextApplicationContext ac =
	WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
	airportService=ac.getBean("airportService",AirportServiceImpl.class);
} 
@Override
protected void service(HttpServletRequest req,HttpServletResponse resp) throwsServletException,IOException {
	req.setAttribute("list", airportService.show());
	req.getRequestDispatcher("index.jsp").forward(req,resp);
}

Tags: Spring xml JDBC Attribute

Posted on Sat, 07 Sep 2019 03:54:22 -0700 by todd-imc