Spring笔记day01

1.spring简介

  • spring是一个full-stack轻量级开源的框架(代码轻 配置重(Spring Boot出现))
  • Spring两大核心IoC 和 AOP
    IoC 控制反转(反转的是bean的创建权:原来手动创建Bean的权利反转给Spring框架(Spring容器))
    AOP 面向切面编程(底层实现:动态代理)
  • Spring优点
    解耦:IoC
    AOP编程的支持
    声明式事务控制(对等的是编程式事务控制)
    方便程序测试(spring可以集成junit)
    集成各种优秀框架(ssh整合)
    降低API的使用难度(XXXTemplate: jdbcTemplate hibernateTemplateredisTemplate、jmsTemplate… …)

2.spring快速开发

1.导入spring开发的jar
spring4个基本包:core、beans、context、expression
log相关:commons-logging.jar   log4j.jar(log4j.properties)
2.定义bean
3.配置bean
<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="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
</beans>
4.通过Spring的API获得Bean实例对象
//加载配置文件 创建spring容器
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) app.getBean("userDao");

2.spring配置详解

1.bean标签

id 给对象在容器中提供唯一标识,用于获取对象
class 指定类的全限定类名,用于反射获取对象,默认调用无参构造
scope:指定对象的作用范围
singleton:单例,默认值
prototype:多例
init-method:指定类中初始化方法名称
destroy-method:指定类中销毁方法名称

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton"></bean>
<bean id="userDao2" class="com.itheima.dao.impl.UserDaoImpl" init-method="init" destroy-method="destroy"></bean>

2.实例化bean的三种方式

  • 无参构造
  • 使用静态工厂方法创建对象
    class属性:指定静态工厂的全限定名
    factory-method:指定生产对象的静态方法
   <bean id="userDao2" class="com.itheima.factory.FactoryBean" factory-method="getUserDao"></bean> 
  • 使用实例工厂方法创建对象
    factory-bean 指定实例工厂bean的id
    factory-method 指定实例工厂中创建对象的方法
<bean id="factoryBean" class="com.itheima.factory.FactoryBean"></bean>
 <bean id="userDao2" factory-bean="factoryBean" factory-method="getUserDao"></bean>

3.依赖注入的方式

  • 构造函数注入
<bean id="user4" class="com.itheima.domain.User">
        <constructor-arg name="name" value="hanmeimei"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
</bean>
  • set方法注入
    ref属性 给属性赋值是其他bean类型的
<bean id="user1" class="com.itheima.domain.User">
        <property name="name" value="jack"></property>
        <property name="age" value="18"></property>
</bean>
  • 使用p名称空间注入数据
<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"
       ...
 <bean id="user3" class="com.itheima.domain.User"  p:name="lilei" p:age="20"> </bean>
</beans>
  • 注入集合类型
<bean id="user1" class="com.itheima.domain.User">
     <property name="list">
            <list>
                <value>渣渣穆</value>
                <value>皮皮飞</value>
                <value>渣渣杰</value>
            </list>
        </property>
</bean>
  • 注入map类型
<property name="map">
    <map>
       <entry key="user1" value-ref="user1"></entry>
       <entry key="user2" value-ref="user2"></entry>
     </map>
</property>
  • 注入Propeties类型
<property name="prop">
     <props>
          <prop key="username">root</prop>
          <prop key="password">123456</prop>
     </props>
 </property>

对象注入,注入对象与被注入的对象都必须存在于容器中.

猜你喜欢

转载自blog.csdn.net/sinat_29211659/article/details/81335229
今日推荐