一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

详解spring applicationContext.xml 配置文件

时间:2017-03-09 编辑:简简单单 来源:一聚教程网

applicationContext.xml 文件

 

 代码如下 复制代码

  xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xmlns:jdbc="http://www.springframework.org/schema/jdbc"

  xmlns:cache="http://www.springframework.org/schema/cache"

  xsi:schemaLocation="

  http://www.springframework.org/schema/context

  http://www.springframework.org/schema/context/spring-context.xsd

  http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans.xsd

  http://www.springframework.org/schema/tx

  http://www.springframework.org/schema/tx/spring-tx.xsd

  http://www.springframework.org/schema/jdbc

  http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

  http://www.springframework.org/schema/cache

  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

  http://www.springframework.org/schema/aop

  http://www.springframework.org/schema/aop/spring-aop.xsd

  http://www.springframework.org/schema/util

  http://www.springframework.org/schema/util/spring-util.xsd">

  

  

  

  

  

  

    

      

        classpath*:jdbc.properties

      

    

  

  

  

  

    

    

    

    

  

    

    

    

    

  

    

    

  

    

    

  

    

    

  

    

    

    

    

  

    

    

    

  

    

    

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

 

1、  作用Spring 容器初始化的时候,会扫描 com.eduoinfo.finances.bank.web下 标有  (@Component,@Service,@Controller,@Repository) 注解的 类 纳入spring容器管理

在类上 ,使用以下注解,实现bean 的声明

@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Service 用于标注业务层组件

@Controller 用于标注控制层组件(如srping mvc的controller,struts中的action)

@Repository 用于标注数据访问组件,即DAO组件

示例:

@Controller

@RequestMapping(value ="/test")

publicclassTestController {

 

}

在类的成员变量上,使用以下注解,实现属性的自动装配

@Autowired : 按类 的 类型进行装配

@Resource (推荐) :

1 如果同时指定了name和type,则从spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 

3.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

4.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。

示例:

@Resource

privateTestServiceImpl testServiceImpl;

热门栏目