请选择 进入手机版 | 继续访问电脑版

湖南新梦想

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 147|回复: 0

Spring创建接口对象的方法

[复制链接]

29

主题

36

帖子

262

积分

中级会员

Rank: 3Rank: 3

积分
262
发表于 2023-6-1 22:43:27 | 显示全部楼层 |阅读模式
Spring创建接口对象的步骤:

1.在pom.xml中添加依赖(导包)
  <!--  导入spring依赖  -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.20</version>
    </dependency>
2.添加Spring配置文件,如
applicationcontext_grade.xml
applicationcontext_student.xml

3.实例化接口对象
准备=实体类+测试类
创建接口对象的方法
第一种:
在applicationcontext_grade.xml中通过<bean/>创建一个对象
<bean id="gradeA" class="com.spring.pojo.Grade">
        <property name="gradeId" value="1"/>
        <property name="gradeName" value="高数"/>
    </bean>
然后在测试类中,调用对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext_grade.xml");
// 因为grade的spring配置文件中,只创建了一个对象,所以可以用Grade.class来调用对象
Grade grade =
applicationContext.getBean(Grade.class);
System.out.println(grade);
第二种:
在applicationcontext_grade.xml中通过<bean/>创建多个对象
<!--spring配置文件,用于创建对象-->
    <bean id="gradeA" class="com.spring.pojo.Grade">
        <property name="gradeId" value="1"/>
        <property name="gradeName" value="高数"/>
    </bean>
    <bean id="gradeB" class="com.spring.pojo.Grade">
        <property name="gradeId" value="2"/>
        <property name="gradeName" value="雅思"/>
    </bean>
然后在测试类中,调用对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext_grade.xml");
/ 因为grade的spring配置文件中,创建了多个不同对象,所以要用<bean/>的id值来调用对象
Grade gradeA = (Grade) applicationContext.getBean("gradeA");
        Grade gradeB = (Grade) applicationContext.getBean("gradeB");
        System.out.println(gradeA);
        System.out.println(gradeB);
第三种:
通过扫描文件+注解,创建对象
只需在applicationcontext_grade.xml中添加扫描配置
<context:component-scan base-package="com.spring.pojo"/>
然后在实体类上加上@Reposity注解
@Repository
public class Grade {
最后在测试类中这样测试
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext_grade.xml");
        Grade grade = applicationContext.getBean(Grade.class);
        System.out.println(grade);
输出的对象属性均为空,因为此时仅仅创建了一个对象,并没有为其属性赋值
第四种:
同时生成多个不同对象
1.每个类对应一个spring配置文件,并在配置文件中,编写响应的<bean/>或扫描配置
比如grade-applicationcontext_grade.xml
student-applicationcontext_student.xml
最后在测试类中这样编写
// 文件名为*,则加载所有spring配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext_*.xml");
Student studentA = (Student) applicationContext.getBean("studentA");
        Student studentB = (Student) applicationContext.getBean("studentB");
        Grade gradeA = (Grade) applicationContext.getBean("gradeA");
        Grade gradeB = (Grade) applicationContext.getBean("gradeB");
        System.out.println(studentA);
        System.out.println(studentB);
        System.out.println(gradeA);
        System.out.println(gradeB);
2.在applicationcontext_student.xml中导入grade的spring配置文件
<import resource="applicationcontext_grade.xml"/>
然后在测试类中只需加载student的spring配置文件
// 通过<import/> 只用加载student的spring配置文件,就可以同时加载grade的配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext_student.xml");
Student studentA = (Student) applicationContext.getBean("studentA");
        Student studentB = (Student) applicationContext.getBean("studentB");
        Grade gradeA = (Grade) applicationContext.getBean("gradeA");
        Grade gradeB = (Grade) applicationContext.getBean("gradeB");
        System.out.println(studentA);
        System.out.println(studentB);
        System.out.println(gradeA);
        System.out.println(gradeB);

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|湖南新梦想 ( 湘ICP备18019834号-2 )

GMT+8, 2023-10-4 15:48 , Processed in 0.040373 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表