尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)
通过id,通过class,和双重方式。
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
User user = (User) context.getBean("user");
System.out.println("通过id获取bean对象:"+user);
User user2 = context.getBean(User.class);
System.out.println("通过类型获取bean对象:"+user2);
User user3 = context.getBean("user", User.class);
System.out.println("通过id和类型获取bean对象:"+user3);
普通属性:String, Interger (set和构造器:感觉还是set比较方便)
<bean id="studentOne" class="com.atguigu.spring6.bean.Student">
<property name="id" value="1001">property>
<property name="name" value="张三">property>
<property name="age" value="23">property>
<property name="sex" value="男">property>
bean>
<bean id="studentTwo" class="com.atguigu.spring6.bean.Student">
<constructor-arg value="1002">constructor-arg>
<constructor-arg value="李四">constructor-arg>
<constructor-arg value="33">constructor-arg>
<constructor-arg value="女">constructor-arg>
bean>
特殊属性:null,特殊的大于小于等(xml转义字符,cdata)
<property name="name">
<null />
property>
<property name="expression" value="a < b"/>
<property name="expression">
<value>value>
property>
对象:外部bean,内部bean,级联(通过.来实现)
外部bean
<bean id="clazzOne" class="com.atguigu.spring6.bean.Clazz">
<property name="clazzId" value="1111">property>
<property name="clazzName" value="财源滚滚班">property>
bean>
<bean id="studentFour" class="com.atguigu.spring6.bean.Student">
<property name="id" value="1004">property>
<property name="name" value="赵六">property>
<property name="age" value="26">property>
<property name="sex" value="女">property>
<property name="clazz" ref="clazzOne">property>
bean>
内部bean
<bean id="studentFour" class="com.atguigu.spring6.bean.Student">
<property name="id" value="1004">property>
<property name="name" value="赵六">property>
<property name="age" value="26">property>
<property name="sex" value="女">property>
<property name="clazz">
<bean id="clazzInner" class="com.atguigu.spring6.bean.Clazz">
<property name="clazzId" value="2222">property>
<property name="clazzName" value="远大前程班">property>
bean>
property>
bean>
级联(通过.来实现)
<bean id="studentFour" class="com.atguigu.spring6.bean.Student">
<property name="id" value="1004">property>
<property name="name" value="赵六">property>
<property name="age" value="26">property>
<property name="sex" value="女">property>
<property name="clazz" ref="clazzOne">property>
<property name="clazz.clazzId" value="3333">property>
<property name="clazz.clazzName" value="最强王者班">property>
bean>
数组
<property name="hobbies">
<array>
<value>抽烟value>
<value>喝酒value>
<value>烫头value>
array>
property>
List
<bean id="clazzTwo" class="com.atguigu.spring6.bean.Clazz">
<property name="clazzId" value="4444">property>
<property name="clazzName" value="Javaee0222">property>
<property name="students">
<list>
<ref bean="studentOne">ref>
<ref bean="studentTwo">ref>
<ref bean="studentThree">ref>
list>
property>
bean>
Map
<bean id="teacherOne" class="com.atguigu.spring6.bean.Teacher">
<property name="teacherId" value="10010">property>
<property name="teacherName" value="大宝">property>
bean>
<bean id="teacherTwo" class="com.atguigu.spring6.bean.Teacher">
<property name="teacherId" value="10086">property>
<property name="teacherName" value="二宝">property>
bean>
<bean id="studentFour" class="com.atguigu.spring6.bean.Student">
<property name="id" value="1004">property>
<property name="name" value="赵六">property>
<property name="age" value="26">property>
<property name="sex" value="女">property>
<property name="clazz" ref="clazzOne">property>
<property name="hobbies">
<array>
<value>抽烟value>
<value>喝酒value>
<value>烫头value>
array>
property>
<property name="teacherMap">
<map>
<entry>
<key>
<value>10010value>
key>
<ref bean="teacherOne">ref>
entry>
<entry>
<key>
<value>10086value>
key>
<ref bean="teacherTwo">ref>
entry>
map>
property>
bean>
util(注意要修改头文件)
<util:list id="students">
<ref bean="studentOne">ref>
<ref bean="studentTwo">ref>
<ref bean="studentThree">ref>
util:list>
<util:map id="teacherMap">
<entry>
<key>
<value>10010value>
key>
<ref bean="teacherOne">ref>
entry>
<entry>
<key>
<value>10086value>
key>
<ref bean="teacherTwo">ref>
entry>
util:map>
<bean id="clazzTwo" class="com.atguigu.spring6.bean.Clazz">
<property name="clazzId" value="4444">property>
<property name="clazzName" value="Javaee0222">property>
<property name="students" ref="students">property>
bean>
<bean id="studentFour" class="com.atguigu.spring6.bean.Student">
<property name="id" value="1004">property>
<property name="name" value="赵六">property>
<property name="age" value="26">property>
<property name="sex" value="女">property>
<property name="clazz" ref="clazzOne">property>
<property name="hobbies">
<array>
<value>抽烟value>
<value>喝酒value>
<value>烫头value>
array>
property>
<property name="teacherMap" ref="teacherMap">property>
bean>
使用p标签(注意要修改头文件)
<bean id="studentSix" class="com.atguigu.spring6.bean.Student"
p:id="1006" p:name="小明" p:clazz-ref="clazzOne" p:teacherMap-ref="teacherMap">bean>
要修改对应的头文件。
外部文件放在了resource目录下。
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
bean>
bean对象创建(调用无参构造器)
给bean对象设置属性
bean的后置处理器(初始化之前)
bean对象初始化(需在配置bean时指定初始化方法:自己设置)
bean的后置处理器(初始化之后)
bean对象就绪可以使用
bean对象销毁(需在配置bean时指定销毁方法:自己设置)
IOC容器关闭
<bean class="com.atguigu.spring6.bean.User" scope="prototype" init-method="initMethod" destroy-method="destroyMethod">
<property name="id" value="1001">property>
<property name="username" value="admin">property>
<property name="password" value="123456">property>
<property name="age" value="23">property>
bean>