网站建设资讯

NEWS

网站建设资讯

Spring实例参考04-通过工厂创建对象-创新互联

本文供以下文章参考使用:

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的青山湖网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

Spring笔记https://blog.csdn.net/qq_34378496/article/details/128095043java bean:

package com.evenif.bean;

public class Index {

    private int id;
    private String name;

    public Index() {
        System.out.println("create Index class");
    }

    public Index(int id, String name) {
        this.id = id;
        this.name = name;
        System.out.println("create Index class with params");
    }
    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;
    }

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

工厂类:

package com.evenif.factory;

import com.evenif.bean.Index;

public class MyFactory {
    public static Index newIndexInstance(int id,String name){
        return new Index(id,name);
    }
}

配置文件:

主测试类:

package com.evenif;

import com.evenif.bean.Index;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //静态工厂获取对象
        Index indexFromFactory = (Index) context.getBean("indexFromFactory");
        System.out.println(indexFromFactory);
        //动态工厂获取对象
        Index indexFromDynamicFactory = (Index) context.getBean("index");
        System.out.println(indexFromDynamicFactory);

        Index indexAlias01 = (Index) context.getBean("index01");
        System.out.println(indexAlias01);
        Index indexAlias02 = (Index) context.getBean("index02");
        System.out.println(indexAlias02);
        Index indexAlias03 = (Index) context.getBean("index03");
        System.out.println(indexAlias03);
        Index indexAlias04 = (Index) context.getBean("index04");
        System.out.println(indexAlias04);
        Index indexAlias05 = (Index) context.getBean("index05");
        System.out.println(indexAlias05);
    }
}
//执行结果:
//        create Index class with params
//        create Index class with params
//        Index{id=7, name='iff07'}
//        Index{id=8, name='ifdf08'}
//        Index{id=8, name='ifdf08'}
//        Index{id=8, name='ifdf08'}
//        Index{id=8, name='ifdf08'}
//        Index{id=8, name='ifdf08'}
//        Index{id=8, name='ifdf08'}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章标题:Spring实例参考04-通过工厂创建对象-创新互联
浏览路径:http://cdweb.net/article/ddpssh.html