文章目录
maven导入spring坐标
<!--pom文件-->
<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.wj</groupId>
<artifactId>soring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>soring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-contest-version>5.2.10.RELEASE</spring-contest-version>
<junit-versin>4.12</junit-versin>
</properties>
<dependencies>
<!--spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-contest-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-versin}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
spring基础包已经导入
Spring框架的核心类
核心容器有BeabFactory和ApplicationContext两种,两者都是通过xml配置文件加载IOC容器、加载bean的。不同的是BeanFactory加载bean不会检查依赖注入,若没有注入依赖的bean,调用时会报错。ApplicationContext会在初始化时自检。
BeanFactory
BeanFactory提供完整的IOC服务支持,BeanFactory就是管理bean的工厂。
ApplicationContext
ApplicationContext是BeanFactory的子接口,也被称为应用上下文,是一种常用的核心容器类,不仅包含了BeanFactory的所有功能,而且提供了对国际化、资源访问、事件传播等方面的支持。
获取ApplicationContext实例的常用的方法有两种
1、ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext(String path);
通过applicationcontext.xml的文件名去找配置文件,path为文件类路径
2、FileSystemXmlApplicationContext
ApplicationContext context = new FileSystemXmlApplicationContex(String path);
这里的path为配置文件的绝对路径,这种方法不太灵活,不常使用
ApplicationContext实例化容器获取bean的两种方式
Object getBean(String name)
这种方式根据容器中的id或者name来获取指定的bean,获取之后需要强制类型转换
HelloSpring helloSpring = (HelloSpring) context.getBean("hellowspring");
T getBean(Class classtype)
这种方式根据容器中bean的类型获取bean,这种方式不需要类型转换
HelloSpring helloSpring = context.getBean(HelloSpring.class);
Spring入门案例
步骤
1、写实现bean的类
2、applicationcontext.xml中配置bean
3、程序中获取IOC容器(applicationcontext实例)
4、通过容器获取bean
创建HelloSpring.java
package com.wj;
import lombok.Getter;
import lombok.Setter;
/**
* @Description:
* @Author WJ
* @Date 2023/2/17:19:16
* @Version V1.0
**/
@Setter
@Getter
public class HelloSpring {
private String who;
public void print(){
System.out.println("Hellow,"+this.getWho()+"!!!");
}
}
创建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/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将HelloSpeing类作为bean加载 id为bean的唯一标识,名字任意,
若想为bean指定更多的别名,可用name属性指定,多个名字用逗号,分号或空格分开
class作为bean的类的路径,指定bean实例的类型-->
<bean id="hellowspring" class="com.wj.HelloSpring">
<!--property 用来为实例的属性赋值,调用实例成员who的setter方法
若成员who的setter方法名不为setWho(),为setSomebody(),property的
name应设置为Somebody-->
<property name="who">
<value>Spring</value>
</property>
</bean>
</beans>
测试
package com.wj;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Unit test for simple App.
*/
public class AppTest{
@Test
public void test(){
//根据applicationcontext.xml加载IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//通过容器,以bean的类型获取bean
HelloSpring helloSpring = context.getBean(HelloSpring.class);
//调用bean实例的方法,
helloSpring.print();
}
}