SSM之Spring学习笔记(2)

一、IOC和DI

1、什么是IOC?

IOC(inversion of controll),控制反转。把对象之间的关系由容器来建立。

2、什么是DI?

DI(pendency injection) 容器通过调用set方法或者构造器来建立对象之间的依赖关系。
注:IOC是目标,DI是手段。

3、通过set方法注入

步骤一:添加相应的set方法。

public class A {
    private IB b;

    public void setB(IB b) {
        System.out.println("setB()" + b);
        this.b = b;
    }

步骤二:在配置文件中,使用property元素进行配置

        <!-- 配置set方法注入 -->
        <!-- 
            property:表示采用set方法注入,其中,
            name指定方法名(会将首字母大写,然后在前面
            添加set形成方法名,比如,这儿会调用setB方法)。
         -->
        <bean id="b1" class="ioc.B"/>
        <bean id="c1" class="ioc.C"/>
        <bean id="a1" class="ioc.A">
            <property name="b" ref="c1"/>
        </bean>

执行过程如下:
这里写图片描述

4、构造器注入

步骤一:添加相应的构造器

public class Phone {
    private Cpu cpu;

    public Phone(Cpu cpu) {
        System.out.println("Phone(Cpu) " + cpu);
        this.cpu = cpu;
    }

步骤二:在配置文件中,使用constructor-arg元素进行配置

        <!-- 配置构造器注入 -->
        <!-- 
            constructor-arg:表示采用构造器注入,
            其中index指定被注入的参数的下标。
         -->
        <bean id="cpu" class="ioc.Cpu"/> 
        <bean id="phone" class="ioc.Phone">
            <constructor-arg index="0" ref="cpu"/>
        </bean>

5、自动装配(了解)

1)在配置文件当中,不用明确告诉容器采用哪种注入方式,也不用告诉
容器被注入的bean的id,由容器依据某些规则,自动建立对象之间的依赖
关系。
注:
a.容器仍然需要调用set方法或者构造器。
b.容器默认情况下,不会自动装配。
2)只需要配置autowire属性:

        <bean id="cp1" class="autowire.Computer"/>
        <bean id="cp2" class="autowire.Computer"/>
        <!-- 
            autowire:表示自动装配。
            byName:容器以属性名作为bean的id,查找是否有
            对应的bean存在,如果存在,就调用对应的set方法,
            如果不存在,不注入(不调用任何方法)。
            byType:容器以属性类型作为bean的类型,查找是否有
            对应的bean存在,如果存在,就调用对应的set方法,
            如果不存在,不注入。
                注:有可能找到多个,则出错。
            constructor:类似于byType,只不过调用的是构造器。
         -->
        <bean id="stu" class="autowire.Student" 
        autowire="byType"/>

注:
a.自动装配要少用。(配置文件虽然简化了,但可读性变差,
另外,容易出错)
b.如果要使用自动装配,尽量使用byName。

6、注入基本类型的值

使用value属性来注入。

创建Teacher类

public class Teacher {
    private String name;
    private int age;

    private List<String> interest;
    private Set<String> city;
    private Map<String,Double> score;
    private Properties db;

创建Student2类

public class Student2 {

    private String name;
    private String interest;
    private double score;
    private int pageSize;
<?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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

        <bean id="stu1" class="value.Student2">
            <property name="name" 
                value="#{t1.name}"/>
            <property name="interest"
                value="#{t1.interest[1]}"/>
            <property name="score"
                value="#{t1.score['英语']}"/>
            <property name="pageSize"
                value="#{config.pagesize}"/>
        </bean>

        <!-- 读取properties文件的内容 -->
        <!-- 
            location:指定要读取的文件的位置。
            注:
                a. classpath: 依据类路径去查找文件。
                b. 容器读取properties文件的内容,并将
                    这些内容存放到Properties对象里面。
         -->
        <util:properties id="config" 
        location="classpath:config.properties"/>

        <!-- 将集合类型的值配置成一个bean -->
        <util:list id="interestBean">
            <value>抽烟</value>
            <value>喝酒</value>
            <value>烫头</value>
        </util:list>
        <util:set id="cityBean">
            <value>金陵</value>
            <value>洛阳</value>
            <value>长安</value>
        </util:set>
        <util:map id="scoreBean">
            <entry key="english" value="90"/>
            <entry key="math" value="80"/>
        </util:map>
        <util:properties id="dbBean">
            <prop key="username">Tom</prop>
            <prop key="password">1234</prop>
        </util:properties>
        <!-- 以引用的方式注入集合类型的值 -->     
        <bean id="t2" class="value.Teacher">
            <property name="interest" 
                ref="interestBean"/>
            <property name="city"
                ref="cityBean"/>
            <property name="score"
                ref="scoreBean"/>
            <property name="db"
                ref="dbBean"/>
        </bean>

        <!-- 
            value:用来注入基本类型的值。
         -->
        <bean id="t1" class="value.Teacher">
            <property name="name" value="小马哥"/>
            <property name="age" value="50"/>
            <property name="interest">
                <list>
                    <value>钓鱼</value>
                    <value>做饭</value>
                    <value>看电视</value>
                    <value>看电视</value>
                </list>
            </property>
            <property name="city">
                <set>
                    <value>杭州</value>
                    <value>北京</value>
                    <value>成都</value>
                </set>
            </property>
            <property name="score">
                <map>
                    <entry key="英语" value="60"/>
                    <entry key="math" value="80"/>
                </map>
            </property>
            <property name="db">
                <props>
                    <prop key="username">Sally</prop>
                    <prop key="password">1234</prop>
                </props>
            </property>
        </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java SSMSpring+SpringMVC+MyBatis)是一种基于Java语言的Web开发框架。学习这个框架的过程中,我深刻体会到它的强大和灵活性。 首先,Spring框架为开发者提供了一个强大的IOC(Inversion of Control)容器,它能够管理和注入对象,减少了代码之间的耦合性。通过配置文件或注解,我们可以轻松地定义和获取各种对象,提高了代码的可维护性和可扩展性。 其次,SpringMVC框架是一种MVC(Model-View-Controller)设计模式的实现,它用于处理Web请求和响应。通过配置一个请求映射表和处理器,我们可以将请求分发给相应的控制器进行处理,并将处理结果返回给客户端。SpringMVC还提供了一些便捷的注解和标签,用于简化页面的渲染和参数的绑定。 最后,MyBatis是一种优秀的持久化框架,它能够将数据库操作与Java对象之间的映射简化为简单的配置。通过编写SQL映射文件和定义POJO(Plain Old Java Object)类,我们可以方便地进行数据库的增删改查操作,而无需编写冗长的SQL语句。 在学习Java SSM框架的过程中,我深入理解了软件开发过程中的MVC思想,并学会了如何利用SpringSpringMVC和MyBatis来实现一个整的Web应用程序。通过不断的实践和调试,我逐渐培养了自己解决问题和调试代码的能力。 总结起来,学习Java SSM框架使我深入理解了软件开发的各个环节,并提升了我的编码能力和开发效率。我相信这些知识和经验将对我的职业发展和项目实施起到积极的促进作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值