• 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);
  • 相关阅读:
    C#枚举(一)使用总结以及扩展类分享
    .NET使用DinkToPdf将HTML转成PDF
    .NET使用MailKit进行邮件处理
    socket+django
    mysql 修改编码格式
    django中Model表的反向查询
    浅谈 session 会话的原理
    浅谈 django Models中的跨表
    django Models 常用的字段和参数
    encoding/xml
  • 原文地址:https://www.cnblogs.com/claricre/p/6636791.html
Copyright © 2020-2023  润新知