Spring学习笔记一

Easter79
• 阅读 676

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[]]>

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

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

//编程
null值和级联属性

默认值就是null

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

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

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

//编程
Properties
HashTable子类

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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"
       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"
       >

    <bean name="helloworld" class="nocollection.HelloWorld">
        <property name="name" value="yaocheng"></property>
    </bean>

    <bean id="car1" class="nocollection.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="dazhong" index="1"></constructor-arg>
        <constructor-arg value="3000" type="double"></constructor-arg>
    </bean>
    <bean id="car2" class="nocollection.Car">
        <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>
                <![CDATA[<beijing>]]>
            </value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>100</value>
        </constructor-arg>
    </bean>
    <bean id="person1" class="nocollection.Person">
        <property name="name" value="Tom"/>
        <property name="age" value="24"/>
        <!--
        <property name="car"><ref bean="car2"></ref></property>
        -->
        <property name="car">
            <bean class="nocollection.Car">
                <constructor-arg value="Ford"/>
                <constructor-arg value="changan"/>
                <constructor-arg value="200000" type="double"/>

            </bean>
        </property>
        <property name="car.maxSpeed" value="100"/>

    </bean>

    <bean id="person2" class="nocollection.Person">
        <constructor-arg value="Jerry"/>
        <constructor-arg value="25"/>
        <constructor-arg ref="car2"/>
    </bean>
    <bean id="person3" class="nocollection.Person">
        <constructor-arg value="Jerry"/>
        <constructor-arg value="25"/>
        <constructor-arg ref="car1"/>
        <property name="car.maxSpeed" value="50"/>
    </bean>


    <bean id="person4" class="collection.Person">
        <property name="name" value="Tom"/>
        <property name="age" value="26"/>
        <property name="cars">
            <list>
                <ref bean="car1"/>
                <ref bean="car2"/>
            </list>
        </property>
    </bean>

    <bean id="persona" class="collection.PersonMap">
        <property name="name" value="rosy"/>
        <property name="age" value="20"/>
        <property name="cars">
            <map>
                <entry key="A" value-ref="car1"></entry>
                <entry key="B" value-ref="car2"></entry>
            </map>
        </property>
    </bean>

    <bean id="datasource" class="collection.DataSource">
        <property name="properties">
            <props>
                <prop key="user">root</prop>
                <prop key="pwd">pwd</prop>
                <prop key="jdbcUrl">jdbc:mysql://test</prop>
                <prop key="driverClass">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

    <util:list id="cars">
        <ref bean="car1"></ref>
        <ref bean="car2"></ref>
    </util:list>

    <bean id="person5" class="collection.Person">
        <property name="name" value="yao"></property>
        <property name="age" value="20"></property>
        <property name="cars" ref="cars"></property>
    </bean>

    <bean id="person6" class="collection.Person" p:name="yao" p:age="20" p:cars-ref="cars">
    </bean>


</beans>
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Easter79 Easter79
2年前
spring注解
随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat
Stella981 Stella981
2年前
Spring 的 IOC 容器
一.BeanFactory 1\.在spring中,最基本的IOC容器接口是BeanFactory这个接口为具体的IOC容器的实现做了最基本的功能规定。  2\.在BeanFactory只对IOC容器的基本行为做了定义,根本不关心你的bean是怎样定义怎样加载的;XmlBeanFactory就是针对最
Stella981 Stella981
2年前
Spring 学习笔记(三):Spring Bean
1Bean配置Spring可以看做是一个管理Bean的工厂,开发者需要将Bean配置在XML或者Properties配置文件中。实际开发中常使用XML的格式,其中<bean中的属性或子元素如下:id:Bean在BeanFactory中的唯一标识,在代码中通过BeanFac
Wesley13 Wesley13
2年前
Spring常用注解
使用注解来构造IoC容器用注解来向Spring容器注册Bean。需要在applicationContext.xml中注册<context:componentscanbasepackage”pagkage1\,pagkage2,…,pagkageN\”/。如:在basepackage指明一个包
Stella981 Stella981
2年前
Spring5.0源码深度解析之Spring基于注解启动流程分析🔥
主要内容:一、IOC容器的初始化流创建IOC容器注册配置类BeanFactory后置处理器Bean的后置处理器创建Bean对象
Stella981 Stella981
2年前
CAS 实现站内单点登录及实现第三方 OAuth、OpenId 登录(四)
一、OAuth配置1.配置OAuth提供商<bean id"weibo" class"com.buession.oauth.provider.impl.WeiboProvider"    <property name"key" value"the_key_for_
Easter79 Easter79
2年前
Spring高级应用之注入嵌套Bean
在Spring中,如果某个Bean所依赖的Bean不想被Spring容器直接访问,可以使用嵌套Bean。和普通的Bean一样,使用<bean元素来定义嵌套的Bean,嵌套Bean只对它的外部的Bean有效,Spring容器无法直接访问嵌套的Bean,因此定义嵌套Bean也无需指定id属性。如下配置片段是一个嵌套Bean的示例:<bean id
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k