Spring-01

导入Spring-webmvc包:

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
    </dependencies>

 今天学了很简单的hellowSpring程序,大致的流程就是创建maven->导入Spring-webmvc包->创建Dao->创建多个DaoImpl->创建Service->创建ServiceImpl(在ServiceImpl中创建privateDao对象)->在ServiceImpl中设置Set方法,设置Dao属性->编写xml配置文件->配置bean对象。

Bean对象和正常对象:

正常来说,应该是 类名 对象名=new 类名();

Bean对象是:

    <bean id="mysqlImpl" class="dao.UserDaoMysqlImpl"/>
    <bean id="UserServiceImpl" class="service.UserServiceImpl">
<!--        ref:引用Spring中已经创建好的对象 -->
<!--        value:具体的值-->
        <property name="userDao" ref="mysqlImpl"/>
    </bean>

id相当于对象名,class相当于类名。如代码看出有的有property,是因为比如第一个mysqlImpl中没有属性,而第二个UserServiceImpl中有userDao属性。而给属性赋值时有ref和value,区别如上图。

这里说几道面试题:

Bean对象和普通对象之间的区别

Spring是使用反射创建的对象,可指定对象的生命周期;如果是直接new的话就是直接创建一个对象。

Spring实现了对象池,一些对象创建和使用完毕之后不会被销毁,放进对象池(某种集合)以备下次使用,下次再需要这个对象,不new,直接从池里取,节省时间。

②什么是IOC

IOC: Inversion of control 反转控制。 比如以前创建一个对象,需要自己主动new 一个对象,通过IOC,对象的创建交由Spring框架 创建,开发人员直接使用已经创建好的对象。

mysqlImpl
posted @ 2022-03-15 23:24  塵暘  阅读(139)  评论(0)    收藏  举报