java spring di_JAVA-Spring注解方式实现IOC和DI

1.Spring注解

Spring除了默认的使用xml配置文件的方式实现配置之外,也支持使用注解的方式实现配置,这种方式效率更高,配置信息更清晰,修改更方便,推荐使用。

所谓注解就是给程序看的提示信息,很多时候都是用来做为轻量级配置的方式。

关于注解的知识点,可以看我上篇随笔内容。

2.Spring引入context名称空间

在MyEclipse中导入spring-context-3.2.xsd约束文件,要求Spring来管理。

在applicationContext.xml文件中,引入该schema文件:

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd7 http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context-3.2.xsd9 ">

10

**可以将以上头信息加入MyEclipse模板中,方便后续自动生成使用。

3.Spring注解实现IOC

(1)开启包扫描

在spring的配置文件中,开启包扫描,指定spring自动扫描哪些个包下的类。只有在指定的扫描包下的类上的IOC注解才会生效。

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd7 http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context-3.2.xsd9 ">

10         

11         

12         

17

(2)使用注解注册bean

在配置的包中的类上使用@Component注解,则这个类会自动被注册成为bean,使用当前类的class为的class,通常情况下使用类名首字母小写为id。

案例:

1 packagecn.tedu.beans;2 importorg.springframework.stereotype.Component;3

4 @Component5 public classPerson{6 }

(3)bean的id

通常情况下注解注册bean使用类名首字母小写为bean的id,但是如果类名第二个字母为大写则首字母保留原样。

1 cn.tedu.beans.Person -->

2 cn.tedu.beans.PErson -->

3 cn.tedu.beans.NBA -->

也可以通过在@Component中配置value属性,明确的指定bean的id

案例:

**可以使bean实现BeanNameAware接口,并实现其中的setBeanName方法。

spring容器会在初始化bean时,调用此方法告知当前bean的id。通过这个方式可以获取bean的id信息。

1 packagecn.tedu.beans;2 importorg.springframework.beans.factory.BeanNameAware;3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.beans.factory.annotation.Qualifier;5 importorg.springframework.stereotype.Component;6

7 @Component("per")8 public class Person implementsBeanNameAware{9 @Override10         public voidsetBeanName(String name) {11                 System.out.println("==="+this.getClass().getName()+"==="+name);12 }13 }

4.Spring注解方式实现DI

(1)在配置文件中开启注解实现DI

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:util="http://www.springframework.org/schema/util"

5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

6 xsi:schemaLocation="http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd8 http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-3.2.xsd10 http://www.springframework.org/schema/util11 http://www.springframework.org/schema/util/spring-util-3.2.xsd12 ">

13         

14         

15         

16         

17

(2)注解方式注入spring内置支持的类型数据 - 非集合类型

spring中可以通过@Value来实现spring内置支持的类型的属性的注入。

1 packagecn.tedu.domain;2

3 importorg.springframework.beans.factory.annotation.Value;4 importorg.springframework.stereotype.Component;5

6 @Component7 public classStudent {8         @Value("zs")9         privateString name;10 11         @Value("19")12         private intage;13 14 @Override15         publicString toString() {16                 return "Student [name=" + name + ", age=" + age + "]";17 }18 }

这种方式可以实现spring内置支持类型的注入,但是这种方式将注入的值写死在了代码中,后续如果希望改变注入的值,必须来修改源代码,此时可以将这些值配置到一个properties配置文件中,再从spring中进行引入。

df1b4c775ff38196a9a899064d84be47.png

b11566e62e720a6fa94ede477ec6325e.png

97604ac16122d88a701f7c62110bf683.png

(3)注解方式注入spring内置支持的数据类型 - 集合类型

将spring-util-3.2xsd交给MyEclipse管理

在当前spring容器的配置文件中导入util名称空间

在通过适当的util标签注册数据

案例:

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:util="http://www.springframework.org/schema/util"

5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

6 xsi:schemaLocation="http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd8 http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-3.2.xsd10 http://www.springframework.org/schema/util11 http://www.springframework.org/schema/util/spring-util-3.2.xsd12 ">

13         

14         

15         

16         

17         

18         

19 20         

21         

22                 aaa

23                 bbb

24                 ccc

25         

26         

27                 111

28                 222

29                 333

30         

31         

32                 

33                 

34                 

35         

36         

37                 v1

38                 v2

39                 v3

40         

41

再在类的属性中通过@Value注入赋值

1 packagecn.tedu.domain;2

3 importjava.util.List;4 importjava.util.Map;5 importjava.util.Properties;6 importjava.util.Set;7

8 importorg.springframework.beans.factory.annotation.Value;9 importorg.springframework.stereotype.Component;10

11 @Component12 public classStudent {13         @Value("${name}")14         privateString name;15 16         @Value("${age}")17         private intage;18 19         @Value("#{@l1}")20         private Listlist;21 22         @Value("#{@s1}")23         private Setset;24 25         @Value("#{@m1}")26         private Mapmap;27 28         @Value("#{@p1}")29         privateProperties prop;30 31 @Override32         publicString toString() {33                 return "Student [name=" + name + ", age=" + age + ", list=" +list34                                 + ", set=" + set + ", map=" + map + ", prop=" + prop + "]";35 }36 37 }

(4)使用注解注入自定义bean类型数据

在bean中的属性上通过@Autowired实现自定义bean类型的属性注入代码:

1 packagecn.tedu.domain;2

3 importjava.util.List;4 importjava.util.Map;5 importjava.util.Properties;6 importjava.util.Set;7

8 importorg.springframework.beans.factory.annotation.Autowired;9 importorg.springframework.beans.factory.annotation.Value;10 importorg.springframework.stereotype.Component;11

12 @Component13 public classStudent {14 @Autowired15         privateDog dog;16 17 @Autowired18         privateCat cat;19 20 @Override21         publicString toString() {22                 return "Student [dog="+ dog + ", cat=" + cat + "]";23 }24 25 }

当spring容器解析到@Component注解时,会创建当前类的bean在spring容器中进行管理,在创建bean的过程中发现了@Autowired注解,会根据当前bean属性名称,寻找在spring中是否存在id等于该名称的bean,如果存在则自动注入,如果不存在,在检查是否存在和当前bean属性类型相同的bean,如果存在就注入,否在抛出异常。

也可以使用@Qualifier(value="dog1")注解,明确的指定要注入哪个id的bean

**也可以使用@Resource(name="id")指定注入给定id的bean,但是这种方式不建议使用。

5.其他注解

@Scope(value="prototype")

配置修饰的类的bean是单例或者多例,如果不配置默认为单例

案例:

1 packagecn.tedu.domain;2

3 importorg.springframework.context.annotation.Scope;4 importorg.springframework.stereotype.Component;5

6 @Component7 @Scope("prototype")8 public classTeacher {9 }

@Lazy

配置修饰的类的bean采用懒加载机制

案例:

1 packagecn.tedu.domain;2

3 importorg.springframework.context.annotation.Lazy;4 importorg.springframework.stereotype.Component;5

6 @Component7 @Lazy8 public classTeacher {9         publicTeacher() {10                 System.out.println("teacher construct..");11         }

@PostConstruct

在bean对应的类中,修饰某个方法,将该方法声明为初始化方法,对象创建之后立即执行。

@PreDestroy

在bean对应的类中,修饰某个方法,将该方法声明为销毁的方法,对象销毁之前调用的方法。

案例:

1 packagecn.tedu.beans;2 importjavax.annotation.PostConstruct;3 importjavax.annotation.PreDestroy;4 importorg.springframework.stereotype.Component;5

6 @Component7 public classDog {8 publicDog() {9 System.out.println("Dog...被创建出来了...");10 }11

12 @PostConstruct13 public voidinit(){14 System.out.println("Dog的初始化方法。。。");15 }16

17 @PreDestroy18 public voiddestory(){19 System.out.println("Dog的销毁方法。。。");20 }21 }

@Controller     @Service     @Repository     @Component

这四个注解的功能是完全一样的,都是用来修饰类,将类声明为Spring管理的bean的。

其中@Component一般认为是通用的注释

而@Controller用在软件分层的控制层,一般在web层

而@Service用在软件分层中的访问层,一般用在service层

而@Repository用在软件分层的数据访问层,一般在dao层

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值