springIOC的笔记总结

IOC

控制反转,将bean交给spring容器管理。

P注入_xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"

    <bean id="school" class="bean.School" p:sid="1"
    p:stu-ref="student">
<!--        <property name="stu">-->
<!--            <ref bean = "student" />-->
<!--        </property>-->
    </bean>
</beans>

set注入_构造器注入

<bean id = "student" class = "bean.Student">
        <constructor-arg name = "sid" value = "1"></constructor-arg>
        <constructor-arg name="name" value = "zhangsan"></constructor-arg>
        <constructor-arg name = "age" value="20"></constructor-arg>
        <constructor-arg name = "course" value = "Java"></constructor-arg>
    </bean>
    <bean id="teacher" class="bean.Teacher">
        <property name="tid" value="1">
        </property>
        <property name="name" value="lisi"></property>
    </bean>
    <bean id="mix" class="bean.Mix">
        <property name="myList">
            <list>
                <value>list01</value>
                <value>list02</value>
            </list>
        </property>
        <property name="myMap">
            <map>
                <entry key = "key01" value="map01"></entry>
                <entry key = "key02" value="map02"></entry>
            </map>
        </property>
        <property name="myArray">
            <array>
                <value>array01</value>
                <value>array02</value>
            </array>
        </property>
    </bean>

属性注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.kuang.pojo.Student">
        <property name="name" value="三线码农学编程"></property>

        <property name="books">
            <array>
                <value>java核心技术</value>
                <value>深入了解java虚拟机</value>
                <value>sql必知必会</value>
            </array>
        </property>
       <property name="address" ref="address"></property>
        <property name="hobbys">
            <list>
                <value>code</value>
                <value>play</value>
                <value>run</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="身份证" value="2001"></entry>
                <entry key="银行卡" value="0624"></entry>
            </map>
        </property>
        <property name="wife" >
            <null></null>
        </property>
        <property name="info">
            <props>
                <prop key="学号">201912984</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
    <bean id="address" class="com.kuang.pojo.Address">
        <property name="address" value="武汉"></property>
    </bean>

</beans>

常用依赖

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

注解说明

@Autowired : 自动装配通过类型,名字;默认Bytype

@Resource: 通过Byname 的方式实现
@Component : 类被管理 组件放在类上说明这个类被spring管理
	@repository dao
	@service service
	@controller controller 
@Value :  注入值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
  <context:annotation-config></context:annotation-config>
   
</beans>

衍生的注解

@Component 衍生注解

  • dao【@Repostory】
  • service【@Service】
  • controller【@controller】

注解注入属性 xml管理bean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想去拉萨。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值