Spring-02 快速上手spring

Hello,Spring

上一期中我们理解了IOC的基本思想,我们现在来看下Spring的应用:
HelloSpring

导入Jar包
注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项 .
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>
编写代码

1、编写一个Hello实体类

package com.xuyuan.Pojo;
public class Hello {
    private String str;
    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
}

2、编写我们的spring文件 , 这里我们命名为beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring这些都称为bean
类型 变量名 =new 类型();
Hello  hello  =new Hello();
  id=变量名
  class= new 的对象;
  Property 相当于给对象中的属性这只一个值!
   核心就是set方法注入
-->
 <bean id="Hello" class="com.xuyuan.Pojo.Hello">
     <property name="str" value="Spring">
     </property>
 </bean>
</beans>

3、我们可以去进行测试了 .

import com.xuyuan.Pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
    public static void main(String[] args) {
        //获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们现在的对象都在Spring中管理了,我们要使用,直接去里边取出来即可。
        Hello hello = (Hello) context.getBean("Hello");
        System.out.println(hello.toString());
    }
}
思考
Hello 对象是谁创建的 ?  【hello 对象是由Spring创建的

Hello 对象的属性是怎么设置的 ?  hello 对象的属性是由Spring容器设置的

这个过程就叫控制反转 :

控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的

反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

依赖注入 : 就是利用set方法来进行注入的.

IOC是一种编程思想,由主动的编程变成被动的接收

可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .

修改案例一

我们在案例一中, 新增一个Spring配置文件beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="mysqlImpl" class="com.xuyuan.Dao.UserDaomysqlImpl" />
    <bean id="Orclempl" class="com.xuyuan.Dao.UserDaoOrclempl"/>
    <bean id="UserServiceImpl" class="com.xuyuan.Service.UserServiceImpl">
<property name="userdao" ref="mysqlImpl"/>
    </bean>
</beans>

测试!

import com.xuyuan.Service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getuser();
    }
}

OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

IOC创建对象方式

通过无参构造方法来创建  默认实现
假设要使用有参构造
1、下标赋值
<!--有参构造第一种 下标-->
<bean id="User" class="com.xuyuan.Pojo.User">
    <constructor-arg index="0" value="蓝田县"/>
</bean>
2、类型创建
    <!--有参构造的第二种 通过类型创建 只能传一个的参数 不建议  -->
  <bean id="User" class="com.xuyuan.Pojo.User">
       <constructor-arg type="java.lang.String" value="家乡"/>
  </bean>
3、直接通过类名创建
<!--第三种 直接通过参数名创建    -->
<bean id="User" class="com.xuyuan.Pojo.User">
    <constructor-arg name="name" value="徐塬村"/>
</bean>
1、UserT . java
public class UserT {
     private String name; 
     public UserT(String name) {
        this.name = name;
    } 
    public void setName(String name) {
        this.name = name;
    }
     public void show(){
        System.out.println("name="+ name );
    } 
}

2、beans.xml 有三种方式编写

<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <!-- index指构造方法 , 下标从0开始 -->
    <constructor-arg index="0" value="kuangshen2"/>
</bean>

<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <!-- name指参数名 -->
    <constructor-arg name="name" value="kuangshen2"/>
</bean>

<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

3、测试

import com.xuyuan.Pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("User");
        user.Show();
            }
}

结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

配置

5.1别名

<!--别名 ,如果添加了别名,我们也可以使用别名获取这个对象-->
    <alias name="User" alias="user"/>

5.2 Bean的配置

<!--
id: bean 的唯一标识符,也就是相当于我们学的对象名
class bean  对象所对应的全 限定名 包名+类名
name: 也是 取别名  而且name 可以去多个别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;-->
 <bean id="UserT" class="com.xuyuan.Pojo.UserT" name="usert,u2">
     <property name="name" value="藏歌"/>
    </bean>

5.3 import

团队的合作通过import来实现 可以将多个配置文件合并成一个.
假设 现在项目中有多个人开发,这三个人赋值不同的类开发,不同的类需要注册不同的bean中我们可以利用 import将所有的bean.xml合并成一个总的。
张三
李四
王五
ApplicationContext.xml
使用的时候直接配置就行
智能:多个类时 测试时若相同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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="beans.xml"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xml"/>
</beans>

小狂神温馨提示

到了这里,就算入门Spring了,认真体会它的好处吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值