博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring的bean管理(注解方式)
阅读量:7106 次
发布时间:2019-06-28

本文共 3324 字,大约阅读时间需要 11 分钟。

1.Spring的Bean管理的中常用的注解

@Component:组件.(作用在类上)
1
2
3
4
5
6
7
8
9
10
11
public 
interface 
UserDao {
    
public 
void 
sayHello();
}
  
@Component
(value=
"userDao"
)
    
public 
class 
UserDaoImpl 
implements 
UserDao {
  
@Override
public 
void 
sayHello() {
    
System.out.println(
"Hello Spring Annotation..."
);
}

Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)

* @Controller :WEB

* @Service :业务层

* @Repository :持久层

 

这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强

图片1.png 

2.属性注入的注解:(使用注解注入的方式,可以不用提供set方法.)

@Value  :用于注入普通类型.

@Autowired :自动装配:

默认按类型进行装配.

按名称注入:

* @Qualifier:强制使用名称注入.

@Resource相当于:

* @Autowired和@Qualifier一起使用.

无标题.png

3.Bean的作用范围的注解:

@Scope:

* singleton:单例

* prototype:多例

4.Bean的生命周期的配置:

@PostConstruct :相当于init-method

@PreDestroy  :相当于destroy-method

    @PostConstruct说明

         @PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Sercletinti()方法。@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

    @PreConstruct说明

         @PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servletdestroy()方法。@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

 

05.SpringBean管理的方式的比较:

图片2.png 

 

XML和注解:

* XML :结构清晰.

注解 :开发方便.(属性注入.)

 

实际开发中还有一种XML和注解整合开发:

* BeanXML配置.但是使用的属性使用注解注入

关于注解1:工作中有一次犯了一个很严重的问题:

spring注解.png

关于注解2:想要两个类使用同一个变量,而且两个类有关系,通过注入方式建立两个类的对象产生关系。但是如果想要共用一个对象,建立对象可以通过有参构造传入(new A("xxx")),但是注解建立对象我不会传参。但可以通过配置文件和注解相结合使用。

spr.png

第二张图演示了properties文件属性的使用方法,在工作中,又遇见另外一种(在这里演示)

首先,在spring的主配文件中要配置:

1
2
3
4
5
6
7
8
9
10
<!-- 加载applicationConfig.properties文件,获取属性文件的内容 -->
    
<bean id=
"propertyConfigurer" 
class
=
"com.ad.utils.GlobalProperties"
>
        
<property name=
"ignoreResourceNotFound" 
value=
"true" 
/>
        
<property name=
"locations"
>
            
<list>
                
<value>classpath:applicationConfig.properties</value>
                
......
            
</list>
        
</property>
    
</bean>

然后写出实现类:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import 
java.util.HashMap;
import 
java.util.Map;
import 
java.util.Properties;
 
import 
org.springframework.beans.BeansException;
import 
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import 
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 
/**
 
* 自定义PropertyPlaceholderConfigurer返回properties内容
 
 
*/
public 
class 
GlobalProperties 
extends 
PropertyPlaceholderConfigurer{
 
    
private 
static 
Map<String, Object> ctxPropertiesMap;
 
    
@Override
    
protected 
void 
processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) 
throws 
BeansException {
         
        
super
.processProperties(beanFactoryToProcess, props);
        
ctxPropertiesMap = 
new 
HashMap<String, Object>();
        
for 
(Object key : props.keySet()) {
            
String keyStr = key.toString();
            
String value = props.getProperty(keyStr);
            
ctxPropertiesMap.put(keyStr, value);
        
}
    
}
 
    
public 
static 
Object getContextProperty(String name) {
        
return 
ctxPropertiesMap.get(name);
    
}
     
    
/**
     
* 获取属性值
     
* @param key
     
* @return
     
*/
    
public 
static 
String getProperties(String key){ 
        
Object value = ctxPropertiesMap.get(key);
        
return 
value != 
null 
? String.valueOf(value) : 
""
;
    
}
     
      
    
/**
     
*获取属性值,返回整形
     
* @param key
     
* @return
     
*/
    
public 
static 
Integer getInteger(String key){
        
Object value = ctxPropertiesMap.get(key);
        
return 
value != 
null 
? Integer.valueOf(value.toString()) : 
0
;
    
}
 
}

最后是使用方法:

1
GlobalProperties.getProperties(
"XXXXX"
).trim();

补:spring的bean管理xml方式

01.注入对象类型属性

    创建service类和dao

    service中得到dao对象

image.png

本文转自 叫我北北 51CTO博客,原文链接:http://blog.51cto.com/qinbin/2052625

转载地址:http://edjhl.baihongyu.com/

你可能感兴趣的文章
My Autoplay v12.0 多国语言最新版
查看>>
通过JS获取CSS属性的值
查看>>
如何用js得到当前页面的url信息方法(JS获取当前网址信息)
查看>>
Sql Server系列:子查询
查看>>
遍历特定类型子对象
查看>>
二逼平衡树 Tyvj 1730 BZOJ3196 Loj#106
查看>>
ORA-12541: TNS: 无监听程序 怎么解决
查看>>
自定义UIButton--iPhone按钮控件点击效果写法
查看>>
HDOJ_ACM_CUP
查看>>
陶哲轩实分析习题8.5.11
查看>>
软件工程随堂小作业——(C++)
查看>>
搭建个人专用的谷歌搜索镜像站---学习笔记
查看>>
三步轻松打造微信聊天机器人(附源码)
查看>>
11月24日学习内容整理:django基础,安装,创建项目,设置配置文件中的信息,简单的登录验证举例...
查看>>
expect和assert的联合用法,git删除本地和远端分支
查看>>
利用Handler延时机制实现点击两次退出程序
查看>>
suse11 oracle11g 安装 3
查看>>
模拟+位运算 HDOJ 5491 The Next
查看>>
ZOJ 3157 Weapon
查看>>
Luogu_3239 [HNOI2015]亚瑟王
查看>>