上一篇学习了控制反转(IoC)的本质和具体实现方法,这次我们就学习写一个小的项目来体验这个过程。
一、项目构建
1.Maven依赖(导包)- <dependencies>
-
- <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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans><artifactId>spring-webmvc</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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans><version>5.3.23</version>
- </dependency>
-
- <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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans><version>4.13.2</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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans><scope>test</scope>
- </dependency>
- </dependencies>
复制代码 只导入第一个spring-webmvc即可,junit只是为了方便测试。
2.写一个pojo:Hello- package com.jms.pojo;
- public class Hello {
- private String str;
- public String getStr() {
- return str;
- }
- public void setStr(String str) {
- this.str = str;
- }
- @Override
- public String toString() {
- return "Hello{" +
- <?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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans> "str='" + str + '\'' +
- <?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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans> '}';
- }
- }
复制代码 3.基于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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="..." class="...">
-
- </bean>
- <bean id="..." class="...">
-
- </bean>
-
- </beans>
复制代码 The id attribute is a string that identifies the individual bean definition.
The class attribute defines the type of the bean and uses the fully qualified classname.
id是标识单个Bean定义的字符串,class是定义Bean的类型并使用全限定类名。
下面看我们需要配置的XML:
(对于XML文件的名字无特殊要求,这里我们命名为beans.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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
-
- <bean id="hello" class="com.jms.pojo.Hello">
- <property name="str" value="Hello Spring"/>
- </bean>
- </beans>
复制代码 二、测试
在测试之前我们需要知道怎么使用,官方给出了具体的使用方法:
实例化容器
提供给构造函数的一个或多个位置路径是资源字符串,允许容器从各种外部资源(如本地文件系统、Java 等)加载配置元数据。ApplicationContext CLASSPATH- ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
复制代码 使用容器
这是高级工厂的接口,能够维护不同 Bean 及其依赖项的注册表。通过使用 该方法 ,您可以检索 Bean 的实例。ApplicationContextT getBean(String name, Class requiredType)
允许您读取 Bean 定义并访问它们,如以下示例所示:ApplicationContext- // create and configure beans
- ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
- // retrieve configured instance
- PetStoreService service = context.getBean("petStore", PetStoreService.class);
- // use configured instance
- List<String> userList = service.getUsernameList();
复制代码 通过上述官方给出的使用例子,我们不难知道我们首先要通过传入XML的文件名实例化容器,并且通过getBean来获取我们的对象。
实例化容器的方法中,可以传入一个XML的文件名,也可以传入多个;getBean中的字符串传入的是XML文件中配置的bean的id,传入的class就是对象对应的类。
知道了具体的使用,下面我们就来实现:- @Test
- public void HelloSpring() {
- //实例化容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
- //通过容器获取对象
- Hello hello = applicationContext.getBean("hello", Hello.class);
- System.out.println(hello);
- }
复制代码 测试结果:

没有问题。
既然HelloSpring没有问题,我们就完善一下一开始用于学习IoC基本原理的一个项目。
先看一下项目框架:

由于依赖已经导入,在此我么就直接去配置XML文件,这里我们配置两个XML文件:
dao.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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="usrDaoImpl" class="com.jms.dao.UserDaoImpl"></bean>
- <bean id="userDaoMysqlImpl" class="com.jms.dao.UserDaoMysqlImpl"></bean>
- <bean id="userDaoOracleImpl" class="com.jms.dao.UserDaoOracleImpl"></bean>
- </beans>
复制代码 service.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
- https://www.springframework.org/schema/beans/spring-beans.xsd">
-
- <bean id="hello" class="com.jms.pojo.Hello">
- <property name="str" value="Hello Spring"/>
- </bean>
- </beans>
复制代码 可以看到上面service.xml中有一个import标签引用了dao.xml,这是因为下面用到了dao.xml里面的bean,所以需要import引用,如果是在同一个xml文件下则不需要。
接下来测试:- @Test
- public void UserService() {
- //实例化容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("dao.xml", "service.xml");
- //获取对象
- UserService userService = applicationContext.getBean("userServiceImpl", UserServiceImpl.class);
- userService.getUser();
- }
复制代码 测试结果:
我们修改service.xml中的ref都会得到不同的结果:- [/code][img]https://img2022.cnblogs.com/blog/2459242/202209/2459242-20220919094729451-308241643.png[/img]
- [code]
复制代码
可见,我们不需要对代码进行修改,只需修改配置文件就可以实现不同的需求。
(本文仅作个人学习用,如有纰漏敬请指正)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |