Spring:Bean依赖注入方式

1 依赖注入方式

1.1 创建maven工程

Alt

pom.xml

Alt

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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.example</groupId>
    <artifactId>chapter07</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

1.2 创建bean

Student.java

package com.qfedu.bean;

public class Student {
    private int sid;
    private String name;
    private String age;
    private String course;

    //全参构造

    public Student(int sid, String name, String age, String course) {
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.course = course;
    }

    //toString()方法

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", course='" + course + '\'' +
                '}';
    }
}

Teacher.java

package com.qfedu.bean;


public class Teacher {
    private int tid;
    private String name;
//set方法
    public void setTid(int tid) {
        this.tid = tid;
    }

    public void setName(String name) {
        this.name = name;
    }

    //toString方法

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", name='" + name + '\'' +
                '}';
    }
}

1.3 创建配置文件

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="student" class="com.qfedu.bean.Student">
        <constructor-arg name="sid" value="1"/>
        <constructor-arg name="name" value="ZhangSan"/>
        <constructor-arg name="age" value="20"/>
        <constructor-arg name="course" value="Java"/>
    </bean>

    <bean id="teacher" class="com.qfedu.bean.Teacher">
        <property name="tid" value="1"></property>
        <property name="name" value="LiSi"></property>
    </bean>
</beans>

2 构造器注入

TestSpring01.java

package com.qfedu.test;

import com.qfedu.bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring01 {
    public static void main(String[] args) throws Exception{
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = applicationContext.getBean("student", Student.class);
        //输出类对象的信息
        System.out.println(student);
    }
}


Alt

3 属性注入

TestSpring02.java

package com.qfedu.test;

import com.qfedu.bean.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring02 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        Teacher teacher = applicationContext.getBean("teacher", Teacher.class);
//        输出Teacher类对象的信息
        System.out.println(teacher);
    }
}

Alt

4 测试问题

Caused by: java.lang.ClassNotFoundException: org.springframework.core.metrics.ApplicationStartup

报错原因:
pom文件依赖版本的问题,Spring核心容器jar包和依赖jar包版本需要统一;这里的Spring版本为5.0.8.RELEASE,所有的Spring 相关的依赖都应该是这个版本。

参考链接: 解决:Caused by: java.lang.ClassNotFoundException: org.springframework.core.metrics.ApplicationStartup

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值