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在后续版本会对其增强
2.属性注入的注解:(使用注解注入的方式,可以不用提供set方法.)
@Value :用于注入普通类型.
@Autowired :自动装配:
* 默认按类型进行装配.
* 按名称注入:
* @Qualifier:强制使用名称注入.
@Resource相当于:
* @Autowired和@Qualifier一起使用.
3.Bean的作用范围的注解:
@Scope:
* singleton:单例
* prototype:多例
4.Bean的生命周期的配置:
@PostConstruct :相当于init-method
@PreDestroy :相当于destroy-method
@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
@PreConstruct说明
被@PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。
05.Spring的Bean管理的方式的比较:
XML和注解:
* XML :结构清晰.
* 注解 :开发方便.(属性注入.)
实际开发中还有一种XML和注解整合开发:
* Bean有XML配置.但是使用的属性使用注解注入
关于注解1:工作中有一次犯了一个很严重的问题:
关于注解2:想要两个类使用同一个变量,而且两个类有关系,通过注入方式建立两个类的对象产生关系。但是如果想要共用一个对象,建立对象可以通过有参构造传入(new A("xxx")),但是注解建立对象我不会传参。但可以通过配置文件和注解相结合使用。
第二张图演示了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对象
本文转自 叫我北北 51CTO博客,原文链接:http://blog.51cto.com/qinbin/2052625