Spring学习笔记一

IOC容器里配置bean
applicationContext.xml
必须有个无参的构造器
class:bean的全类名
通过bean id获得这个bean必须唯一

spring提供2种IOC容器容器实现方式
beanfactory用在spring本身
applicationContext用在开发者
配置方式相同

ApplicationContext有2个主要的实现类
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext

ApplicationContext下面有
ConfigurableApplicationContext启动更新关闭上下文过程

ApplicationContext初始化上下文的时候,实例化所有单例的bean

WebApplicationContext用于web

ApplicationContext的getBean方法读取bean

用类型获取,容器内只能有一个
ctx.getBean(HelloWorld.class);

用id获取
ctx.getBean(helloworld);

注入方式
属性注入(最常用)
构造器注入
工厂方法注入

//编程
区分重载构造器

字面值可用字符串表示的值,通过value标签或者value属性注入,value子节点
//字面值表示特殊字符
<![CDATA[<beijing>]]>

//编程, property 的ref
bean之间的引用

//编程
内部bean不能被外部bean引用

//编程
null值和级联属性
<null/>
默认值就是null

//编程
级联属性
属性先初始化后才能赋值,否则异常

//编程List属性赋值
集合属性List,也能定义内部bean

//编程
集合属性Map,Map有k/v

//编程
Properties
HashTable子类
<props>
<prop>

//编程
集合配置
把集合的配置拿出来做成一个公用的bean
namespace: util.list

//编程
使用p命名空间
namespace: p
加入xmlns:p="http://www.springframework.org/schema/p"

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3       xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" 4       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" 5       > 6 7    <bean name="helloworld" class="nocollection.HelloWorld"> 8        <property name="name" value="yaocheng"></property> 9    </bean> 10 11    <bean id="car1" class="nocollection.Car"> 12        <constructor-arg value="Audi" index="0"></constructor-arg> 13        <constructor-arg value="dazhong" index="1"></constructor-arg> 14        <constructor-arg value="3000" type="double"></constructor-arg> 15    </bean> 16    <bean id="car2" class="nocollection.Car"> 17        <constructor-arg value="Audi" type="java.lang.String"></constructor-arg> 18        <constructor-arg type="java.lang.String"> 19            <value> 20                <![CDATA[<beijing>]]> 21            </value> 22        </constructor-arg> 23        <constructor-arg type="int"> 24            <value>100</value> 25        </constructor-arg> 26    </bean> 27    <bean id="person1" class="nocollection.Person"> 28        <property name="name" value="Tom"/> 29        <property name="age" value="24"/> 30        <!-- 31        <property name="car"><ref bean="car2"></ref></property> 32        --> 33        <property name="car"> 34            <bean class="nocollection.Car"> 35                <constructor-arg value="Ford"/> 36                <constructor-arg value="changan"/> 37                <constructor-arg value="200000" type="double"/> 38 39            </bean> 40        </property> 41        <property name="car.maxSpeed" value="100"/> 42 43    </bean> 44 45    <bean id="person2" class="nocollection.Person"> 46        <constructor-arg value="Jerry"/> 47        <constructor-arg value="25"/> 48        <constructor-arg ref="car2"/> 49    </bean> 50    <bean id="person3" class="nocollection.Person"> 51        <constructor-arg value="Jerry"/> 52        <constructor-arg value="25"/> 53        <constructor-arg ref="car1"/> 54        <property name="car.maxSpeed" value="50"/> 55    </bean> 56 57 58    <bean id="person4" class="collection.Person"> 59        <property name="name" value="Tom"/> 60        <property name="age" value="26"/> 61        <property name="cars"> 62            <list> 63                <ref bean="car1"/> 64                <ref bean="car2"/> 65            </list> 66        </property> 67    </bean> 68 69    <bean id="persona" class="collection.PersonMap"> 70        <property name="name" value="rosy"/> 71        <property name="age" value="20"/> 72        <property name="cars"> 73            <map> 74                <entry key="A" value-ref="car1"></entry> 75                <entry key="B" value-ref="car2"></entry> 76            </map> 77        </property> 78    </bean> 79 80    <bean id="datasource" class="collection.DataSource"> 81        <property name="properties"> 82            <props> 83                <prop key="user">root</prop> 84                <prop key="pwd">pwd</prop> 85                <prop key="jdbcUrl">jdbc:mysql://test</prop> 86                <prop key="driverClass">com.mysql.jdbc.Driver</prop> 87            </props> 88        </property> 89    </bean> 90 91    <util:list id="cars"> 92        <ref bean="car1"></ref> 93        <ref bean="car2"></ref> 94    </util:list> 95 96    <bean id="person5" class="collection.Person"> 97        <property name="name" value="yao"></property> 98        <property name="age" value="20"></property> 99        <property name="cars" ref="cars"></property> 100    </bean> 101 102    <bean id="person6" class="collection.Person" p:name="yao" p:age="20" p:cars-ref="cars"> 103    </bean> 104 105 106</beans>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

spring注解

随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat

Spring 的 IOC 容器

一.BeanFactory 1\.在spring中,最基本的IOC容器接口是BeanFactory这个接口为具体的IOC容器的实现做了最基本的功能规定。  2\.在BeanFactory只对IOC容器的基本行为做了定义,根本不关心你的bean是怎样定义怎样加载的;XmlBeanFactory就是针对最