当前位置:   article > 正文

2 Spring IoC

2 Spring IoC

目录

POM

案例一,基于 xml 配置

创建 entity

创建 Spring 配置文件

测试 Spring IoC

案例一,基于注解配置

改造 entity

改造 Spring 配置文件

改造测试类 Spring IoC

案例二,基于 xml 配置

创建 StudentService 接口

创建 UserServiceImpl 实现类

创建 Spring 配置文件

测试 Spring IoC

案例二,基于注解配置

改造实现类,加上注解 @Service

改造 Spring 配置文件

改造测试类 Spring IoC


POM

创建一个工程名为 spring-ioc-demo 的项目,pom.xml 文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.example</groupId>
  7. <artifactId>spring-ioc-demo</artifactId>
  8. <version>1.0.0-SNAPSHOT</version>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.8</maven.compiler.source>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. <spring.version>5.1.5.RELEASE</spring.version>
  14. <lombok.version>1.16.20</lombok.version>
  15. <junit.version>4.12</junit.version>
  16. <log4j.version>1.2.17</log4j.version>
  17. <slf4j.version>1.7.25</slf4j.version>
  18. </properties>
  19. <dependencies>
  20. <!-- Spring Begin -->
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-context</artifactId>
  24. <version>${spring.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-test</artifactId>
  29. <version>${spring.version}</version>
  30. <scope>test</scope>
  31. </dependency>
  32. <!-- Spring End -->
  33. <dependency>
  34. <groupId>junit</groupId>
  35. <artifactId>junit</artifactId>
  36. <version>${junit.version}</version>
  37. </dependency>
  38. <!-- lombok Begin -->
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <version>${lombok.version}</version>
  43. <scope>provided</scope>
  44. </dependency>
  45. <!-- lombok End -->
  46. <!-- Log Begin -->
  47. <dependency>
  48. <groupId>org.slf4j</groupId>
  49. <artifactId>slf4j-api</artifactId>
  50. <version>${slf4j.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.slf4j</groupId>
  54. <artifactId>slf4j-log4j12</artifactId>
  55. <version>${slf4j.version}</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.slf4j</groupId>
  59. <artifactId>jcl-over-slf4j</artifactId>
  60. <version>${slf4j.version}</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.slf4j</groupId>
  64. <artifactId>jul-to-slf4j</artifactId>
  65. <version>${slf4j.version}</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>log4j</groupId>
  69. <artifactId>log4j</artifactId>
  70. <version>${log4j.version}</version>
  71. </dependency>
  72. <!-- Log End -->
  73. </dependencies>
  74. <build>
  75. <plugins>
  76. <!-- Compiler 插件, 设定 JDK 版本 -->
  77. <plugin>
  78. <groupId>org.apache.maven.plugins</groupId>
  79. <artifactId>maven-compiler-plugin</artifactId>
  80. <version>3.7.0</version>
  81. <configuration>
  82. <source>${java.version}</source>
  83. <target>${java.version}</target>
  84. <encoding>${project.build.sourceEncoding}</encoding>
  85. <showWarnings>true</showWarnings>
  86. </configuration>
  87. </plugin>
  88. </plugins>
  89. </build>
  90. </project>

核心包:主要增加了 org.springframework: spring-context 依赖

案例一,基于 xml 配置

创建 entity

  1. @Data
  2. public class Student {
  3. private int id;
  4. private String name;
  5. private String email;
  6. private String address;
  7. private Hobboy hobboy;
  8. }
  1. @Data
  2. public class Hobboy {
  3. private String basketball;
  4. private String football;
  5. private String running;
  6. }

创建 Spring 配置文件

在 src/main/resources 目录下创建 spring-context.xml 配置文件,从现在开始类的实例化工作交给 Spring 容器管理(IoC),配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  5. <bean id="stu" class="com.example.spring.demo.entity.Student">
  6. <property name="id" value="10"></property>
  7. <property name="name" value="vincent"></property>
  8. <property name="email" value="601521821@qq.com"></property>
  9. <property name="address" value="上海市、宝山区"></property>
  10. <property name="hobboy" ref="hob"></property>
  11. </bean>
  12. <bean id="hob" class="com.example.spring.demo.entity.Hobboy">
  13. <property name="basketball" value="Nike"></property>
  14. <property name="football" value="Adidas"></property>
  15. <property name="running" value="5km"></property>
  16. </bean>
  17. </beans>

<bean/>:用于定义一个实例对象。一个实例对应一个 bean 元素。

id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依赖关系也是通过 id 属性关联的。

class:指定该 Bean 所属的类,注意这里只能是类,不能是接口

测试 Spring IoC

创建一个 GetBeanTest 测试类,测试对象是否能够通过 Spring 来创建,代码如下:

  1. public class GetBeanTest {
  2. @Test
  3. public void testGetBean() {
  4. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
  5. Student stu = (Student) applicationContext.getBean("stu");
  6. System.out.println(stu);
  7. }
  8. }

测试结果:

Student(id=10, name=vincent, email=601521821@qq.com, address=上海市、宝山区, hobboy=Hobboy(basketball=Nike, football=Adidas, running=5km))

案例一,基于注解配置

改造 entity

  1. @Data
  2. @Component
  3. public class Student {
  4. @Value("10")
  5. private int id;
  6. @Value("Vincrnt")
  7. private String name;
  8. @Value("601521821@qq.com")
  9. private String email;
  10. @Value("上海市宝山区")
  11. private String address;
  12. @Autowired
  13. private Hobboy hobboy;
  14. }

  1. @Data
  2. @Component
  3. public class Hobboy {
  4. @Value("Nike")
  5. private String basketball;
  6. @Value("Adidas")
  7. private String football;
  8. @Value("5km")
  9. private String running;
  10. }

改造 Spring 配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  5. <!-- 开启自动注解 -->
  6. <context:annotation-config/>
  7. <!-- 扫描全部的整个项目的包 -->
  8. <context:component-scan base-package="com.example.spring.ioc.demo"/>
  9. </beans>

改造测试类 Spring IoC

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration("classpath:spring-context.xml")
  3. public class GetBeanTest {
  4. @Autowired
  5. private Student student;
  6. @Test
  7. public void annotationStu(){
  8. System.out.println(student);
  9. }
  10. }

测试结果:

Student(id=10, name=vincent, email=601521821@qq.com, address=上海市、宝山区, hobboy=Hobboy(basketball=Nike, football=Adidas, running=5km))

案例二,基于 xml 配置

创建 StudentService 接口

  1. public interface StudentService {
  2. public void sayHi();
  3. }

创建 UserServiceImpl 实现类

  1. public class StudentServiceImpl implements StudentService {
  2. public void sayHi() {
  3. System.out.println("Hello Spring IoC !!!");
  4. }
  5. }

创建 Spring 配置文件

在 src/main/resources 目录下创建 spring-context.xml 配置文件,从现在开始类的实例化工作交给 Spring 容器管理(IoC),配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="studentService" class="com.example.spring.demo.service.impl.StudentServiceImpl" />
  7. </beans>

<bean />:用于定义一个实例对象。一个实例对应一个 bean 元素。

id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依赖关系也是通过 id 属性关联的。

class:指定该 Bean 所属的类,注意这里只能是类,不能是接口。

测试 Spring IoC

创建一个 GetBeanTest 测试类,测试对象是否能够通过 Spring 来创建,代码如下:

  1. public class GetBeanTest {
  2. @Test
  3. public void sayHi() {
  4. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
  5. StudentService studentServiceImpl = (StudentService) applicationContext.getBean("studentService");
  6. String sayHi = studentServiceImpl.sayHi();
  7. System.out.println(sayHi);
  8. }
  9. }

测试结果

Hello Spring IoC !!!

案例二,基于注解配置

改造实现类,加上注解 @Service

  1. @Service
  2. public class StudentServiceImpl implements StudentService {
  3. @Override
  4. public String sayHi() {
  5. return "Hello Spring IoC !!!";
  6. }
  7. }

改造 Spring 配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  5. <!-- 开启自动注解 -->
  6. <context:annotation-config/>
  7. <!-- 扫描全部的整个项目的包 -->
  8. <context:component-scan base-package="com.example.spring.ioc.demo"/>
  9. </beans>

改造测试类 Spring IoC

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration("classpath:spring-context.xml")
  3. public class GetBeanTest {
  4. @Autowired
  5. private StudentService studentService;
  6. @Test
  7. public void annotationSayHi() {
  8. String sayHi = studentService.sayHi();
  9. System.out.println(sayHi);
  10. }

测试结果:

Hello Spring IoC !!!
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/557592
推荐阅读
相关标签
  

闽ICP备14008679号