综合百科

spring入门详解

Spring是一个轻量级的Java框架,它提供了许多功能强大的工具和库,用于简化Java开发过程中的许多常见任务。如果您想深入了解Spring,以下是一些入门详解:

1. Spring框架的核心概念:Spring框架由多个模块组成,每个模块都提供了不同的功能。其中最核心的概念是IoC(Inversion of Control)和DI(Dependency Injection),它们可以帮助我们管理应用程序中的对象和依赖关系。

2. Spring的核心容器:Spring框架的核心容器是BeanFactory和ApplicationContext,它们可以管理应用程序中的对象和依赖关系。BeanFactory是一个较早的版本,而ApplicationContext是一个较新的版本,它提供了更多的功能和扩展。

3. Spring MVC:Spring MVC是Spring框架中的一个模块,它提供了一个基于模型-视图-控制器(MVC)模式的Web应用程序开发框架。它可以帮助我们轻松地创建Web应用程序,并提供了许多有用的功能,如数据绑定、表单验证和国际化等。

4. Spring Boot:Spring Boot是一个快速开发框架,它可以帮助我们快速创建基于Spring框架的Web应用程序。它可以自动配置许多常见的Spring特性,如数据源、消息队列和安全性等。

5. Spring Security:Spring Security是一个用于身份验证和授权的框架,它可以帮助我们保护Web应用程序免受未经授权的访问。它提供了许多有用的功能,如身份验证、授权、密码重置等。

一、Spring概述

Spring是一个轻量级的DI/IOC和AOP的容器框架

  轻量级:简单好用,通常来说功能不强大(但spring功能强大)

  DI(依赖注入):动态的向某个对象提供它所需要的其他对象,也可以为对象的属性字段赋值。(依赖注入又分为xml注入和注解注入)

  IOC(控制翻转):由spring控制对象的生命周期(创建,销毁)

  AOP(面向切面编程):解决重复代码。将相同的逻辑抽取出来,即将业务逻辑从应用服务中分离出来。然后以拦截的方式作用在一个方法的不同位置。

二、Spring入门

1.引入库

导包的时候注意,现在使用Spring,要完成最小导包,即:需要什么jar包,我们就导入什么jar包,用到了其他功能,再添加相应jar包。这个对认识框架的包是非常有帮助的:

2.导入Spring配置文件

1. 在classpath的根目录下新建一个applicationContext.xml配置文件,文件名可以自定义,但是通常使用applicationContext这个名字:

<?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 id="..." class="...">

<!-- collaborators and configuration for this bean go here -->

</bean>

</beans>

1

2

3

4

5

6

7

8

9

10

1

2

3

4

5

6

7

8

9

10

3.编写逻辑代码

public class MyBean {

public void hello(){

System.out.println("hello spring...");

}

}

1

2

3

4

5

6

1

2

3

4

5

6

4.将这个类交给Spring去管理即注册到Spring容器中

在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置

<beans ...>

<bean id="myBean" class="cn.itsource._01_hello.MyBean"></bean>

</beans>

1

2

3

4

1

2

3

4

5.Spring容器的实例化

Spring容器对象有两种:BeanFactory和ApplicationContext(推荐使用)

BeanFactory

@Test

public void testHelloSpring1() throws Exception {

/**

*我们靠前步是要启动框架,而启动框架则需要拿到Spring的核心对象

*咱们学习的靠前个核心对象是BeanFactory : 顾名思义,这是一个创建Bean的工厂

*而Bean工厂创建对象又必需拿到配置文件中的数据

*因为:我们的靠前步读取配置文件,拿到BeanFactory工厂

*/

//靠前步:读取资源文件

Resource resource = new ClassPathResource("applicationContext.xml");

//第二步:拿到核心对象 BeanFactory

BeanFactory factory = new XmlBeanFactory(resource);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

ApplicationContext(推荐使用)

@Test

public void testHelloSpring2() throws Exception {

/**

*我们靠前步是要启动框架,而启动框架则需要拿到Spring的核心对象

*咱们学习的靠前个核心对象是BeanFactory : 顾名思义,这是一个创建Bean的工厂

*而Bean工厂创建对象又必需拿到配置文件中的数据

*因为:我们的靠前步读取配置文件,拿到BeanFactory工厂

*/

//加载工程classpath下的配置文件实例化

String conf = "applicationContext.xml";

ApplicationContext factory = new ClassPathXmlApplicationContext(conf);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

6.获取对象方式

方式一:通过id直接拿到相应的Bean对象

//通过xml中配置的id拿到对象

MyBean bean = (MyBean)factory.getBean("myBean");

System.out.println(bean);

1

2

3

4

1

2

3

4

方式二:通过id与对象的Class对象拿到Bean对象(推荐使用)

//通过id与对象的class拿到Bean对象

MyBean bean = factory.getBean("myBean",MyBean.class);

System.out.println(bean);

1

2

3

4

1

2

3

4

三、Spring依赖注入

1.xml注入

顾名思义:在xml中进行配置,但是这种方式必须有对应的setter方法,所有这种注入方式又称之为属性注入或setter方法注入

public class MyBean{

private OtherBean otherBean;

public void hello(){

otherBean.hello();

}

public void setOtherBean(OtherBean otherbean){

this.OtherBean = OtherBean

}

}

1

2

3

4

5

6

7

8

9

10

1

2

3

4

5

6

7

8

9

10

public class OtherBean{

public void hello(){

System.out.println("otherbean hello");

}

}

1

2

3

4

5

6

1

2

3

4

5

6

//xml配置:

<bean id="otherBean" class="cn.itsource.bean.OtherBean"></bean>

<bean id="myBean" class="cn.itsource.bean.MyBean">

<property name="otherBean" ref="otherBean"></property>

</bean>

1

2

3

4

5

6

1

2

3

4

5

6

2.注解注入

顾名思义:通过注解实现注入,这种方式可以将注解写在setter方法上,也可以写在字段上,如果写在字段上可以不需要setter方法

2.1方案一:使用@Autowired

@Autowired为Spring提供的注解

public class MyBean{

@Autowired

private OtherBean otherBean;

public void hello(){

otherBean.hello();

}

}

public class OtherBean{

public void hello(){

System.out.println("otherbean hello");

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//xml配置:

<bean id="otherBean" class="cn.itsource.bean.OtherBean"></bean>

<bean id="myBean" class="cn.itsource.bean.MyBean"></bean>

1

2

3

1

2

3

2.2方案二:使用@Resource

public class MyBean{

@Resource

private OtherBean otherBean;

public void hello(){

otherBean.hello();

}

}

public class OtherBean{

public void hello(){

System.out.println("otherbean hello");

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

1

2

3

4

5

6

7

8

9

10

11

12

13

14

2.3@Autowired和@Resource区别

@Autowired:默认类型匹配再按照名字匹配

@Resource:默认按照名字匹配然后按照类型匹配。