(01Day)

   
最近在尚硅谷上自学spring框架。既然是自学,那么总结最重要,接下来我把每次学习的总结写成博客以便以后自己回顾和给一些想自学spring框架的人作作参考过。

    何为spring

    spring框架是个轻量级的 JavaEE框架,所谓轻量就是在开发中的对象可以不依赖Spring的API。

   
spring是为了简化企业级应用开发而生的,使用spring我们可以不必自己去创建对象,而交给spring的容器去创建,这就是控制反转(IOC)也可以说成依赖注入(DI),通俗来说控制反转就是把原来由我们自己创建对象的过程交给IOC容器来做。


    首先我们把开发需要准备的jar包和eclipse插件准备好。

    需要的基本jar包


       

    同时需要安装 SPRING TOOL SUITE 插件以便开发中使用


    HelloWorld

    那么接下来按照国际惯例,先来个HelloWorld


    (1)创建一个HelloWorld类
package com.etc; public class HelloWorld { private String userName; public
void setUserName(String userName) { System.out.println("HelloWorld执行了set方法");
this.userName = userName; } public void hello(){
System.out.println("Hello:"+userName); } public HelloWorld() { super();
System.out.println("HelloWorld执行构造方法"); } }
    (2)创建spring的配置文件,通过SPRING TOOL SUITE 插件创建 Spring Bean Configuration File

       注:spring的配置文件名称和位置不是固定的,但是官方建议将配置文件放在src目录下,且命名为applicationContext.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"> <!-- 配置Bean
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参的构造方法
id:标识容器中的Bean。id唯一,注意id的首字母最好小写 --> <bean id="helloWorld"
class="com.etc.HelloWorld"> <!--
(最常用的方式)通过set方法注入,userName就是setUserName方法去掉set,并把首字母小写 --> <property
name="userName" value = "World"></property> </bean> </beans>
    (3)创建一个Main入口来测试
package com.etc; import org.springframework.context.ApplicationContext; import
org.springframework.context.support.ClassPathXmlApplicationContext; public
class Main { public static void main(String[] args) { //1.创建Spring的IOC容器
//ClassPathXmlApplicationContext是ApplicationContext接口的实现类, 该实现类从类路径下加载配置文件。
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器中获取Bean实例
//通过id获取Bean HelloWorld helloworld = (HelloWorld)ctx.getBean("helloWorld");
//通过类型获取Bean,不推荐这样使用,因为如果在appliactionContext.xml中配置了两个HelloWorld,那么IOC容器会不知道要放回哪一个
HelloWorld helloworld2 = (HelloWorld) ctx.getBean(HelloWorld.class);
//3.调用hello方法 helloworld.hello(); helloworld2.hello(); } }
    依赖注入(DI)

现在我们来仔细的看看在spring的IOC容器中依赖注入的方式

    spring支持3种依赖注入的方式



* 属性注入(就是通过Set方法注入)
* 构造器注入(就是通过构造方法注入)
* 工厂方法注入(非常少使用,不推荐)
    (1)属性注入(最常用的方式)

            属性注入需要使用<property>元素使用name属性指定Bean的属性名,value属性或者<value>子节点指定属性值


类似下图这样使用


      (2)构造器注入(比较麻烦)

        通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用,构造器注入在
<constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性。

            举个例子


          (2-1)创建一个实体类
package com.etc; public class Car { private String brand; private String corp;
private double price; private int maxSpeed; public Car(String brand, String
corp, double price) { super(); this.brand = brand; this.corp = corp; this.price
= price; } public Car(String brand, String corp, int maxSpeed) { super();
this.brand = brand; this.corp = corp; this.maxSpeed = maxSpeed; } public String
toString() { return "Car [brand=" + brand + ", corp=" + corp + ", price=" +
price + ", maxSpeed=" + maxSpeed + "]"; } }
            (2-2)创建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
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--
通过构造方法来配置Bean index为属性在构造方法中参数的位置 --> <bean id="car1" class="com.etc.Car">
<constructor-arg value="奥迪" index="0"></constructor-arg> <constructor-arg
value="上海" index="1"></constructor-arg> <constructor-arg value="300000"
index="2"></constructor-arg> </bean> <!--
通过构造方法来配置Bean,可以用指定参数的位置和参数的类型!以区分重载构造方法--> <bean id="car2"
class="com.etc.Car"> <constructor-arg value="奔驰"
type="java.lang.String"></constructor-arg> <constructor-arg value="上海"
type="java.lang.String"></constructor-arg> <constructor-arg value="260"
type="int"></constructor-arg> </bean> </beans>
            (2-3)创建Main入口测试
package com.etc; import org.springframework.context.ApplicationContext; import
org.springframework.context.support.ClassPathXmlApplicationContext; public
class Main { public static void main(String[] args) { ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml"); Car car = (Car)
ctx.getBean("car1"); System.out.println(car); car = (Car) ctx.getBean("car2");
System.out.println(car); } }

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信