Spring-Bean之基于注解(Annotation)装配解析
来源:互联网 发布:小狐狸加速器mac 编辑:程序博客网 时间:2024/09/21 13:52
1、概述
在Spring中,尽管使用XML配置文件可以实现Bean的装配工作,但如果XML配置的bean非常的多,XML配置文件代码将变得非常臃肿,不利于代码的后期维护。
自从JDK 1.5之后,Java提供了注解(Annotation)功能,与此同时,Spring 3也提供了Annotation的全面支持,如注解装配Bean、注解实现AspectJ AOP编程等。
在Spring 3中,常用的注解如下:
①定义Bean注解
@Compenent:定义Spring中的Bean,可以表示任何层的Bean(Controller、Service、Repository),这是一个泛化的注解。
@Repository:功能与Compenent完全一样,用于注解Repository层的Bean。
@Service:功能与Compenent完全一样,用于注解Service层的Bean。
@Controller:功能与Compenent完全一样,用于注解Controller层的Bean。(如SpringMVC)
②装配Bean注解
@Autowired:用于装配Bean的属性。可标注在成员变量、成员变量的setter方法、构造函数上。@Autowired默认按照Bean的类型进行装配。若有多个相同类型的Bean实例,将抛出BeanCreationException。
@Resource:与@Autowired功能相同,不同的是@Resource默认按照Bean的名称进行装配。@Resource中有两个重要的属性:name、type。name用于指定装配Bean的实例名称,type用于指定装配Bean的类型。若name、type属性都没有被指定,@Resource先按实例名称进行装配(默认实例名称除了类名首字母小写,其他部分完全一样,如Book类的默认实例名称为book)。若找不到默认的实例名称的Bean实例,就按类型进行装配;若有多个相同类型的Bean的实例,将抛出BeanCreationException异常。
@Qualifier:与@Autowired注解配合使用,会默认的按Bean类型装配修改为按Bean的实例名称装配。Bean的实例名称由@Qualifier注解参数决定。
笔者个人经验:尽量使用@Autowired。
2、实例分析
①创建项目
笔者的项目大概是这样的:
创建一个source folder:config
然后导包:
②分析代码
创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com.hanlin.spring"></context:component-scan></beans>
这段代码:<context:component-scan base-package="com.hanlin.spring"></context:component-scan>
表示扫描com.hanlin.spring包下的注解。
定义Book类:
import org.springframework.stereotype.Component;/** * @author Hanlin Wang */@Componentpublic class Book { public void show(){ System.out.println("原--始--书"); }}
注:这里的@Compenent与@Compenent(“book”)与@Compenent(value=”book”)完全一样。
定义Book类的子类:English、Math
English:
import org.springframework.stereotype.Component;/** * @author Hanlin Wang */@Componentpublic class English extends Book{ @Override public void show() { // TODO Auto-generated method stub System.out.println("英语书"); }}
Math:
import org.springframework.stereotype.Component;/** * @author Hanlin Wang */@Componentpublic class Math extends Book{ @Override public void show() { // TODO Auto-generated method stub System.out.println("数学书"); }}
创建Box类,成员变量:Book类型的book
import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;/** * @author Hanlin Wang */@Componentpublic class Box { @Autowired private Book book; public void showBox(){ this.book.show(); }}
创建主程序类:SpringBeans
public class SpringBeans { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Box box = (Box) context.getBean("box"); box.showBox(); }}
运行结果:
二月 26, 2017 3:18:51 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26d7ad36: startup date [Sun Feb 26 15:18:51 CST 2017]; root of context hierarchy二月 26, 2017 3:18:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]二月 26, 2017 3:18:51 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@371ee2ea: defining beans [book,box,english,math,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy原--始--书
- Spring-Bean之基于注解(Annotation)装配解析
- Spring(3、基于注解装配Bean)
- spring-装配Bean 基于注解
- spring装配bean(基于注解)
- Spring 装配Bean 基于注解
- Spring-装配Bean 基于注解
- Spring In Action(一):基于注解装配bean
- Spring(七)基于注解装配bean
- 装配Bean 基于注解
- bean装配基于注解
- 装配Bean基于注解
- Spring学习之Bean的自动装配(非注解)
- Spring Bean装配之基于Java的容器注解说明——@Bean
- Spring Bean装配之基于Java的容器注解说明——@Bean和@Scope
- Spring Bean装配之基于Java的容器注解说明——基于泛型的自动装配
- Spring-IOC装配Bean(注解方式)
- Spring入门(Bean装配,Autowired注解)
- spring基础-Bean的装配方式(二)基于Annotation的配置
- switch语句中default用法详解
- JNI
- 无法获得锁 /var/lib/dpkg/lock
- PHP 函数(1)
- 【Javaweb】笔面试题 ---(1)
- Spring-Bean之基于注解(Annotation)装配解析
- 单例模式
- leetcode136 Single Number136 Java
- Android从服务器取数据
- Android RadioButton自定义背景色圆角样式的方法
- hdu 1133 Buy the Ticket(卡特兰数+大整数)
- Java中main函数里面的的args
- 常见git命令
- ural1007 Code Words (字符串处理)