Spring笔记(1)简介及简单配置

spring简介

spring:开源的具有loC功能和AOP思想的java对象/实例容器,管理和装配实例供项目使用

IoC Inversion of Control 控制反转   class代码决定调用改为配置决定调用  
DI Dependency Injection 依赖注入        容器自动匹配并注入依赖的类型  
AOP Aspect Oriented Programming     面向切面编程思想  
OOP Object Oriented Programming     面向对象编程思想

spring配置

1.导入spring的jar包

001.PNG

2.spring的配置文件xxx.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <!-- 管理实例 p1 -->
        <bean id="p1" class="com.senchen.ioc.test.Person">
            <property name="pid"        value="10"/>
            <property name="pname"      value="哈哈1"/>
            <property name="pweight"    value="180"/>
        </bean>
        
        <!-- 管理实例 p2 -->
        <bean id="p2" class="com.senchen.ioc.test.Person">
            <constructor-arg index="1"  value="哼哼"/>
            <constructor-arg type="double"  value="201"/>
            <constructor-arg type="int"     value="20"/>
        </bean>
        
    
    </beans>

3.使用

创建 springIoc 容器, web项目使用 Listener创建

    ApplicationContext cxt = new ClassPathXmlApplicationContext("app.xml");  

从容器中取出要使用的 实例

    Person e = (Person)cxt.getBean("p1");


    public static void main(String[] args) {
        
        //创建 springIoc 容器, web项目使用 Listener创建
        ApplicationContext cxt = new ClassPathXmlApplicationContext("app.xml");
        
        //从容器中取出要使用的 实例
        Person e = (Person)cxt.getBean("p1");
        
        System.out.println("日志1.." +e.getPid());
        System.out.println("日志2.." +e.getPname());
        System.out.println("日志3.." +e.getPweight());
        System.out.println("--------------");
        //从容器中取出要使用的 实例
        Person n = (Person)cxt.getBean("p2");
        System.out.println("日志3.." +n.getPid());
        System.out.println("日志4.." +n.getPname());
        System.out.println("日志5.." +n.getPweight());
        
    }//main()

注入方式

1.set注入

002.PNG

2.构造器注入

003.PNG

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">
        
        <!-- 管理实例 p1 -->
        <bean id="p1" class="com.senchen.ioc.test.Person"/>
        
        <!-- 管理实例 p2 -->
        <bean id="p2" class="com.senchen.ioc.test.Person" scope="prototype"/>
           
        <!-- 管理实例 f  id/name 都可以 -->
        <!-- spring整个容器设置 :default-init-method="f1"   default-destroy-method="f3" default-lazy-init="true" -->
        <bean name="f"  class="com.senchen.ioc.test.FirstMananger" init-method="f1" destroy-method="f3" lazy-init="true"/>
        
        <!-- 给 f 添加别名 fff -->
        <alias name="f" alias="fff"/>
        
    </beans>
scope

<bean id="p2" class="com.senchen.ioc.test.Person" scope="prototype"/>
不写默认为scope="singleton":只创建一次实例
prototype:每次getBean时创建新实例

init-method
destroy-method
lazy-init
alias bean别名
<!-- 管理实例 f  id/name 都可以 -->
<!-- spring整个容器设置 :default-init-method="f1"   default-destroy-method="f3" default-lazy-init="true" -->
<bean name="f"  class="com.senchen.ioc.test.FirstMananger" init-method="f1" destroy-method="f3" lazy-init="true"/>  
<!-- 给 f 添加别名 fff -->
<alias name="f" alias="fff"/>

init-method=""创建对象后立刻调用的方法
destroy-method=""对象销毁前调用的方法
lazy-init="" 默认为true 加载IoC容器时创建实例
false getBean时创建实例
id也可以写为name,推荐使用id

ref:引用数据,一般是另一个bean id值

本类中需要有set方法,name为set方法中set后的名字首字母小写



    public class FirstController {
    
    //本Controller需要的其他类
    FirstManager fmng;
    
    SecondManager secMng;
    
    public void setFm(FirstManager f){
        this.fmng = f;
    }
    

    public void setSecMng(SecondManager secMng) {
        this.secMng = secMng;
    }


    <!-- 管理员 manager 和 controller -->
    <bean id="mng1" class="com.senchen.manager.FirstManager"/>
    <bean id="mng2" class="com.senchen.manager.SecondManager"/>

    <!-- reg 引用     value 值 -->
    <bean id="fctrl" class="com.senchen.controller.FirstController">
        <property name="fm"     ref="mng1"/>
        <property name="secMng" ref="mng2"/>
    </bean>
autowire="byType"自动注入set方法对应的类型不能存在两个类型一致的bean
004.PNG
autowire="byName"自动注入容器中和set方法名一致的bean
005.PNG
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容