當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
Spring 创建对象的问题,不同构造方法创建对象,使用工厂类类获取对象、单例或多例、延迟创建问题
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Spring 创建对象的问题,不同构造方法创建对象,使用工厂类类获取对象、单例或多例、延迟创建问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                創建實體類Student
package star.july.b_ioc; public class Student {private String name;private int age;public Student() {System.out.println("執行了Student構造方法");}public Student(String name, int age) {System.out.println("調用了有參構造方法");this.name = name;this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}}再創建工廠類,
package star.july.b_ioc; public class StudentFactory {public StudentFactory() { // new Student(); System.out.println("調用StudentFactory構造方法");}public boolean setHaha(){System.out.println("哈哈");return true;}public Student getStudent(){System.out.println("進入了StudentFactory的getStudent()方法");return new Student();}public static Student getStu(){System.out.println("調用靜態方法");return new Student();} }
配置Spring的xml,applicationContext.xml
 
最后分別測試
 
總結
以上是生活随笔為你收集整理的Spring 创建对象的问题,不同构造方法创建对象,使用工厂类类获取对象、单例或多例、延迟创建问题的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Hibernate 注解映射
 - 下一篇: Spring 依赖注入(DI)