Spring 依赖注入(一、注入方式)

首先装配一个实体类People

复制代码
package com.maya.model;

public class People {
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public People(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public People() {
        super();
    }
}
复制代码

配置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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 属性注入 -->
    <!-- 通过属性名 -->
    <bean id="people1" class="com.maya.model.People">
        <property name="id" value="1"></property>
        <property name="name" value="小明"></property>
        <property name="age" value="22"></property>
    </bean>
    
    <!-- 通过构造方法:可以通过属性名、索引、属性类型-->
    <!-- 通过属性名 -->
    <bean id="people2" class="com.maya.model.People">
        <constructor-arg name="id" value="2"></constructor-arg>
        <constructor-arg name="name" value="小明2"></constructor-arg>
        <constructor-arg name="age" value="12"></constructor-arg>
    </bean>
    
    <!-- 通过索引 -->
    <bean id="people3" class="com.maya.model.People">
        <constructor-arg index="0" value="2"></constructor-arg>
        <constructor-arg index="1" value="小明2"></constructor-arg>
        <constructor-arg index="2" value="12"></constructor-arg>
    </bean>
    
    <!-- 通过属性类型 -->
    <bean id="people4" class="com.maya.model.People">
        <constructor-arg type="int" value="2"></constructor-arg>
        <constructor-arg type="String" value="小明2"></constructor-arg>
        <constructor-arg type="int" value="12"></constructor-arg>
    </bean>
  
  
</beans>
复制代码

读取配置文件

复制代码
public class Test {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");//读取配置文件
        People people1=(People)ac.getBean("people1");
        System.out.println(people1);
        
        People people2=(People)ac.getBean("people2");
        System.out.println(people2);
        People people3=(People)ac.getBean("people3");
        System.out.println(people3);
        People people4=(People)ac.getBean("people4");
        System.out.println(people4);
    }
}
复制代码

 注入静态类与非静态类

public class Factory {
    public People createPeople(){
        People p=new People();
        p.setId(5);
        p.setName("小5");
        p.setAge(55);
        return p;
    }
复制代码
//静态
public class StaticFactory {
    public static People createPeople(){
        People p=new People();
        p.setId(6);
        p.setName("小6");
        p.setAge(66);
        return p;
    }
复制代码
<!-- 通过非静态工厂 -->
      <bean id="peopleFactory" class="com.maya.factory.Factory"></bean>    
    <bean id="people5" factory-bean="peopleFactory" factory-method="createPeople"></bean>
      
      <!-- 通过静态工厂 -->
      <bean id="people6" class="com.maya.factory.StaticFactory" factory-method="createPeople"></bean>    
     People people5=(People)ac.getBean("people5");
        System.out.println(people5);
        
        People people6=(People)ac.getBean("people6");
        System.out.println(people6);

 

posted @   AnswerTheQuestion  阅读(146)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示