依赖注入是什么时候注入的 spring入门详解?

[更新]
·
·
分类:互联网
4696 阅读

依赖注入是什么时候注入的

spring入门详解?

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这个名字:
lt?xml version#341.0#34 encoding#34UTF-8#34?gt
ltbeans xmlns##34
xmlns:xsi##34
xsi:schemaLocation#
#34gt
tltbean id#34...#34 class#34...#34gt
tlt!-- collaborators and configuration for this bean go here --gt
tlt/beangt
lt/beansgt
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
3.编写逻辑代码
public class MyBean {
tpublic void hello(){
(#34hello spring...#34)
t}
}
1
2
3
4
5
6
1
2
3
4
5
6
4.将这个类交给Spring去管理即注册到Spring容器中
在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置
ltbeans
ltbean id#34myBean#34 class#_01_#34gtlt/beangt
lt/beansgt
1
2
3
4
1
2
3
4
5.Spring容器的实例化
Spring容器对象有两种:BeanFactory和ApplicationContext(推荐使用)
BeanFactory
@Test
public void testHelloSpring1() throws Exception {
t/**
t *我们第一步是要启动框架,而启动框架则需要拿到Spring的核心对象
t *咱们学习的第一个核心对象是BeanFactory : 顾名思义,这是一个创建Bean的工厂
t *而Bean工厂创建对象又必需拿到配置文件中的数据
t *因为:我们的第一步读取配置文件,拿到BeanFactory工厂t
t */
t
t//第一步:读取资源文件
tResource resource new ClassPathResource(#34applicationContext.xml#34)
t//第二步:拿到核心对象 BeanFactory
tBeanFactory 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 {
t/**
t *我们第一步是要启动框架,而启动框架则需要拿到Spring的核心对象
t *咱们学习的第一个核心对象是BeanFactory : 顾名思义,这是一个创建Bean的工厂
t *而Bean工厂创建对象又必需拿到配置文件中的数据
t *因为:我们的第一步读取配置文件,拿到BeanFactory工厂t
t */
t
t//加载工程classpath下的配置文件实例化
tString conf #34applicationContext.xml#34
tApplicationContext 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)(#34myBean#34)
(bean)
1
2
3
4
1
2
3
4
方式二:通过id与对象的Class对象拿到Bean对象(推荐使用)
//通过id与对象的class拿到Bean对象
MyBean bean (#34myBean#34,)
(bean)
1
2
3
4
1
2
3
4
三、Spring依赖注入
1.xml注入
顾名思义:在xml中进行配置,但是这种方式必须有对应的setter方法,所有这种注入方式又称之为属性注入或setter方法注入
public class MyBean{
tprivate OtherBean otherBean
tpublic void hello(){
ttotherBean.hello()
t}
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{
tpublic void hello(){
(#34otherbean hello#34)
t}
}
1
2
3
4
5
6
1
2
3
4
5
6
//xml配置:
ltbean id#34otherBean#34 class##34gtlt/beangt
ltbean id#34myBean#34 class##34gt
ltproperty name#34otherBean#34 ref#34otherBean#34gtlt/propertygt
lt/beangt
1
2
3
4
5
6
1
2
3
4
5
6
2.注解注入
顾名思义:通过注解实现注入,这种方式可以将注解写在setter方法上,也可以写在字段上,如果写在字段上可以不需要setter方法
2.1方案一:使用@Autowired
@Autowired为Spring提供的注解
public class MyBean{

tprivate OtherBean otherBean
tpublic void hello(){
ttotherBean.hello()
t}
}
public class OtherBean{
tpublic void hello(){
(#34otherbean hello#34)
t}
}
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配置:
ltbean id#34otherBean#34 class##34gtlt/beangt
ltbean id#34myBean#34 class##34gtlt/beangt
1
2
3
1
2
3
2.2方案二:使用@Resource
public class MyBean{
@Resource
tprivate OtherBean otherBean
tpublic void hello(){
ttotherBean.hello()
t}
}
public class OtherBean{
tpublic void hello(){
(#34otherbean hello#34)
t}
}
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
和@Resource区别
@Autowired:默认类型匹配再按照名字匹配
@Resource:默认按照名字匹配然后按照类型匹配

不用spring和用spring的区别?

spring的两大核心功能就是依赖注入DI和AOP,依赖注入实现的功能是不需要自己去new对象而是通过set方法注入,例如把DAO等注入到一个业务逻辑的类中来实现数据库操作,从而使类与类之间的联系更小,耦合度就小。AOP一般用于事务管理。spring功能很强大,如果不用的话可能实现以上功能很复杂的。