您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Spring配置详解
Spring配置文件总结首先来看一个标准的Spring配置文件applicationContext.xml?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:context=:tx=:schemaLocation=://://://://://=byNamedefault-lazy-init=true!--配置数据源--beanid=dataSourceclass=org.springframework.jdbc.datasource.DriverManagerDataSourcepropertyname=driverClassNamevaluecom.mysql.jdbc.Driver/value/propertypropertyname=urlvaluejdbc:mysql://localhost/ssh?characterEncoding=utf-8/value/propertypropertyname=usernamevalueroot/value/propertypropertyname=passwordvalue123/value/property/bean!--配置SessionFactory--beanid=sessionFactoryclass=org.springframework.orm.hibernate3.LocalSessionFactoryBeanpropertyname=dataSourcerefbean=dataSource//propertypropertyname=mappingResourceslistvaluecom/ssh/pojo/User.hbm.xml/value/list/propertypropertyname=hibernatePropertiespropspropkey=hibernate.show_sqltrue/prop/props/property/bean!--事务管理--beanid=transactionManagerclass=org.springframework.orm.hibernate3.HibernateTransactionManagerpropertyname=sessionFactoryrefbean=sessionFactory//property/bean!--hibernateTemplate--beanid=hibernateTemplateclass=org.springframework.orm.hibernate3.HibernateTemplatepropertyname=sessionFactoryrefbean=sessionFactory//property/bean!--配置数据持久层--beanid=userDaoclass=com.ssh.dao.impl.UserDaoImplpropertyname=hibernateTemplateref=hibernateTemplate/property/bean!--配置业务逻辑层--beanid=userServiceclass=com.ssh.service.impl.UserServiceImplpropertyname=userDaoref=userDao/property/bean!--配置控制层--beanid=UserActionclass=com.ssh.action.UserActionscope=prototypepropertyname=userServiceref=userService/property/bean!--配置pojo--beanid=Userclass=com.ssh.pojo.Userscope=prototype//beans////////////////////////////////////////////////下面是详解:////////////////////////////////////////////////////////////////////////1.基本配置:?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:context=:schemaLocation=://://://:component-scanbase-package=com.persia!--开启组件扫描--/context:component-scancontext:annotation-config!--开启注解处理器--/context:annotation-config!--使用注解,省去了propertity的xml配置,减少xml文件大小--beanid=personServiceAnnoclass=com.persia.PersonServiceAnnotation/beanbeanid=personDaoBeanAnnoclass=com.persia.PersonDaoBean/beanbeanid=personDaoBeanAnno2class=com.persia.PersonDaoBean/bean!--自动注解--beanid=personServiceAutoInjectclass=com.persia.PersonServiceAutoInjectautowire=byName/beanbeanid=personServiceclass=com.persia.PersonServiceBean!--由spring容器去创建和维护,我们只要获取就可以了--/beanbeanid=personService2class=com.persia.PersonServiceBeanFactoryfactory-method=createInstancelazy-init=trueinit-method=initdestroy-method=destory!--静态工厂获取bean--/beanbeanid=facclass=com.persia.PersonServiceBeanInsFactory/beanbeanid=personService3factory-bean=facfactory-method=createInstancescope=prototype!--实例工厂获取bean,先实例化工厂再实例化bean--/bean!--ref方式注入属性--beanid=personDaoclass=com.persia.PersonDaoBean/beanbeanid=personService4class=com.persia.PersonServiceBeanpropertyname=personDaoref=personDao/property/bean!--内部bean方式注入--beanid=personService5class=com.persia.PersonServiceBeanpropertyname=personDaobeanclass=com.persia.PersonDaoBean/bean/propertypropertyname=namevalue=persia/propertypropertyname=agevalue=21/propertypropertyname=sets!--集合的注入--setvalue第一个/valuevalue第二个/valuevalue第三个/value/set/propertypropertyname=lists!--集合的注入--listvalue第一个l/valuevalue第二个l/valuevalue第三个l/value/list/propertypropertyname=propertiespropspropkey=key1value1/proppropkey=key2value2/proppropkey=key3value3/prop/props/propertypropertyname=mapmapentrykey=key1value=value-1/entryentrykey=key2value=value-2/entryentrykey=key3value=value-3/entry/map/property/beanbeanid=personService6class=com.persia.PersonServiceBeanconstructor-argindex=0value=构造注入的name/constructor-arg!--基本类型可以不写type--constructor-argindex=1type=com.persia.IDaoBeanref=personDao/constructor-arg/bean/beans2.开启AOP:?xmlversion=1.0encoding=UTF-8?beansxmlns=
本文标题:Spring配置详解
链接地址:https://www.777doc.com/doc-6224892 .html