文档库 最新最全的文档下载
当前位置:文档库 › spring配置文件详解

spring配置文件详解

spring配置文件详解
spring配置文件详解

1.基本配置:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd ">

class="com.persia.PersonServiceAnnotation">

class="com.persia.PersonServiceAutoInject"autowire="byName">

init-method="init"destroy-method="destory">

method="createInstance"scope="prototype">

第一个

第二个

第三个

第一个l

第二个l

第三个l

value1

value2

value3

ref="personDao">

2.开启AOP:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd ">

class="com.persia.service.MyInterceptor">

class="com.persia.service.impl.PersonServiceImpl">

AOP的xml版本

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd ">

class="com.persia.service.impl.PersonServiceImpl">

names="name"/>

3.开启事务和注解:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xmlns:tx="https://www.wendangku.net/doc/f013721558.html,/schema/tx"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/tx https://www.wendangku.net/doc/f013721558.html,/schema/tx/spring-tx-2.5.xsd ">

class="https://www.wendangku.net/doc/f013721558.html,mons.dbcp.BasicDataSource"destroy-

method="close">

value="jdbc:mysql://localhost:3306/test?useUnicode=true&character Encoding=utf-8"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManag er">

class="com.persia.service.impl.PersonServiceImpl">

XML版本:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xmlns:tx="https://www.wendangku.net/doc/f013721558.html,/schema/tx"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/tx https://www.wendangku.net/doc/f013721558.html,/schema/tx/spring-tx-2.5.xsd ">

class="https://www.wendangku.net/doc/f013721558.html,mons.dbcp.BasicDataSource"destroy-

method="close">

value="jdbc:mysql://localhost:3306/test?useUnicode=true&character Encoding=utf-8"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManag er">

class="com.persia.service.impl.PersonServiceImpl">

ref="transactionPointcut"/>

propagation="NOT_SUPPORTED"/>

4.SSH:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xmlns:tx="https://www.wendangku.net/doc/f013721558.html,/schema/tx"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/tx https://www.wendangku.net/doc/f013721558.html,/schema/tx/spring-tx-2.5.xsd ">

class="https://www.wendangku.net/doc/f013721558.html,mons.dbcp.BasicDataSource"destroy-

method="close">

value="jdbc:mysql://localhost:3306/test?useUnicode=true&character Encoding=utf-8"/>

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

/>

com/persia/model/Person.hbm.xml

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

hibernate.hbm2ddl.auto=update

hibernate.show_sql=false

hibernate.format_sql=false

https://www.wendangku.net/doc/f013721558.html,e_second_level_cache=true https://www.wendangku.net/doc/f013721558.html,e_query_cache=false

hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

class="org.springframework.orm.hibernate3.HibernateTransactionManager ">

class="com.persia.service.impl.PersonServiceImpl">

class="com.persia.struts.PersonManageAction">

5.SSH2:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xmlns:tx="https://www.wendangku.net/doc/f013721558.html,/schema/tx"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/tx https://www.wendangku.net/doc/f013721558.html,/schema/tx/spring-tx-2.5.xsd ">

class="https://www.wendangku.net/doc/f013721558.html,mons.dbcp.BasicDataSource"destroy-

method="close">

value="jdbc:mysql://localhost:3306/test?useUnicode=true&character Encoding=utf-8"/>

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

/>

com/persia/model/Person.hbm.xml

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

hibernate.hbm2ddl.auto=update

hibernate.show_sql=false

hibernate.format_sql=false

https://www.wendangku.net/doc/f013721558.html,e_second_level_cache=true https://www.wendangku.net/doc/f013721558.html,e_query_cache=false

hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

class="org.springframework.orm.hibernate3.HibernateTransactionManager ">

class="com.persia.service.impl.PersonServiceImpl">

class="com.persia.struts2.action.PersonListAction">

6.SSJ:

xmlns="https://www.wendangku.net/doc/f013721558.html,/schema/beans"

xmlns:xsi="https://www.wendangku.net/doc/f013721558.html,/2001/XMLSchema-instance"

xmlns:context="https://www.wendangku.net/doc/f013721558.html,/schema/context"

xmlns:aop="https://www.wendangku.net/doc/f013721558.html,/schema/aop"

xmlns:tx="https://www.wendangku.net/doc/f013721558.html,/schema/tx"

xsi:schemaLocation="https://www.wendangku.net/doc/f013721558.html,/schema/bea ns

https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.5.xsd

https://www.wendangku.net/doc/f013721558.html,/schema/context

https://www.wendangku.net/doc/f013721558.html,/schema/context/spring-context-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/aop

https://www.wendangku.net/doc/f013721558.html,/schema/aop/spring-aop-2.5.xsd https://www.wendangku.net/doc/f013721558.html,/schema/tx https://www.wendangku.net/doc/f013721558.html,/schema/tx/spring-tx-2.5.xsd ">

class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">

class="org.springframework.orm.jpa.JpaTransactionManager">

ref="entityManagerFactory"/>

class="com.persia.service.impl.PersonServiceImpl">

class="com.persia.struts.PersonManageAction"/>

spring配置文件各个属性详解

spring配置文件各个属性详解 分类:spring 2012-08-09 11:25 9316人阅读评论(2) 收藏举报springaophibernateattributesxhtmlwebsphere 目录(?)[+]一、引用外部属性文件 classpath:mail.properties classpath:jdbc.properties 我们定义了一个PropertyPlaceholderConfigurer类的实例,并将其位置属性设置为我们的属性文件。该类被实现为Bean工厂的后处理器,并将使用定义在文件中的属性来代替所有的占位符(${...}value)。 注意: 而在spring2.5的版本中提供了一种更简便的方式,如: 1. 这样以后要使用属性文件中的资源时,可以使用${属性名}来获得。 二、常用数据源的配置 第一种是:DBCP数据源,(需要加入2个jar文件,在spring中的lib下 jakarta-commons/commons-dbcp.jar和commons-pools.jar)主要配置如下:

spring6种配置datasource的方法

在Spring3中,配置DataSource的方法有6种。 JDBCSpringXMLMicrosoftHTML 在Spring3中,配置DataSource的方法有五种。 第一种:beans.xml Xml代码 1. 3. 4. 6. 7. 8. 第二种:beans.xml 2. 3. 4. 6. 7. 8. 9. 10. 在src文件夹里新建一个jdbc.properties文件,里面的内容为如下: Xml代码 1.jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriv er

spring在web.xml中的配置

把如下代码添加到web.xml即可完成spring的基本配置 SetCharacterEncoding org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true SetCharacterEncoding /* contextConfigLocation /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml org.springframework.web.context.ContextLoaderListener

spring配置

作者简介: Craig Walls是Texas-based公司的软件开发人员,有着超过13年的开发经验,涉及的领域有通信,金融,零售,教育以及软件业等。他是Spring Framework的狂热拥护者,频繁的在当地local user groups讨论组和相关会议上演讲Spring,并且他的Blog上也有很多关于Spring的内容。出版的著作有: z Spring in Action, 2nd Edition, 2007 z XDoclet in Action, 2003 他的Blog是: z https://www.wendangku.net/doc/f013721558.html, 所参与的项目: z Committer to XDoclet project; z Originator of Portlet and Spring modules for XDoclet 本手册主要是将分布于文档中的那些零散的配置文件部分统一成一个比较系统的整体。结合Spring文档一起查阅也许能节省你一些时间。不过,并不推荐你全部掌握;很多陌生的元素或标签只应用于特定场合。本手册英文版本可以在:https://www.wendangku.net/doc/f013721558.html,下载。 Spring配置全书 作者Craig Walls 译者superleo 关于Spring的配置 Spring Framework总是不断的改变着Java企业开发的方向,它用一种松耦合的方式来配置和组装应用程序对象和业务对象,比以往的Java企业开发来的更加简洁。一旦你开发了基于Spring 的应用程序,在Spring上下文配置的那些资源简直就是唾手可得。 依赖注入是Spring容器的核心 尽管Spring Framework可以做很多事,但依赖注入却是Spring容器提供的最基本的功能。 任何稍微复杂一点的应用程序都至少由两个或两个以上的对象协作在一起,共同完成一些业务逻辑。以往的Java企业开发,每个对象都要自己去主动获得他们所引用(或依赖)的对象,才可正常运作。这将导致代码之间的紧耦合,难以测试。

Spring中加载xml配置文件的几种方式

项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,就总结了下Spring中加载xml配置文件的方式, xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, XmlWebApplicationContext, ..... 一: XmlBeanFactory 引用资源 1.Resource cr = new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(cr); UserDao userDao = (UserDao)bf.getBean("userDao"); 二: ClassPathXmlApplicationContext 编译路径 使用ClassPathXmlApplicationContext对象获取,必须把applicationContext.xml放置到类的加载路径中,也就是Src下面 1.ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml"); // src目录下的 2.ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao)context.getBean("userDao"); 3.ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext-oracle.xml","applicationContext.xml"}); UserDao userDao = (UserDao)context.getBean("userDao"); // src/conf 目录下的 4.ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml"); 5.ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml"); 三: FileSystemXmlApplicationContext用文件系统

Spring基本配置及其常用方法

搭建spring的环境: 新建User Library:spring,引入spring的核心jar包: SPRING_HOME\dist\spring.jar spring核心jar包 SPRING_HOME \lib\log4j\log4j-1.2.14.jar 记录日志 SPRING_HOME \lib\jakarta-commons\commons-logging.jar 日志的抽象,日志的抽象,如果没有log4j则会借助commons-logging调用sun的记录日志的工具包,如果没有sun的和log4j的则它会调用自己的 在src目录下建立applicationContext.xml (可从SPRING_HOME\ samples\jpetstore\war\WEB-INF\目录下拷贝applicationContext.xml) 设置spring的xml标签的自动提示: Window-->Preferences-->MyEclipse Enterprise Workbench-->Files and Editors-->XML-->XML Catalog-->add-->location中添加Schema文件(SPRING_HOME\dist\resources\spring-beans-2.0.xsd)-->修改Key Type为Schema Location,将key的文件名设为:https://www.wendangku.net/doc/f013721558.html,/schema/beans/spring-beans-2.0.xsd 提供log4j.properties配置文件: (G:\software_programming\Java\SSH\spring\spring-framework-2.0.8-with-dependencies\sp ring-framework-2.0.8\samples\jpetstore\war\WEB-INF\ log4j.properties) 定义bean标签的格式 DI方式从BeanFactory中获取对象: BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); //ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); 定义公共配置为抽象bean: 其他bean继承抽象bean: 属性延迟初始化的配置方法:

Spring配置文件

Spring 配置文件命名空间 1.xml文档的命名空间 XML文档可以创建自定义的元素和属性来描述数据,这样就很容易产生名称相同而含义不同的元素。XML使用命名空间来加以区分。 命名空间使用统一资源定位符URI来识别。URI必须是XML文档中唯一的。 1.1 XML文档中加入命名空间 为了引用一个命名空间,可以将其以属性的形式加入任何包含了属于该命名空间中元素的节点中。 比如: 这决定了命名空间wq指向https://www.wendangku.net/doc/f013721558.html,/ns/furniture 1.2 添加默认命名空间 https://www.wendangku.net/doc/f013721558.html,/ Copyright?2010吴青版权所有

一个XML文档大部分通常属于一个XML词汇。在这种情况下,可以定义一个默认的命名空间,而不是为每一个元素重复地添加命名空间前缀。可以使用xmlns关键字来定义默认的命名空间。这样,就不用为默认的命名空间内的元素指定前缀了。 2.XML的约束模式 XML的约束模式定义了XML文档中允许出现的元素名,元素中的属性,内容类型,以及元素之间的嵌套关系和出现顺序。如果把一个XML文件看做数据库中的一个表,那么XML约束模式就相当于数据库表结构的定义。 约束模式通常都是在一个单独的文件中进行定义,这个文件被称为模式文档。模式文档采用某种约束模式语言编写,用于描述XML文档的结构。 模式语言应用最广泛的就是XML DTD和XML Schema。Spring的配置文件中这两种模式浴盐都支持 https://www.wendangku.net/doc/f013721558.html,/ Copyright?2010吴青版权所有

spring配置文件说明

想必用过Spring的程序员们都有这样的感觉,Spring把逻辑层封装的太完美了(个人感觉View层封装的不是很好)。以至于有的初学者都不知道Spring配置文件的意思,就拿来用了。所以今天我给大家详细解释一下Spring的applicationContext.xml文件。Ok,还是通过代码加注释的方式为大家演示: 以下是详解Spring的applicationContext.xml文件代码: com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8 root 123 com/alonely/vo/User.hbm.xml

12种的有关Spring XML配置文件

Spring是一个强有力的java程序框架,其被广泛应用于java的程序中。它用POJO提供了企业级服务。 Spring利用依赖注入可以获得简单而有效的测试能力。Spring beans,依赖关系,以及服务所需要的bean都将在配置文件中予以描述,配置文件一般采用XML格式。然而XML配置文件冗长而不易使用,在你进行一个使用了大量bean的大项目中它将变得难以阅读和控制。 在这篇文章中我将给你展示12种的有关Spring XML配置文件的最佳技巧。它们中的一些具有更多的实际意义,而不仅是最好的技巧。请注意另外一些因素,例如域模型的设计,会影响到XML配置,但是这篇文章更关注于XML配置的可读性和可操控性。 1.避免使用自动装配 Spring 可以通过bean类的自省来实现自动装配依赖,这样的话你就不必明确地描述bean 的属性或者构造函数的参数。根据属性名称活匹配类型,bean属性可以自动进行装配。而构造函数可以根据匹配类型自动装配。你甚至可以设置自动装配进行自动侦测,这样Spring 替你就会选择一个合适的机制。请看下面的例子: Spring 可以通过bean类的自省来实现自动装配依赖,这样的话你就不必明确地描述bean 的属性或者构造函数的参数。根据属性名称活匹配类型,bean属性可以自动进行装配。而构造函数可以根据匹配类型自动装配。你甚至可以设置自动装配进行自动侦测,这样Spring 替你就会选择一个合适的机制。请看下面的例子: OrderService 类的属性名被用来和容器中的一个bean实例进行匹配。自动装配会默默的保存一些类型信息并降低混乱。然而,由于它会牺牲掉这种配置的直观性和可维护性,你在实际的项目中将不会用到它。许多指南和陈述材料都把它吹捧为Spring的一个非常cool的特性,而没有提到它的这个缺点。依我之见,就像Spring的对象池一样,它更多了一些商业味道。它看起来好像可以使XML配置文件更精简一些,但实际上却增加其复杂性,尤其是在你的较大规模的工程中已经定义了很多bean的时候更是如此。Spring允许你混合使用自动和手动装配,但是这种矛盾会使XML配置更加的令人费解。 2.使用命名规范 和Java 编码的理念一样,在项目中始终用清晰的,描述性的,一致的命名规范对开发人员理解XML配置非常有用。拿bean ID举例来说,你可以遵循Java类中属性的命名规范。比如说,OrderServiceDAO的bean ID应该是orderServiceDAO。对于大项目来说,在bean ID 前加包名来作为前缀。 3.使用简化格式 简化格式有利于减少冗余,因为它把属性值和引用作为属性,而不是子元素。看下面的例子: lizjason

spring学习笔记.doc

spring 学习笔记 【篇一:spring 学习笔记】 4.主要内容: 从下向上看spring 的主要内容: test :spring 支持junit 单元测试 核心容器(ioc) :bean(bean 工厂,创建对象)、core( 一切的基础)、context( 上下文)、spel(spring 的表达式语言); aop :aop 、aspects 对数据访问的支持:jdbc 、orm 、transaction 、jms(java 邮件服务) 对web 的支持:spring mvc 5.ioc--inversion of control 控制反转: 首先引用一篇文章的内容,简单了解一下dao 和service 层,controller 层、view 层(),另一篇文章对spring ioc 设计思想的总 结比较全面和通俗易懂; 第一步:定义dao 接口userdao package cn.sxt.dao;// 降低耦合性public interface userdao { public void getuser();} 第二步:定义dao 接口的实现类userdaomysqlimpl 和userdaooracleimpl userdaomysqlimpl: package cn.sxt.dao.impl;import https://www.wendangku.net/doc/f013721558.html,erdao;public class userdaomysqlimpl implements userdao { @override public void getuser(){ system.out.println(mysql 获取用户数 据);}userdaooracleimpl: package cn.sxt.dao.impl;import https://www.wendangku.net/doc/f013721558.html,erdao;public class userdaomysqlimpl implements userdao { @override public void getuser(){ system.out.println(mysql 获取用户数据);} 第三步:定义service 层接口userservice ,实现具体的业务 package cn.sxt.service;public interface userservice { public void getuser();} 第四步: 定义service 接口的实现类 userserviceimpl,service 层的业务实现,具体要调用到已定义的 dao 层的接口 我们传统的javase 设计思想,代码是这样组织的:我们直接在对象内部(应用程序本身)通过new 进行创建对象,是程序主动去创建依赖对象

Spring定时器配置(XML和注解)

Spring配置定时器(注解+xml)方式—整理 一、注解方式 1.在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="https://www.wendangku.net/doc/f013721558.html,/schema/task" https://www.wendangku.net/doc/f013721558.html,/schema/task https://www.wendangku.net/doc/f013721558.html,/schema/task/spring-task-3.1.xsd 2.最后是我们的task任务扫描注解 3.spring扫描位置 4.写自己的定时任务 @Component//import https://www.wendangku.net/doc/f013721558.html,ponent; public class MyTestServiceImpl implements IMyTestService{ @Scheduled(cron="0/5****?")//每5秒执行一次 public void myTest(){ System.out.println("进入测试"); } } 注意:定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true) 二、XML方式 1.在spring配置文件中创建bean,创建schedule 2.在spring配置文件中创建bean,创建你的triggers 3.在spring配置文件中创建bean,指定定时器作用在那个类那个方法上面

Spring加载配置文件的三种方式

1、把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式 2、采用在web.xml中配置ContextLoaderListenera或ContextLoaderServlet指定加载路径方式。它们两个有着同样的功能,都实现在了org.springframework.web.conte xt.ContextLoader类,都要定义contextConfigLocation参数。区别在于listener不能在Servlet 2.2兼容的容器中使用。自从Servelt 2.4规范,listener被要求在web应用启动后初始化。web.xml初始化的时候,listerner会检查contextConfigLocation参数。如果不存在的话,它将默认使用/WEB-INF/applicationContext.xml。如果它存在,它就会用预先定义的分隔符(逗号,分号和空格)分开分割字符串( contextConfigLocation /WEB-INF/daoContext.xml, /WEB-INF/config/appContext1.xml, /WEB-INF/config/appContext2.xml org.springframework.web.context.ContextLoaderListener< /listener-class>