Spring学习之创建对象实例的三种方式

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

创建对象实例的三种方式我们知道可以通过Spring的IOC容器创建对象的实例,有三种方式:举例:UserDao接口UserDaoImpl实现类:UserDao接口:packagecom.workit.threestyle01;publicinterfaceUserDao{publicvoidprintInfo();}UserDaoImpl实现类:packagecom.workit.threestyle01;publicclassUserDaoImplimplementsUserDao{privateStringjdbcUrl;privateStringdriverClass;privateStringusername;privateStringpassword;@OverridepublicvoidprintInfo(){System.out.println(jdbcUrl=+jdbcUrl);System.out.println(driverClass=+driverClass);System.out.println(username=+username);System.out.println(password=+password);}publicStringgetJdbcUrl(){returnjdbcUrl;}publicvoidsetJdbcUrl(StringjdbcUrl){this.jdbcUrl=jdbcUrl;}publicStringgetDriverClass(){returndriverClass;}publicvoidsetDriverClass(StringdriverClass){this.driverClass=driverClass;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}}beanFactory:packagecom.workit.threestyle01;publicclassBeanFactory{publicstaticUserDaonewInstanceByStaticMethod(){System.out.println(BeanFactoryByStaticMethod.newInstanceByStaticMethod());returnnewUserDaoImpl();}publicUserDaonewInstanceByUnStaticMethod(){System.out.println(BeanFactoryByStaticMethod.newInstanceByUnStaticMethod());returnnewUserDaoImpl();}}三种方式的配置文件的配置:applicationContext.xml通过类的全限定名利用反射创建:?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:aop=:tx=:schemaLocation=://://://://://!--通过反射创建对象实例--beanid=userDaoclass=com.workit.threestyle01.UserDaoImplpropertyname=jdbcUrlvalue=jdbc:oracle:thin:@localhost:1521:oracle10/propertypropertyname=driverClassvalue=oracle.jdbc.driver.OracleDriver/propertypropertyname=usernamevalue=frame/propertypropertyname=passwordvalue=frame/property/bean!--通过对象工厂的静态方法创建对象实例--!--通过对象工厂的非静态方法创建实例--/beans通过对象工厂的静态方法?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:aop=:tx=:schemaLocation=://://://://://!--通过反射创建对象实例--beanid=userDaoclass=com.workit.threestyle01.UserDaoImplpropertyname=jdbcUrlvalue=jdbc:oracle:thin:@localhost:1521:oracle10/propertypropertyname=driverClassvalue=oracle.jdbc.driver.OracleDriver/propertypropertyname=usernamevalue=frame/propertypropertyname=passwordvalue=frame/property/bean!--通过对象工厂的静态方法创建对象实例--beanid=userDao01class=com.workit.threestyle01.BeanFactoryfactory-method=newInstanceByStaticMethodpropertyname=jdbcUrlvalue=jdbc:oracle:thin:@localhost:1521:oracle10/propertypropertyname=driverClassvalue=oracle.jdbc.driver.OracleDriver/propertypropertyname=usernamevalue=frame/propertypropertyname=passwordvalue=frame/property/bean!--通过对象工厂的非静态方法创建实例--/beans通过对象工厂的非静态方法?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:aop=:tx=:schemaLocation=://://://://://!--通过反射创建对象实例--beanid=userDaoclass=com.workit.threestyle01.UserDaoImplpropertyname=jdbcUrlvalue=jdbc:oracle:thin:@localhost:1521:oracle10/propertypropertyname=driverClassvalue=oracle.jdbc.driver.OracleDriver/propertypropertyname=usernamevalue=frame/propertypropertyname=passwordvalue=frame/property/bean!--通过对象工厂的静态方法创建对象实例--beanid=userDao01class=com.workit.threestyle01.BeanFactoryfactory-method=newInstanceByStaticMethodpropertyname=jdbcUrlvalue=jdbc:oracle:thin:@localhost:1521:oracle10/propertypropertyname=driverClassvalue=oracle.jdbc.driver.OracleDriver/propertypropertyname=usernamevalue=frame/propertypropertyname=passwordvalue=frame/property/bean!--通过对象工厂的非静态方法创建实例--!--先配置beanFactory信息--beanid=beanFactoryclass=com.workit.threestyle01.BeanFactory/bea

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功