spring-依赖注入的实现方式
来源:互联网 发布:外贸客户搜索软件 编辑:程序博客网 时间:2024/06/05 22:13
1.原理
在spring中,所有的POJO都可以被声明,或者定义为一个组件,通过配置文件,自动扫描,java代码的方式,可以任意装配不同组件2.方法
在spring中,装配组件的方式有三种,分别是自动装配,XML配置文件装配以及java代码中显示装配,而组件装配无非就是两个过程1.创建组件;2.装配组件
1.自动装配创建组件
使用注解
@Component("组件名")来创建组件
1
2
3
4
5
<code>
//飞行接口
package
Test;
public
interface
FlyBehavior {
public
void
fly();
}</code>
1
2
3
4
5
6
7
8
9
<code>
//飞行方式具体类
package
Test;
import
org.springframework.stereotype.Component;
@Component
(
"duckfly"
)
public
class
DuckFly
implements
FlyBehavior{
public
void
fly(){
System.out.println(
"I am a duck ,I am flying"
);
}
}</code>
1
2
3
4
5
<code>
//鸭子接口
package
Test;
public
interface
BaseDuck {
public
void
fly();
}</code>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code>
//鸭子类
package
Test;
import
org.springframework.context.annotation.ComponentScan;
import
org.springframework.stereotype.Component;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
javax.annotation.Resource;
@Component
(
"duck"
)
public
class
Duck
implements
BaseDuck{
FlyBehavior flybehavior;
@Resource
(name=
"duckfly"
)
public
void
setFlyBehavior(FlyBehavior flybehavior){
this
.flybehavior = flybehavior;
}
public
void
fly(){
flybehavior.fly();
}
}</code>
如上,创建了一个飞行方式组件(用于注入鸭子类中)和一个鸭子组件(用于注入测试类中)
- 装配组件
使用@Configuration注解用于创建配置类
使用@ComponentScan注解用于开启组件扫描扫描(默认扫描配置类所在包),使用basePackages属性或者basePackageClasses属性设置要扫描的包,或者在配置文件中加入
“`xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<code><code><code>开启组件扫描
使用`
@Resource
(name=
"组件名"
)`或者`
@autowired
`注解方法或者属性注入组件
- 编写测试类加载配置文件运行结果如下
```java
package
Test;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
javax.annotation.Resource;
//用于创建上下文,如果没有上下文无法spring项目无法启动
@RunWith
(SpringJUnit4ClassRunner.
class
)
//指定配置文件
@ContextConfiguration
(classes = DuckConfig.
class
)
public
class
TestDuckFly {
BaseDuck duck;
@Resource
(name=
"duck"
)
public
void
setDuck(BaseDuck duck){
this
.duck = duck;
}
@Test
public
void
test(){
duck.fly();
}
}</code></code></code>
1
2
<code><code><code>十一月
06
,
2017
8
:
41
:
40
下午
I am a duck ,I am flying</code></code></code>
可以看出,duckfly被注入duck类中,duck被注入测试类中
2.通过java代码装配
创建组件(不完全是)
1
2
3
4
<code><code><code>
//飞行接口
public
interface
FlyBehavior {
public
void
fly();
}</code></code></code>
1
2
3
4
5
6
7
<code><code><code>
//飞行类,没有component注解
package
Test;
public
class
DuckFly
implements
FlyBehavior{
public
void
fly(){
System.out.println(
"I am a duck ,I am flying"
);
}
}</code></code></code>
1
2
3
4
5
<code><code><code>
//鸭子接口
package
Test;
public
interface
BaseDuck {
public
void
fly();
}</code></code></code>
1
2
3
4
5
6
7
8
9
10
11
12
<code><code><code>
//鸭子类,没有注解
package
Test;
public
class
Duck
implements
BaseDuck{
FlyBehavior flybehavior;
public
void
setFlyBehavior(FlyBehavior flybehavior){
this
.flybehavior = flybehavior;
}
public
void
fly(){
flybehavior.fly();
}
}</code></code></code>
因为不再是自动装配,所以去除
@Component注解
1
2
3
4
5
<code><code><code><code>
//声明组件
@Bean
(name=
"Duckfly"
)
public
FlyBehavior getFlyBehavior(){
return
new
DuckFly();
}</code></code></code></code>
java代码通过方法来声明一个组件
装配组件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code><code><code><code>
package
Test;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
@Configuration
public
class
DuckConfig{
@Bean
(name=
"duck"
)
public
Duck getDuck(FlyBehavior duckfly){
//装配组件
Duck duck =
new
Duck();
duck.setFlyBehavior(duckfly);
return
duck;
}
@Bean
(name=
"Duckfly"
)
public
FlyBehavior getFlyBehavior(){
//声明组件
return
new
DuckFly();
}
}</code></code></code></code>
同样的,java代码不仅可以通过方法声明组件,还可以通过java代码装配组件,并且装配方式可以是通过构造函数,set方法等等
测试类和结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<code><code><code><code>
package
Test;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
javax.annotation.Resource;
@RunWith
(SpringJUnit4ClassRunner.
class
)
@ContextConfiguration
(classes = DuckConfig.
class
)
public
class
TestDuckFly {
BaseDuck duck;
@Resource
(name=
"duck"
)
public
void
setDuck(BaseDuck duck){
this
.duck = duck;
}
@Test
public
void
test(){
duck.fly();
}
}</code></code></code></code>
1
2
<code><code><code><code>十一月
06
,
2017
9
:
08
:
43
下午
I am a duck ,I am flying</code></code></code></code>
同样的,通过java代码的配置类也能装配组件
XML配置文件装配组件
声明组件
在xml配置文件中,通过标签来声明组件
1
2
3
4
5
6
<code><code><code><code> <bean
class
=
"Test.DuckFly"
id=
"duckfly"
></bean>
<bean
class
=
"Test.Duck"
id=
"duck"
>
<property name=
"flyBehavior"
ref=
"duckfly"
>
</property></bean>
<bean
class
=
"Test.TestDuckFly"
id=
"test"
>
</bean></code></code></code></code>
组装组件
1
2
3
<code><code><code><code><bean
class
=
"Test.Duck"
id=
"duck"
>
<property name=
"flyBehavior"
ref=
"duckfly"
>
</property></bean></code></code></code></code>
上面的代码通过设置属性的方式将duckfly注入到duck之中,还有一种方式是通过构造器注入
测试结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<code><code><code><code><code>
package
Test;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
javax.annotation.Resource;
@org
.springframework.test.context.ContextConfiguration(locations={
"applicationContext.xml"
})
@RunWith
(SpringJUnit4ClassRunner.
class
)
public
class
TestDuckFly {
BaseDuck duck;
@Resource
(name=
"duck"
)
public
void
setDuck(BaseDuck duck){
this
.duck = duck;
}
@Test
public
void
test(){
duck.fly();
}
}</code></code></code></code></code>
需要手动加载application-Context.xml配置文件,运行结果如下
1
2
<code><code><code><code><code>十一月
06
,
2017
9
:
39
:
43
下午
I am a duck ,I am flying</code></code></code></code></code>
以上就是spring中实现依赖注入的三种方式
推荐阅读:胶体果胶铋胶囊 m.120ask.com/yaopin/manual/47955.html
阅读全文
0 0
- spring-依赖注入的实现方式
- Spring依赖注入的方式
- spring-依赖注入的方式
- spring依赖注入的方式
- spring依赖注入的方式
- Spring的依赖注入方式
- spring的依赖注入方式
- Spring的依赖注入方式
- Spring依赖注入(DI)的三种实现方式
- spring依赖注入的3种实现方式
- spring依赖注入的3种实现方式
- spring依赖注入的3种实现方式
- Spring依赖注入(DI)的三种实现方式
- Spring依赖注入的几种实现方式
- spring依赖注入的3种实现方式
- Spring源码阅读 之依赖注入的实现方式
- spring 依赖注入的三种实现方式
- Spring依赖注入的三种实现方式
- k8s--dns插件部署
- 解决Mysql You can change this value on the server by setting the max_allowed_packet' variable. 异常
- 14、Python 3.x 之操作 MySQL
- 斐波那契数列
- Java自学必备的视频教学之面向对象
- spring-依赖注入的实现方式
- 习题7.1
- shadow的socks5服务器搭建
- LeetCode.724 Find Pivot Index
- 别人分析的mips架构的linux启动第一步(作为对比)
- 判断list不为空或者为空需要注意
- postgresql 插入带单引号字符串问题解决
- 编写Qt Designer自定义控件(二)——编写自定义控件界面
- App Transport Security Settings设置