让spring启动时应用不需要加载数据库【default-lazy-init="true"】

spring启动时应用不需要加载数据库,需要时在加载,如何实现?

2010-10-22 11:22 提问者悬赏:15分 |zxqwh| 分类:其他编程语言 | 浏览1195次
也就是说应用启动时不连接数据库,其它不使用数据库的应用也能启动,当用到数据库操作的应用需要操作数据库时,在加载数据库,怎么实现,我说情况和hibernate的延迟加载是不一样的,我是用的是spring+hibernate实现DAO的开发模式。 你的回答好像没有理解我的意思,说的其实好像不是这样实现,我的业务需求是开始时不连接数据库,等用到数据库时在做相应的操作,相当于动态的加载数据库,你说的延迟加载时加载完了,获取对象时候的事了。

<beans>加一个属性:<beans default-lazy-init="true">
就会导致Spring在启动的时候不加载数据库.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-lazy-init="true">



但是不要每个applicationContext-xxx.xml文件都加这个。只是在需要延迟加载的文件里面写上这段。

[img]http://dl2.iteye.com/upload/attachment/0095/4662/14901f76-b0c8-35ef-94a9-9414e2b9c895.jpg[/img]


[code="java"]
2014-3-28 14:24:02 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_34\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.6.0_34\jre\bin;C:/Program Files/Java/jdk1.6.0_34/bin/../jre/bin/server;C:/Program Files/Java/jdk1.6.0_34/bin/../jre/bin;C:/Program Files/Java/jdk1.6.0_34/bin/../jre/lib/amd64;E:\Oracle11gR2\Administrator\product\11.2.0\dbhome_1\bin;.;C:\Program Files\Java\jdk1.6.0_34\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;D:\tools\apache-maven-2.2.1\bin;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files (x86)\Common Files\Easysoft\Shared\;C:\Program Files\Common Files\Easysoft\Shared\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;E:\Microsoft SQL Server\Shared\100\Tools\Binn\;E:\Microsoft SQL Server\Shared\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;D:\sqluldr64;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\Git\bin;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;D:\Program Files (x86);D:\eclipse;;.
2014-3-28 14:24:02 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:wxsc' did not find a matching property.
2014-3-28 14:24:02 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8989
2014-3-28 14:24:02 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 569 ms
2014-3-28 14:24:02 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2014-3-28 14:24:02 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.36
2014-3-28 14:24:03 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
2014-03-28 14:24:03,366 INFO [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started
2014-03-28 14:24:03,408 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing org.springframework.web.context.support.XmlWebApplicationContext@40c07527: display name ; startup date [Fri Mar 28 14:24:03 CST 2014]; root of context hierarchy
2014-03-28 14:24:03,496 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from URL [file:/H:/project249/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/wxsc/WEB-INF/classes/applicationContext.xml]
2014-03-28 14:24:04,135 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from URL [file:/H:/project249/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/wxsc/WEB-INF/classes/applicationContext-persist.xml]
2014-03-28 14:24:04,157 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from URL [file:/H:/project249/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/wxsc/WEB-INF/classes/applicationContext-service.xml]
2014-03-28 14:24:04,176 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from URL [file:/H:/project249/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/wxsc/WEB-INF/classes/applicationContext-service-liuhaihui.xml]
2014-03-28 14:24:04,200 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@40c07527]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bc22eb
2014-03-28 14:24:04,301 INFO [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties]
2014-03-28 14:24:04,353 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bc22eb: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,pabpp,cabpp,dataSource,entityManagerFactory,transactionManager,javaMailSender,txAdvice,org.springframework.aop.config.internalAutoProxyCreator,managerTx,validateService,deleteToolService,request.musicMessageHandler,request.newsMessageHandler,request.textMessageHandler,request.voiceMessageHandler,serverNumMessageHandlerChain,serverNumberMenuService,subscribeNumberMenuService,dataAnalysisService]; root of factory hierarchy
2014-03-28 14:24:04,422 INFO [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 1055 ms
2014-03-28 14:24:04,551 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Parsing configuration file [struts-default.xml]
2014-03-28 14:24:04,665 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Parsing configuration file [struts-plugin.xml]
2014-03-28 14:24:04,691 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Unable to locate configuration files of the name struts.xml, skipping
2014-03-28 14:24:04,691 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Parsing configuration file [struts.xml]
2014-03-28 14:24:04,699 INFO [org.apache.struts2.config.BeanSelectionProvider] - Loading global messages from applicationMessage
2014-03-28 14:24:04,799 INFO [org.apache.struts2.spring.StrutsSpringObjectFactory] - Initializing Struts-Spring integration...
2014-03-28 14:24:04,799 INFO [com.opensymphony.xwork2.spring.SpringObjectFactory] - Setting autowire strategy to name
2014-03-28 14:24:04,800 INFO [org.apache.struts2.spring.StrutsSpringObjectFactory] - ... initialized Struts-Spring integration successfully
2014-03-28 14:24:05,659 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Parsing configuration file [struts-default.xml]
2014-03-28 14:24:05,696 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Parsing configuration file [struts-plugin.xml]
2014-03-28 14:24:05,698 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Unable to locate configuration files of the name struts.xml, skipping
2014-03-28 14:24:05,698 INFO [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider] - Parsing configuration file [struts.xml]
2014-03-28 14:24:05,702 INFO [org.apache.struts2.config.BeanSelectionProvider] - Loading global messages from applicationMessage
2014-03-28 14:24:05,712 INFO [org.apache.struts2.spring.StrutsSpringObjectFactory] - Initializing Struts-Spring integration...
2014-03-28 14:24:05,712 INFO [com.opensymphony.xwork2.spring.SpringObjectFactory] - Setting autowire strategy to name
2014-03-28 14:24:05,713 INFO [org.apache.struts2.spring.StrutsSpringObjectFactory] - ... initialized Struts-Spring integration successfully
2014-3-28 14:24:05 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8989
2014-3-28 14:24:06 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2014-3-28 14:24:06 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/19 config=null
2014-3-28 14:24:06 org.apache.catalina.startup.Catalina start
信息: Server startup in 3512 ms
2014-03-28 14:24:23,813 INFO [com.mchange.v2.log.MLog] - MLog clients using log4j logging.
2014-03-28 14:24:23,899 INFO [com.mchange.v2.c3p0.C3P0Registry] - Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
2014-03-28 14:24:24,054 INFO [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] - Building JPA container EntityManagerFactory for persistence unit 'AppProductPersistenceUnit'
2014-03-28 14:24:24,095 INFO [org.hibernate.cfg.annotations.Version] - Hibernate Annotations 3.4.0.GA
2014-03-28 14:24:24,111 INFO [org.hibernate.cfg.Environment] - Hibernate 3.3.2.GA
2014-03-28 14:24:24,113 INFO [org.hibernate.cfg.Environment] - hibernate.properties not found
2014-03-28 14:24:24,116 INFO [org.hibernate.cfg.Environment] - Bytecode provider name : javassist
2014-03-28 14:24:24,121 INFO [org.hibernate.cfg.Environment] - using JDK 1.4 java.sql.Timestamp handling
2014-03-28 14:24:24,208 INFO [org.hibernate.annotations.common.Version] - Hibernate Commons Annotations 3.1.0.GA
2014-03-28 14:24:24,212 INFO [org.hibernate.ejb.Version] - Hibernate EntityManager 3.4.0.GA
2014-03-28 14:24:24,234 INFO [org.hibernate.ejb.Ejb3Configuration] - Processing PersistenceUnitInfo [
name: AppProductPersistenceUnit
...]
2014-03-28 14:24:24,436 INFO [org.hibernate.cfg.AnnotationBinder] - Binding entity from annotated class: com.kington.wxsc.model.db.OrderStatistic
2014-03-28 14:24:24,472 INFO [org.hibernate.cfg.annotations.EntityBinder] - Bind entity com.kington.wxsc.model.db.OrderStatistic on table wxsc_order_statistics
2014-03-28 14:24:24,537 INFO [org.hibernate.cfg.AnnotationConfiguration] - Hibernate Validator not found: ignoring
2014-03-28 14:24:24,560 INFO [org.hibernate.cfg.search.HibernateSearchEventListenerRegister] - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
2014-03-28 14:24:24,607 INFO [org.hibernate.connection.ConnectionProviderFactory] - Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
2014-03-28 14:24:24,610 INFO [org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider] - Using provided datasource
2014-03-28 14:24:24,665 INFO [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1b60udb9121ivj6qczq01|5fbd7d0e, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1b60udb9121ivj6qczq01|5fbd7d0e, idleConnectionTestPeriod -> 0, initialPoolSize -> 2, jdbcUrl -> jdbc:mysql://localhost:3306/wxsc?useUnicode=true&characterEncoding=UTF-8, lastAcquisitionFailureDefaultUser -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 3, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
2014-03-28 14:24:24,790 INFO [org.hibernate.cfg.SettingsFactory] - RDBMS: MySQL, version: 5.1.41-community
2014-03-28 14:24:24,790 INFO [org.hibernate.cfg.SettingsFactory] - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.4 ( $Date: 2006-10-19 17:47:48 +0200 (Thu, 19 Oct 2006) $, $Revision: 5908 $ )
2014-03-28 14:24:24,812 INFO [org.hibernate.dialect.Dialect] - Using dialect: org.hibernate.dialect.MySQLDialect
2014-03-28 14:24:24,817 INFO [org.hibernate.transaction.TransactionFactoryFactory] - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
2014-03-28 14:24:24,818 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2014-03-28 14:24:24,818 INFO [org.hibernate.cfg.SettingsFactory] - Automatic flush during beforeCompletion(): disabled
2014-03-28 14:24:24,819 INFO [org.hibernate.cfg.SettingsFactory] - Automatic session close at end of transaction: disabled
2014-03-28 14:24:24,819 INFO [org.hibernate.cfg.SettingsFactory] - JDBC batch size: 15
2014-03-28 14:24:24,819 INFO [org.hibernate.cfg.SettingsFactory] - JDBC batch updates for versioned data: disabled
2014-03-28 14:24:24,819 INFO [org.hibernate.cfg.SettingsFactory] - Scrollable result sets: enabled
2014-03-28 14:24:24,820 INFO [org.hibernate.cfg.SettingsFactory] - JDBC3 getGeneratedKeys(): enabled
2014-03-28 14:24:24,820 INFO [org.hibernate.cfg.SettingsFactory] - Connection release mode: auto
2014-03-28 14:24:24,821 INFO [org.hibernate.cfg.SettingsFactory] - Maximum outer join fetch depth: 3
2014-03-28 14:24:24,821 INFO [org.hibernate.cfg.SettingsFactory] - Default batch fetch size: 1
2014-03-28 14:24:24,821 INFO [org.hibernate.cfg.SettingsFactory] - Generate SQL with comments: enabled
2014-03-28 14:24:24,821 INFO [org.hibernate.cfg.SettingsFactory] - Order SQL updates by primary key: disabled
2014-03-28 14:24:24,821 INFO [org.hibernate.cfg.SettingsFactory] - Order SQL inserts for batching: disabled
2014-03-28 14:24:24,821 INFO [org.hibernate.cfg.SettingsFactory] - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2014-03-28 14:24:24,823 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] - Using ASTQueryTranslatorFactory
2014-03-28 14:24:24,823 INFO [org.hibernate.cfg.SettingsFactory] - Query language substitutions: {}
2014-03-28 14:24:24,823 INFO [org.hibernate.cfg.SettingsFactory] - JPA-QL strict compliance: enabled
2014-03-28 14:24:24,823 INFO [org.hibernate.cfg.SettingsFactory] - Second-level cache: enabled
2014-03-28 14:24:24,823 INFO [org.hibernate.cfg.SettingsFactory] - Query cache: disabled
2014-03-28 14:24:24,823 INFO [org.hibernate.cfg.SettingsFactory] - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
2014-03-28 14:24:24,824 INFO [org.hibernate.cfg.SettingsFactory] - Optimize cache for minimal puts: disabled
2014-03-28 14:24:24,824 INFO [org.hibernate.cfg.SettingsFactory] - Structured second-level cache entries: disabled
2014-03-28 14:24:24,828 INFO [org.hibernate.cfg.SettingsFactory] - Echoing all SQL to stdout
2014-03-28 14:24:24,828 INFO [org.hibernate.cfg.SettingsFactory] - Statistics: enabled
2014-03-28 14:24:24,828 INFO [org.hibernate.cfg.SettingsFactory] - Deleted entity synthetic identifier rollback: disabled
2014-03-28 14:24:24,829 INFO [org.hibernate.cfg.SettingsFactory] - Default entity-mode: pojo
2014-03-28 14:24:24,829 INFO [org.hibernate.cfg.SettingsFactory] - Named query checking : enabled
2014-03-28 14:24:24,859 INFO [org.hibernate.impl.SessionFactoryImpl] - building session factory
2014-03-28 14:24:25,014 INFO [org.hibernate.impl.SessionFactoryObjectFactory] - Not binding factory to JNDI, no JNDI name configured
2014-03-28 14:24:25,025 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - Running hbm2ddl schema update
2014-03-28 14:24:25,025 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - fetching database metadata
2014-03-28 14:24:25,030 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - updating schema
2014-03-28 14:24:25,064 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - table found: wxsc.wxsc_order_statistics
2014-03-28 14:24:25,064 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - columns: [delivery_time, delivery_type, gift, value_added_tax_invoice, order_quantity, updatetime, product_id, product_attribute, consignee, invoice, product_name, total_sum, express_type, remarks, order_system_category, express_number, unit_price, version, invoice_content, invoice_type, createtime, id, order_amount, account_payable, place_order_account_number, order_time, customer_payment_term, seller_remarks, payment_confirm_time, order_remarks, order_date, store_name, order_number, customer_address, consignee_telephone_number, customer_name, balance_payment, freight_amount, order_status, post_invoice, article_number, customer_telephone_number, consignee_address, invoice_head, customer_id, order_type]
2014-03-28 14:24:25,064 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - foreign keys: []
2014-03-28 14:24:25,064 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - indexes: [primary]
2014-03-28 14:24:25,064 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - schema update complete

[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值