3.1、创建module
3.1.1、右击project,创建新module

3.1.2、选择maven

3.1.3、设置module名称和路径


3.1.4、module初始状态

3.1.5、配置打包方式和依赖
3.2、示例
3.2.1、创建组件类
 - 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配置文件

++++++++++++++++++++++++++++++++++++分割线++++++++++++++++++++++++++++++++++++

注意:因为会通过自定义代码指定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、创建测试类

如图所示,获取到了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、根据类型获取(最常用)

如图所示,根据类型获取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,helloWordtwo
复制代码
+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

3.3.3、根据id和类型获取
 - @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有且只能有一个

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
 - @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(); }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |