详解spring applicationContext.xml 配置文件

作者:袖梨 2022-06-25

applicationContext.xml 文件

代码如下 复制代码

xmlns:tx="http://www.sp*ri*ngframe*work.org/schema/tx"xmlns:p="http://www.**sp*ringframework.org/schema/p"xmlns:util="http://www.spr*in*gframe*work.org/schema/util"xmlns:jdbc="http://www.sp*r*ingfra*mework.org/schema/jdbc"

xmlns:cache="http://www.spri*n*gfr*amework.org/schema/cache"

xsi:schemaLocation="

http://www.spri*n*gframewo*rk.org/schema/context

http://www.spri*n*gframewo*rk.org/schema/context/spring-context.xsd

http://www.springf**ra*mework.org/schema/beans

http://www.springf**ra*mework.org/schema/beans/spring-beans.xsd

http://www.sp*ri*ngframe*work.org/schema/tx

http://www.sp*ri*ngframe*work.org/schema/tx/spring-tx.xsd

http://www.sp*r*ingfra*mework.org/schema/jdbc

http://www.sp*r*ingfra*mework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://www.spri*n*gfr*amework.org/schema/cache

http://www.spri*n*gfr*amework.org/schema/cache/spring-cache-3.1.xsd

http://www.spr*i*ngframewo*rk.org/schema/aop

http://www.spr*i*ngframewo*rk.org/schema/aop/spring-aop.xsd

http://www.spr*in*gframe*work.org/schema/util

http://www.spr*in*gframe*work.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;

相关文章

精彩推荐