栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【无标题】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】

Spring-ioc

只使用的spring-ioc这个工厂没有使用自己所创建的工厂

//user实现类
public class User {
    private Integer id;
    private String  name;
    private String  age;
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age='" + age + ''' +
                '}';
    }
    public User() {
    }
    public User(Integer id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}
//配置文件

    
    

测试类

public class Test {
    public static void main(String[] args) {
        //applicationContext:功能更强大
        ApplicationContext factory = new ClassPathXmlApplicationContext("application.xml");
        User user1  = (User) factory.getBean("user1");
//        int count = factory.getBeanDefinitionCount();  //查看配置文件中有多少个对象
//        System.out.println(count);
//       String[] fa =  factory.getBeanDefinitionNames(); //显示对象的名字
//       for(String name:fa) {
//           System.out.println(name);}
             Date date = (Date) factory.getBean("data"); //调用date的类
              System.out.println(date );
    }
}
工厂的静态对象和实例化对象同时结合spring-ioc的使用
public class UserFactory {
    //工厂静态方法
    public static User getElement(){
        System.out.println("静态工厂对象");
        return new User(1001,"神里凌华","20");
    }
    //非静态对象(实例方法创建对象)
    public  User getElement2(){
        System.out.println("实例方法创建对象");
        return new User(1002,"申鹤","21");
    }
}
//配置文件

    
    
    
    
//测试类
public class test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        User user2 = (User) applicationContext.getBean("user2");//因为getBean的低层是object所以需要强制转换
        System.out.println(user2);
        User user3 =(User) applicationContext.getBean("user3");
        System.out.println(user3);
    }
}
使用构造方法创建对象不使用工厂模式
//配置文件

        
        
        
    
//测试类
public class test3 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application2.xml");
        User user1 = (User) applicationContext.getBean("user-1");
        System.out.println(user1);
    }
}

spring-ioc面试可能会问到的题:

spring-ioc已经是一个特别好的大工厂,我该如何从spring-ioc中调用我自己的工厂模式创建的对 象?

如果是静态的话,直接调用factor的getBean如果是非静态方法就先实例化对象然后调用其方法。

.spring-ioc已经是很成熟的工厂了,为什么还要使用其他的工厂模式?

因为spring-ioc是针对的是该类的常规的方式创建的对象某些对象需要经过特殊的处理和特殊的定制。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770575.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号