spring入门详解

更新时间:02-09 教程 由 心蛊 分享

spring入门详解?

一、Spring概述

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

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

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

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

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

二、Spring入门

1.引入库

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

2.导入Spring配置文件

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

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">

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中配置

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配置:

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配置:

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:默认按照名字匹配然后按照类型匹配

声明:关于《spring入门详解》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2303057.html