Spring两大核心机制
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>
|
导入依赖后
开发步骤
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> <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
| 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
1
| Student st = (Student) applicationContext.getBean("student");
|
1
| Student st = (Student) applicationContext.getBean(Student.class);
|
注意: 通过类型取值,当IoC容器中存在两股或两个以上Student Bean的时候就会抛异常,因为此时没有唯一的bean。
-