day13_spring环境配置及bean使用

1.spring简介:

​ 整合现有的技术框架。是一个轻量级的控制反转(IOC)和面向切面编程的框架(AOP),支持事务的处理和对框架整合的支持

DI(控制反转)就是IOC(依赖注入)的具体实现方式

弊端:配置十分繁琐!

官方下载地址: http://repo.spring.io/release/org/springframework/spring

GitHub地址:GitHub - spring-projects/spring-framework: Spring Framework

spring boot是快速开发的脚手架,微服务,约定大于配置

spring cloud是基于spring boot实现的

2.开发的基本配置
* 1.创建beans.xml spring的配置文件
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用spring创建对象,在spring中这些都称为bean-->
<!--    必须有无参构造才能用下面这个方法-->
<!--    <bean id="hello" class="com.yang.pojo.Hello">
&lt;!&ndash;        name为普通属性用value,其他的用ref&ndash;&gt;
        <property name="str" value="Spring"/>
    </bean>-->

    <!--通过下标获取值-->
    <bean id="hello" class="com.yang.pojo.Hello">
        <constructor-arg index="0" value="spring学习"/>
    </bean>

<!--    若多个参数都是string类型,就不能区分要用哪个 - 不推荐使用-->
    <bean id="hello" class="com.yang.pojo.Hello">
        <constructor-arg type="java.lang.String" value="zhangsan"/>
    </bean>


</beans>
  • 2.主程序中获取spring上下文

    public static void main(String[] args) {
    
            //获取spring的上下文,此时beans.xml中的bean都被实例化了,getBean只是获取已经实例化的其中一个bean
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            //对象现在都在spring中的bean管理了
            Hello hello = (Hello) context.getBean("hello");
            System.out.println(hello);
    
        }
    
3.IOC创建对象的四种方式

​ 第一种必须要有无参构造,后面三种都是使用无参构造创建对象

1.常用的方式(默认),类中必须要有无参构造方法,否则报错!
 <!--    必须有无参构造才能用下面这个方法-->
    <!--        name为普通属性用value,其他的用ref-->
    <bean id="hello" class="com.yang.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>

​ 有dao,service三层结构的写法如下

<!--使用spring创建对象,在spring中这些都称为bean
    ref:引用spring容器中创建好的对象
    value:具体的值,基本数据类型
-->
    <bean id="userDao" class="com.yang.dao.UserDaoImpl"/>
    <bean id="userService" class="com.yang.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
2.使用有参构造创建对象
1.下标赋值法
<!--通过下标获取值 下标指的是有参构造中参数的下标,而且必须要把有参构造中的参数写完整-->
    <bean id="hello" class="com.yang.pojo.Hello">
        <constructor-arg index="0" value="spring学习"/>
        <constructor-arg index="1" value="23"/>
    </bean>
2.属性类型赋值法,若是多个属性都是同一类型,就不好区分了----不推荐使用
<!--    若多个参数都是string类型,就不能区分要用哪个 - 不推荐使用-->
    <bean id="hello" class="com.yang.pojo.Hello">
        <constructor-arg type="java.lang.String" value="zhangsan"/>
    </bean>
3.参数名赋值
<bean id="hello" class="com.yang.pojo.Hello">
<!--        <constructor-arg type="java.lang.String" value="zhangsan"/>-->
        <constructor-arg name="str" value="nihao"/>
    </bean>
4.spring配置文档学习
1.alise 别名 : 同bean标签下的name属性
<!--针对bean中的id取的别名-->    <alias name="hello" alias="newHello"/>
2.bean的配置
<!--通过下标获取值 下标指的是有参构造中参数的下标,而且必须要把有参构造中的参数写完整autowire:自动导入scope:作用域(单例模式)id:bean的唯一标识符,相当于对象名class:bean对象所对应的全限定名。包名+类型,如下name:取别名,可以同时取多个,可以用空格,逗号分号等来区分,如下--><bean id="hello" class="com.yang.pojo.Hello" name="he1 he2,he3;he4">    <constructor-arg index="0" value="spring学习"/>    <constructor-arg index="1" value="23"/></bean>
3.import

​ 用于团队开发,可将多个文件导入合并成一个,注意bean不要重复!

<import resource="beans.xml"/>
##### 	4.各种类型属性的依赖注入
<?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        https://www.springframework.org/schema/beans/spring-beans.xsd">    <import resource="beans.xml"/>    <bean id="addre" class="com.yang.pojo.Address">        <property name="address" value="河南"/>    </bean>    <bean id="student" class="com.yang.pojo.Student">        <property name="name" value="zhangqian"/> <!--    基本属性字段-->        <property name="address" ref="addre"/> <!--    实体类-->        <property name="hobbies" >             <!--    list类型字段-->            <list>                <value>编程</value>                <value>舞蹈</value>                <value>歌曲</value>            </list>        </property>        <property name="games" >            <!--    set类型-->            <set>                <value>LOL</value>                <value>BOB</value>                <value>COC</value>            </set>        </property>        <property name="card" >             <!--    map类型-->            <map>                <entry key="身份证" value="422535454"/>                <entry key="银行卡" value="2324"/>            </map>        </property>        <property name="books" >            <!--    数组类型-->            <array>                <value>红楼梦</value>                <value>西游记</value>                <value>水浒传</value>            </array>        </property>        <property name="wife" >             <!--    string类型-->           <null/>  <!--值为null的时候的写法-->        </property><!--        <property name="wife" value=""/>-->        <property name="info" >             <!--    Properties类型-->            <props merge="true">                <prop key="学号">12345</prop>                <prop key="姓名">张三</prop>                <prop key="性别"></prop>            </props>        </property>    </bean></beans>
public class Student {    private String name;    private Address address;    private List<String> hobbies;    private Map<String,String> card;    private String[] books;    private Set<String> games;    private String wife;    private Properties info;    //address是对象,要想获取对象值,必须.toString()方法,同时Address类中也要重写.toString()方法    @Override    public String toString() {        return "Student{" +                "name='" + name + '\'' +                ", address=" + address.toString() +                ", hobbies=" + hobbies +                ", card=" + card +                ", books=" + Arrays.toString(books) +                ", games=" + games +                ", wife='" + wife + '\'' +                ", info=" + info +                '}';    }        //省略getter,setter方法}

总结依赖注入的三种方式:

1. 构造器注入2. set方式注入(**重点!**),即上面通过属性创建bean的方式 * 依赖:bean对象的创建依赖于容器 * 注入:bean对象中的所有属性都由容器来注入3. 标签注入,即p标签和c标签注入,扩展方式
5.命名空间

​ p命名空间和c命名空间

  1. p命名空间: 属性命名空间,即property简写.必须有无参构造

    先引入头文件

    xmlns:p="http://www.springframework.org/schema/p"
    
    <!--    或者用p标签:先引用p标签路径-->    <bean id="hello" class="com.yang.pojo.Hello" p:age="2" p:name="sansan"/>
    
  2. c命名空间:构造器命名空间,即constructor简写.必须要有有参构造

    先引入头文件

    xmlns:c="http://www.springframework.org/schema/c"
    
<bean id="hello1" class="com.yang.pojo.Hello" c:age="1" c:name="lisi"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值