spring的bean管理(注解方式)

简介:

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


相关文章
|
19天前
|
Java API 数据安全/隐私保护
掌握Spring Boot中的@Validated注解
【4月更文挑战第23天】在 Spring Boot 开发中,@Validated 注解是用于开启和利用 Spring 的验证框架的一种方式,特别是在处理控制层的输入验证时。本篇技术博客将详细介绍 @Validated 注解的概念和使用方法,并通过实际的应用示例来展示如何在项目中实现有效的数据验证
26 3
|
20天前
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
76 1
|
17天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
6天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
2天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解
|
5天前
|
JavaScript Java 开发者
Spring Boot中的@Lazy注解:概念及实战应用
【4月更文挑战第7天】在Spring Framework中,@Lazy注解是一个非常有用的特性,它允许开发者控制Spring容器的bean初始化时机。本文将详细介绍@Lazy注解的概念,并通过一个实际的例子展示如何在Spring Boot应用中使用它。
17 2
|
6天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
16 2
|
12天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
20 1
|
12天前
|
Java Spring
springboot自带的@Scheduled注解开启定时任务
springboot自带的@Scheduled注解开启定时任务
|
15天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
32 2