(1)IOC:控制反转就是应用本身不负责对象的创建及维护,对象的创建和维护由外部容易负责,所以控制权就由应用转移到外部容器,控制权的转移就是所谓的控制反转。
(2)DI:在运行时,由外部容易将对象注入到组件中。
(1)核心模块core:负责Bean的构建和管理,底层使用的是BeanFactory工厂模式来实现。BeanFactory使用依赖注入的形式提供给组件组件依赖
(2)Spring上下文模块context:国际化,Spring上下文是一个配置文件,主要向框架提供上下文信息。
(3)Spring AOP:面向切面的编程
(4)Spring Dao:它主要和dao层相关,可以用来构件数据库的异常处理,和不同数据库供应商抛出异常信息。
(5)Sping ORM:实体关系映射
(6)Spring Web:Web上下文模块建立在应用程序的上下文模块之上,为基于web的应用程序提供了上下文,Web模块还简化了处理多部分请求及将请求参数绑定到域对象的过程。
(7)Spring Web MVC:
配置文件pom.xml
<properties>
<spring.version>5.3.19spring.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.9version>
dependency>
dependencies>
applicatin.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="p1" class="entity.Person">
<property name="name" value="张三">property>
<property name="gender" value="男">property>
<property name="age" value="18">property>
bean>
<bean id="car1" class="entity.Car">
<property name="brand" value="奔驰">property>
<property name="color" value="white">property>
<property name="price" value="50">property>
bean>
<bean id="house1" class="entity.House">
<property name="location" value="和平区">property>
<property name="size" value="100">property>
<property name="price" value="600">property>
bean>
<bean id="house2" class="entity.House">
<property name="location" value="南开区">property>
<property name="size" value="200">property>
<property name="price" value="500">property>
bean>
<bean id="p2" class="entity.Person">
<property name="name" value="张三">property>
<property name="gender" value="男">property>
<property name="age" value="18">property>
<property name="car" ref="car1">property>
<property name="houseList">
<list>
<ref bean="house1">ref>
<ref bean="house2">ref>
<bean class="entity.House">
<property name="location" value="蓟州区">property>
<property name="size" value="500">property>
<property name="price" value="800">property>
bean>
list>
property>
<property name="map">
<map>
<entry key="address" value="天津市和平区">entry>
<entry key="phone" value="139000000">entry>
<entry key="info">
<bean class="entity.Info">
<property name="address" value="天津市和平区">property>
<property name="phone" value="13900000">property>
<property name="email" value="1213@ooo.com">property>
bean>
entry>
<entry key="niceName" value="老李">entry>
map>
property>
bean>
***src/java/entity/Car.class***
package entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Car {
private String brand;
private String color;
private int price;
}
src/java/entity/House.class
package entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class House {
private String location;
private int size;
private int price;
}
src/java/entity/Info.class
package entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Info {
private String address;
private String phone;
private String email;
}
src/java/entity/Person.class
```java
package entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.List;
import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Person {
private String name;
private String gender;
private int age;
private Car car;
private List houseList;
private Map map;
}
#### 4.在测试类中,测试对象的数据
```java
import entity.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyText {
@Test
public void textPerson(){
ApplicationContext application=new ClassPathXmlApplicationContext("application.xml");
Person person1=(Person) application.getBean("p1");
System.out.println(person1);
Person person2=(Person) application.getBean("p2");
System.out.println(person2);
Person person3=(Person) application.getBean("p3");
System.out.println(person3);
Person person4=(Person) application.getBean("p4");
System.out.println(person4);
}
}
总结1:创建项目选择mevan,首先先向pom.xml添加依赖,在resources下创建xxx.xml写Spring配置文件,其次在entity写所需要的类。然后进行数据关联。数据关联,在xxm中用创建对象,id="xxx"为对象名,与类进行绑定,与类中属性进行绑定,name绑定属性名称,value为值。
可与list集合绑定,底下的书写规则同
eg:
private List<House> houseList;
<property name="houseList">
<list>
<ref bean="house1">ref>
<ref bean="house2">ref>
<bean class="entity.House">
<property name="location" value="蓟州区">property>
<property name="size" value="500">property>
<property name="price" value="800">property>
bean>
list>
property>
可与map集合绑定
eg:
private Map<String,Object> map;
<property name="map">
<map>
<entry key="address" value="天津市和平区">entry>
<entry key="phone" value="139000000">entry>
<entry key="info">
<bean class="entity.Info">
<property name="address" value="天津市和平区">property>
<property name="phone" value="13900000">property>
<property name="email" value="1213@ooo.com">property>
bean>
entry>
<entry key="niceName" value="老李">entry>
map>
property>
总结2:
ref可用于引入其他对象到该对象下!
eg:
```xml
<bean id="car1" class="entity.Car">
<property name="brand" value="奔驰"></property>
<property name="color" value="white"></property>
<property name="price" value="50"></property>
</bean>
<bean id="house1" class="entity.House">
<property name="location" value="和平区"></property>
<property name="size" value="100"></property>
<property name="price" value="600"></property>
</bean>
```xml
```xml
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)