美文网首页
Spring框架——IOC

Spring框架——IOC

作者: 奔跑_孤傲的路上 | 来源:发表于2017-06-14 18:54 被阅读0次

    spring

    作用:ioc容器,控制反转,将创建对象的权利交给容器去做
    好处:不用new对象,降低了类与类之间的耦合度
    功能:IOC+AOP+DATA+WEB

    spring的原理

    将bean的类名以及类与类的关系配置在xml文件中,通过反射的方式创建对象,并且组装对象。

    spring快速入门

    1.导包
    core、context、expression、bean

    2.引入schema文档(类似dtd文档)来约束xml文档

        <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">
    </beans>
    

    3.通过xml配置文件

         <bean class="com.hemi.bean.CandidateA" id="canA"/>
         <bean class="com.hemi.bean.CandidateB" id="canB"/>
          <bean class="com.hemi.bean.Personnal" id="personnal">
          <!-- 通过构造函数将候选人注入到人事部中  name就是Personnal类中要传进去的对象-->   
          <constructor-arg name="write" ref="canA"></constructor-arg>
          </bean>
    

    4.创建测试类

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Personnal personnal =(Personnal) context.getBean("personnal");
        personnal.interviw();
    }
    

    注入方式

    -构造函数方式注入:
    -constructor-arg:构造函数参数
    -type:使用构造函数参数类型
    -name:使用构造函数参数名
    -index:使用位置 0代表构造函数第一个位置,1代表第二个位置,以此类推

     <bean class="com.hemi.bean.Personnel" id="personnel">
            <constructor-arg index="0" ref="canB" />
            <constructor-arg index="1" ref="canA" />
        </bean>
    

    -get set方法注入
    -property代表属性名称
    -value属性值
    -ref 对象的引用

       <bean class="com.hemi.bean.Personnel" id="personnel">
            <property name="name" value="lili"></property>
            <property name="programme" ref="canA"></property>
        </bean>
    

    -p名称空间
    在文档定义中添加xmlns:p="http://www.springframework.org/schema/p"

    <bean class="com.hemi.bean.Personnel" id="personnel" p:name="lisi"></bean>
    

    总结

    spring ioc容器特点:
    1.在启动的时候会将所有的对象按顺序创建完毕
    2.按需注入
    3.按需获取

    bean参数详解

    id:对象的名字
    destory-method:ioc容器摧毁时创建
    init-method:创建对象时执行的方法
    depends-on:创建对象之前应该创建好的对象
    lazy-init:延迟创建对象
    scope:设置作用域:Singleton(单例)、prototype(多例)、request、session、global session factory-method:工厂方法 factory-bean:工厂对象
    abstract:标记为抽象类

    注解创建对象

    创建一个xml文件

    <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.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.hemi.bean"></context:component-scan>
    </beans>  
    

    创建对象的注解
    -@Component
    -@Service
    -@Reposotory
    -Controller
    用法:

    //创建对象的时候可以使用参数,设置对象的引用变量
    //如果没有写,那么默认使用小驼峰命名
    @Component("blackBox")
    public class BlackBox{
    
    }
    

    note:四者用法一致,一般使用@Service

    注解注入对象

    注入对象的注解
    -@Resource
    -@Autowired

    用法:

        //name:按照名称来查找
        @Resource(name="blackBox")
        private IBox box;
    
        //type:按照类型来查找
        @Resource(type=A4Paper.class)
        private IPaper paper;
    
        //如果没有写,那么name就是参数的变量名 box,所以找不到,然后按照type来查找,IBox类型,所以可以找得到
        //如果没有写,而内存中有多个相同类型的对象,那么就报错
        @Resource
        private IBox box1;
    
        //@Autowired不能写任何参数
        //按照类型来查找,如果内存中有多个相同类型的对象,那么报错
        //解决问题:使用@Qualifier来指定注入哪个名称的对象
        @Autowired
        @Qualifier("blackBox")
        private IBox box;
    

    note:用那个注解根据实际需求选择

    注入数组、集合

    配置xml文件注入

        <bean class="com.hemi.bean.Classes" id="classes">
            <property name="name">
                <list>
                    <value>lisi</value>
                    <value>wangwu</value>
                    <value>zhaoliu</value>
                </list>     
            </property>
            <property name="list">
                <list>
                    <value>1</value>
                    <value>2</value>
                    <value>3</value>
                </list>
            </property>
            <property name="map">
                <map>
                    <entry key="JDBCDriver" value="com.mysql.jdbc.dDriver"></entry>
                    <entry key="username" value="root"></entry>
                </map>
            </property>
        </bean>
    

    相关文章

      网友评论

          本文标题:Spring框架——IOC

          本文链接:https://www.haomeiwen.com/subject/gjdeqxtx.html