3、Spring之入门案例
3.1、创建module3.1.1、右击project,创建新module
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081202352-22924479.png
3.1.2、选择maven
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081231545-772277435.png
3.1.3、设置module名称和路径
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081343361-110286104.png
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081428542-2020663255.png
3.1.4、module初始状态
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081540355-392286923.png
3.1.5、配置打包方式和依赖
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725081906917-308533206.png
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<?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">
<bean id="helloWord" ></bean>
</beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<?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">
<bean id="helloWord" ></bean>
</beans> 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>org.rain</groupId>
<artifactId>spring_helloword</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<?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">
<bean id="helloWord" ></bean>
</beans>
<?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">
<bean id="helloWord" ></bean>
</beans><dependency>
<?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">
<bean id="helloWord" ></bean>
</beans> <groupId>org.springframework</groupId>
<?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">
<bean id="helloWord" ></bean>
</beans> <artifactId>spring-context</artifactId>
<?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">
<bean id="helloWord" ></bean>
</beans> <version>5.3.1</version>
<?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">
<bean id="helloWord" ></bean>
</beans></dependency>
<?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">
<bean id="helloWord" ></bean>
</beans>
<?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">
<bean id="helloWord" ></bean>
</beans><dependency>
<?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">
<bean id="helloWord" ></bean>
</beans> <groupId>junit</groupId>
<?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">
<bean id="helloWord" ></bean>
</beans> <artifactId>junit</artifactId>
<?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">
<bean id="helloWord" ></bean>
</beans> <version>4.12</version>
<?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">
<bean id="helloWord" ></bean>
</beans> <scope>test</scope>
<?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">
<bean id="helloWord" ></bean>
</beans></dependency>
</dependencies>
</project>3.2、示例
3.2.1、创建组件类
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725161821878-599039383.png
package org.rain.spring.component;
/**
* @author liaojy
* @date 2023/7/25 - 19:17
*/
public class HelloWord {
public void sayHello(){
<?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">
<bean id="helloWord" ></bean>
</beans>System.out.println("Hello,Spring!");
}
}3.2.2、创建Sprig配置文件
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725162032876-1933595880.png
++++++++++++++++++++++++++++++++++++分割线++++++++++++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725163139785-1201946288.png
注意:因为会通过自定义代码指定Spring配置文件,所以Spring配置文件名可以是任意的;
但当整合ssm后,就不能通过通过自定义代码指定Spring配置文件,因此文件名有硬性要求。
<?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">
<bean id="helloWord" ></bean>
</beans>3.2.3、创建测试类
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230725164556891-722235301.png
如图所示,获取到了IOC容器和容器中对应的bean组件,并成功调用了该bean组件的方法
package org.rain.spring.test;import org.junit.Test;import org.rain.spring.component.HelloWord;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @author liaojy * @date 2023/7/25 - 19:39 */public class HelloWordTest { @Test public void testHelloWord(){<?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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器<?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">
<bean id="helloWord" ></bean>
</beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器中的bean<?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">
<bean id="helloWord" ></bean>
</beans>HelloWord helloWord = (HelloWord)applicationContext.getBean("helloWord");<?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">
<bean id="helloWord" ></bean>
</beans>helloWord.sayHello(); }}3.3、获取bean的三种方式
3.3.1、根据id获取
由于 id 属性是 bean 的唯一标识,所以根据 bean 标签的 id 属性可以精确获取到一个组件对象;但也存在类型转换问题,具体见上一小节。
3.3.2、根据类型获取(最常用)
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727002838853-343052770.png
如图所示,根据类型获取bean,则不存在类型转换问题
@Test public void testHelloWord(){<?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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器<?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">
<bean id="helloWord" ></bean>
</beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器中的bean<?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">
<bean id="helloWord" ></bean>
</beans>HelloWord helloWord = applicationContext.getBean(HelloWord.class);<?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">
<bean id="helloWord" ></bean>
</beans>helloWord.sayHello(); }注意:如下图所示,当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个;否则会报错
org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'org.rain.spring.component.HelloWord' available:
expected single matching bean but found 2: helloWord,helloWordtwohttps://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727003154916-1733943968.png
+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727003252243-1330321075.png
3.3.3、根据id和类型获取
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727004743536-217283141.png
@Test public void testHelloWord(){<?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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器<?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">
<bean id="helloWord" ></bean>
</beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器中的bean<?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">
<bean id="helloWord" ></bean>
</beans>HelloWord helloWord = applicationContext.getBean("helloWord",HelloWord.class);<?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">
<bean id="helloWord" ></bean>
</beans>helloWord.sayHello(); }3.3.4、重要扩展
如果组件类实现了接口,则根据接口类型可以获取 bean,前提是IOC容器中实现该接口的组件类型的bean有且只能有一个
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727010014262-329120459.png
+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727010151464-1569980253.png
+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727010215920-1066818401.png
+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202307/2052479-20230727010520343-1419495855.png
@Test public void testHelloWord(){<?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">
<bean id="helloWord" ></bean>
</beans>//获取IOC容器<?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">
<bean id="helloWord" ></bean>
</beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("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">
<bean id="helloWord" ></bean>
</beans>//根据接口类型获取IOC容器中的bean<?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">
<bean id="helloWord" ></bean>
</beans>HelloWord helloWord = (HelloWord) applicationContext.getBean(Hello.class);<?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">
<bean id="helloWord" ></bean>
</beans>helloWord.sayHello(); }
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]