注解版

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 4     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
10         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
11         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
13         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
14         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
15         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
16         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
17     <!--
18     #开启动态注解扫描,spring容器会根据base-package设置的包(含子包)下扫描所有java类
19     a.注解下spring管理组件的方式:
20         @Scope("singleton/prototype/request/session")  设置组件的作用域
21             singleton是单例
22             prototype是非单例模式,默认延迟
23             request 每次http请求有各自的bean
24             session 一次会话使用一个bean 实例
25         @Lazy(true/false) 延迟加载
26             true 延迟加载
27             false 非延迟加载
28         @PostConstruct  //该注解告诉spring这个方法是当前bean的  初始化的时候调用的方法
29         @PreDestroy  //销毁的时候调用的方法    只在单例模式才能生效@Scope("singleton")
30     b.注解下的类扫描和依赖注入
31         @Component    :泛指组件,当不好归类时使用
32         @Repository    :标注dao类
33         @Service    :标注业务类
34         @Controller    :标注控制器类
35     c.依赖关系的注入:
36         注意:a.此类注解可以在属性上面设置,也可以在set方法上面设置
37             b.一般用于属性上,因为注解实现下set方法是可以省略不写的
38             c.下面的多个依赖注解,只需要使用一个Resource即可
39         @Value(值)     给一些基本类型(包括String)赋值的注解
40     d.引用关系的依赖注入:
41         @Resource /@Resource(name="xxx") 用于引用类型属性注入,先按名称查找,再按类型查找
42         @Autowired/@Qualifier("xxx")
43             @Autowired 按照类型查找,再按照名称查找
44              @Qualifier("empDaoImpl") 按照指定bean的名称查找
45     -->
46     <context:component-scan base-package="根包名"></context:component-scan>
49 </beans>

配置版

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6     xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  7     xsi:schemaLocation="
  8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 10         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
 11         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
 12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
 13         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
 14         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
 15         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
 16         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
 17     <!-- spring 读取配置文件的标记并根据标记通过反射创建对象 -->
 18     <!-- ====bean实例化例子 ====
 19         #使用构造器创建对象 #
 20         <bean id="唯一标识" name="标识" class="类路径"></bean>
 21
 22         #使用静态工厂实例化
 23         <bean id="唯一标识" class="类路径" > </bean>
 24         <bean id="唯一标识" class="类路径" factory-bean="创建对象的工厂bean对象"  factory-method="创建对象方法名"> </bean>
 25
 26         #使用工厂实例化
 27         <bean id="唯一标识" factory-bean="继承标识" factory-method="创建方法"></bean>
 28         <bean id="继承标识" class="类路径"></bean>
 29
 30         #延时实例化
 31         ==关键属性 :lazy-init="true"
 32         <bean id="唯一标识" class="类路径" lazy-init="true"></bean>
 33
 34         #实例化作用域
 35             属性scope的值:singleton 默认单例
 36                         prototype 多对象
 37                         request 每次http请求有各自的bean
 38                         session 一次会话使用一个bean 实例
 39         <bean id="唯一标识" class="类路径" scope="prototype"></bean>
 40
 41         #有参构造方法注入
 42         <bean id="user1" class="beans.User">
 43             <constructor-arg index="0" value="骆武辉" type="java.lang.String"></constructor-arg>
 44             <constructor-arg index="1" value="luowuhui" type="java.lang.String"></constructor-arg>
 45         </bean>
 46
 47         #属性注入
 48         <bean id="user2" class="beans.User">
 49             <property name="name" value="骆武辉01"></property>
 50             <property name="password" value="luowuhui"></property>
 51         </bean>
 52
 53         #自动装配
 54         <bean id="user3" class="beans.User" autowire="byType"></bean>
 55
 56         #属性注入常见集合
 57         <bean id="exampleBean" class="beans.ExampleBean">
 58             #list集合
 59             <property name="cities">
 60                 <list>
 61                     <value>值</value>
 62                 </list>
 63             </property>
 64             #set集合
 65             <property name="names">
 66                 <set>
 67                     <value>值</value>
 68                 </set>
 69             </property>
 70             #map集合
 71             <property name="map">
 72                 <map>
 73                     <entry>
 74                         <key>
 75                             <value>键名</value>
 76                         </key>
 77                         <value>值</value>
 78                     </entry>
 79                 </map>
 80             </property>
 81             #properties类型的值注入 一
 82             <property name="标识名">
 83                 <props> <prop key="user">root</prop>
 84                 <prop key="password">123123</prop>
 85                 <prop key="url">jdbc:mysql://localhost:3306/test&amp;characterEncoding=utf-8</prop>
 86                 <prop key="driver">com.mysql.jdbc.Driver</prop>
 87             </props> </property>
 88             #properties类型的值注入 二
 89             #通过读.properties 方式注入
 90             #读取配置文件转成Properties对象 id:唯一标识 location:从哪里读取文件 classpath: 通过类路径开始读取
 91             <util:properties id="props" location="classpath:db.properties"></util:properties>
 92             <property name="props" ref="props"></property>
 93         </bean>
 94     -->
163 </beans>

转载于:https://www.cnblogs.com/sylwh/p/7793265.html

Spring框架配置文件 application.xml 示例相关推荐

  1. spring框架里面applicationContext.xml 和spring-servlet.xml 的区别

    问题:spring框架里面applicationContext.xml 和spring-servlet.xml 的区别 在Spring框架中applicationContext.xml和Spring ...

  2. Spring Boot配置文件application.properties

    整理了一份比较全的Spring Boot的配置文件大家可以参考一下. #================================================================ ...

  3. idea自动识别spring框架配置文件,自动提示spring配置

    最近看到其他人在使用idea的时候编辑springboot框架的application.properties配置文件时能够自动提示并且该文件在idea里面显示是的是一片绿叶的样子,而我本地却无提示显示 ...

  4. spring加载application.xml异常

    2019独角兽企业重金招聘Python工程师标准>>> Caused by: org.springframework.beans.factory.xml.XmlBeanDefinit ...

  5. 集成spring框架的web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...

  6. Spring+Hibernate配置文件-applicationContext.xml设置

    搭建完整个工程之后,我们打开applicationContext.xml文件,在这个配置文件中主要涉及的有Spring的相关配置以及Hibernate的相关配置.applicationContext. ...

  7. Spring Boot配置文件 application.properties

    记录Spring Boot application.propertis配置文件的相关通用属性 1 # ================================================= ...

  8. SpringBoot中整合freemarker时配置文件application.properties示例代码

    场景 整合过程参照: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89074931 实现 如果要想配置freemarker的一些 ...

  9. spring cloud 配置文件application.yml和bootstrap.yml 的定位,区别和联系

    最近在启用springcloud配置中心server的东西,在整理属性资源的时候,突然发现:用了这么久的springboot,为什么会配置两个属性文件同时存在(application.yml/prop ...

最新文章

  1. Android canvas.translate
  2. 软件工程师不可不知的10个概念
  3. 树转换为二叉树小技巧
  4. Highly Available (Mirrored) Queues
  5. mysql rpm 安装6_linux6.5 RPM方式安装 mysql5.6
  6. Python练习:百分制到五级制的转换
  7. PHP中使用CURL实现Get和Post请求方法
  8. 3631: [JLOI2014]松鼠的新家
  9. 获取python安装路径
  10. 浅析数据链路层的介质访问控制
  11. 深度学习-对抗神经网络简介
  12. [软件测试]软件测试的原则及软件质量
  13. 模拟NAND FLASH 读写BCH ECC校验
  14. 区块链应用场景:物联网和物流供应链
  15. 每日一练-1-CAD
  16. 环境变量配置步骤(误删除找回方法)
  17. jtag和swd区别,该用哪个?
  18. C++数据类型占据多少个字节
  19. java 输入五种水果_java--IO和面向对象(简单的水果仓库管理系统--可选择操作)...
  20. Android开发之自定义SurfaceView绘制动效音波图 | 动效音阶图 | Android自定义View

热门文章

  1. java如何阻塞父窗体,java图形设计,窗口阻塞的问题。
  2. kafka为什么用java重写,kafka怎么发布订阅 怎么在java中实现
  3. 最大公约数简便算法_最大公约数的求法
  4. 风变编程python网址_风变编程:职场学习Python的重要性
  5. ctfd的mysql导入_centos7 CTFd平台搭建过程
  6. 深度学习常用性能评价指标
  7. MongoDB存在使用Mongo Shell和Java驱动程序的示例
  8. Spring 4 Security MVC登录注销示例
  9. log4j.dtd_Eclipse log4j.xml –无法将log4j.dtd验证为XML定义
  10. 使用PDF-XChange Editor为PDF文件添加图章(仅图片)