Spring学习-1


Spring两大核心机制

  • IoC:工厂模式

  • AOP:代理模式

IoC

loC是Spring 框架的灵魂,控制反转。

lombok可以帮助开发者自动生成实体类相关的方法。

在IDEA中使用必须安装插件:添加链接

1
2
3
4
5
6
7
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

导入依赖后

  • @Data 自动生成构造方法(包含所有)

  • @Getter Get构造方法

  • @Setter Set构造方法

  • @AllArgsConstructor 带参构造器

  • @NoArgsConstructor 无参构造器

开发步骤

1、创建Maven工程,pom.xml导入依赖。

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
</dependencies>

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.godve</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- Spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

</dependencies>

</project>

2、在resources路径下创建Spring.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

<bean id="student" class="com.godve.entity.Student"></bean>

</beans>

3、IoC容器通过读取spring.xml配置文件,加载bean标签来创建对象。

4、调用API 获取IoC容器中已经创建的对象。

1
2
3
4
//IoC容器自动创建对象,开发者只需要取出对象即可。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student st = (Student) applicationContext.getBean("student");
System.out.println(st);

IoC容器创建bean的两种方式

  • 无参构造
1
<bean id="student" class="com.godve.entity.Student"></bean>

给成员变量赋值

1
2
3
4
5
<bean id="student" class="com.godve.entity.Student">
<property name="id" value="1"></property>
<property name="age" value="23"></property>
<property name="name" value="张三"></property>
</bean>
  • 有参构造
1
2
3
4
5
<bean id="student3" class="com.godve.entity.Student">
<constructor-arg name="id" value="3"></constructor-arg>
<constructor-arg name="name" value="王五"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>
1
2
3
4
5
<bean id="student3" class="com.godve.entity.Student">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="王五"></constructor-arg>
<constructor-arg index="2" value="18"></constructor-arg>
</bean>

从IoC容器中去bean

  • 通过id取值
1
Student st = (Student) applicationContext.getBean("student");
  • 通过class取值
1
Student st = (Student) applicationContext.getBean(Student.class);

注意: 通过类型取值,当IoC容器中存在两股或两个以上Student Bean的时候就会抛异常,因为此时没有唯一的bean。

-