二、Spring内置注解
1、@Component注解
@Component("Bean的名称") 通过注解标注一个类为受管Bean。默认情况下通过@Component定义的Bean都是singleton的,如果需要使用其它作用范围的Bean,可以通过@Scope注释来达到目标。
注解类层次关系:
@Component
@Configuration
@Controller
@RestController
@Service
@Repository
2、@Service注解
用于标注业务层组件
3、@Repository注解
用于标注数据访问组件
4、@Autowired注解
默认按类型装配bean,且容器中匹配的候选Bean数目必须有且仅有一个,可以设置required=false让其成为可选的。想使用名称装配可以结合@Qualifier注解进行使用。
@Qualifier注解
@Qualifier("Bean的名称") 指定注入Bean的名称。只能用于类的成员变量、方法的参数和构造子的参数。如果它与@Autowired联合使用,则自动装配的策略就变为byName了。
注解使用范例:
public class Person { private Long id; private String name; private Address address; public Person(){ } public Person(Long id, String name){ this.id = id; this.name = name; } @Autowired(required=false) public void setAddress(@Qualifier("address2")Address address) { this.address = address; } }
<!-- 通过注解定义bean。默认同时也通过注解自动注入 --> <context:component-scan base-package="com.cjm"/> <bean id="address1" class="com.cjm.model.Address" p:city="gz1" p:zipCode="111"/> <bean id="address2" class="com.cjm.model.Address" p:city="gz2" p:zipCode="222"/> <bean id="person" class="com.cjm.model.Person"> <constructor-arg index="0" value="111"/> <constructor-arg index="1" value="cjm"/> </bean>
自定义限定符注解:
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface MyQulifier { String value(); }
public class Person { private Long id; private String name; private Address address; public Person(){ } public Person(Long id, String name){ this.id = id; this.name = name; } @Autowired public void setAddress(@MyQulifier("a2")Address address) { this.address = address; } }
<bean id="address1" class="com.cjm.model.Address" p:city="gz1" p:zipCode="111"> <qualifier type="com.cjm.annotation.MyQulifier" value="a1"/> </bean> <bean id="address2" class="com.cjm.model.Address" p:city="gz2" p:zipCode="222"> <qualifier type="com.cjm.annotation.MyQulifier" value="a2"/> </bean>
5、@ComponentScan
该注解默认会自动扫描该类所在包下所有的配置类,相当于之前的 <context:component-scan>。
useDefaultFilters属性的默认值为true,也就是说spring默认会自动发现被 @Component、@Controller、@Service和@Repository 标注的类,并注册进容器中。
可以使用value属性来指定要扫描的包。
@ComponentScan(value="com.seasy.controller")
使用 excludeFilters 来按照规则排除某些包的扫描。
@ComponentScan(value="com.seasy", excludeFilters={@Filter(type=FilterType.ANNOTATION, value={Controller.class})}, useDefaultFilters=false)
使用 includeFilters 来按照规则只包含某些包的扫描。
@ComponentScan(value="com.seasy", includeFilters={@Filter(type=FilterType.ANNOTATION, classes={Controller.class})}, useDefaultFilters=false)
添加多种扫描规则
可以使用 @ComponentScans 来添加多个 @ComponentScan,从而实现添加多个扫描规则。同样,也需要加上 @Configuration 注解,否则无效。
@ComponentScans(value={@ComponentScan(value="com.seasy.controller"), @ComponentScan(value="com.seasy.service")})
@Configuration
public class BeanConfig { }
6、@Scope注解
@Scope("Bean的作用范围") 通过@Scope注解为受管bean指定作用域。Bean的作用范围有:singleton、prototype、request、session、globalsession等。
7、@ImportResource
导入Spring的xml配置文件,让配置文件的内容生效,同时需要配合@Configuration注解一起使用:
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig{
}
8、@PropertySource
加载指定的属性文件,文件中的属性可以配合@value注解一起使用
@PropertySource("classpath:person.properties")
public class AppConfig{
@value("${key}")
private String username;
}
9、@Value
通过@Value将外部的值动态注入到Bean中
@Value的值有两类:
${ property : default_value }注入的是外部参数对应的property
#{ obj.property? : default_value }注入的是SpEL表达式对应的内容
注入普通字符串
@Value("normal")
private String normal;
//注入文件资源
@Value("classpath:com/seasy/config/config.txt")
private Resource resourceFile;
//注入URL资源
@Value("http://www.baidu.com")
private Resource testUrl;
//注入操作系统属性
@Value("#{systemProperties['os.name']}")
private String systemPropertiesName;
//注入表达式结果,T表示使用类的静态方法
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber;
//注入其他Bean属性
@Value("#{beanInject.another}")
private String fromAnotherBean;
//将外部配置文件的值动态注入到Bean中。
//在Springboot中默认可以使用application.properties属性文件的配置,自定义属性文件可以通过@PropertySource加载
@Value("${app.name}")
private String appName;
10、@Required
该注解适用于bean属性的setter方法,并表示bean属性必须要设值。否则,容器会抛出一个BeanInitializationException异常。
@Required
public void setAge(Integer age) {
this.age = age;
}
11、@Primary
自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
12、@DependsOn
用于强制初始化其他Bean,可以修饰Bean类或方法。
@DependsOn({"bean1", "bean2"})
13、@Lazy
用于指定是否延迟初始化Bean,主要用于修饰Bean类。
@Lazy(true)
14、@EnableAspectJAutoProxy
启用AOP。该注解类用于自动注册AnnotationAwareAspectJAutoProxyCreator对象到Spring容器中,该对象除了会自动将容器的所有Advisor作用于所有的Bean外,也会自动将标注有@Aspect注解的切面类织入到目标bean中。
三、JSR规范的注解(需要common-annotations.jar包的支持)
1、@Resource 由JSR-250提供
作用与@Autowired注解相同。默认按名称进行装配bean,名称可以通过name属性进行指定。
@Resource(name="person") name属性用于指定注入的Bean的名称。
@Resource(type=Person.class) type属性用于指定注入的Bean的类型。
2、@PostContsuct
用于指定受管Bean的初始化方法,作用与Bean的init-method属性类似。
3、@PreDestory
用于指定受管Bean的析构方法,作用与Bean的destory-method属性类似。
4、@Inject 由JSR-330提供
和spring中的 @Autowired 相同。
5、@Named
和spring中的 @Component 相同。
@Name可以有值,如果没有值生成的bean名称默认和类名相同。
相关推荐
Spring注解大全,注解整理方式采用思维导图工具(XMind)整理,对注解按自己的方式进行了分类,并对所有的注解在备注中进行了解释说明;
spring注解描述大全
spring注解整理,及应用
Spring 常用注解整理,分类:创建对象;注入数据;范围;全局异常;生命周期;新注解;JPA;扩展原理等注解类型。
spring4注解[整理].pdf
自己整理的一些常见的spring注解,文件是xmind的,平时用于梳理记忆
自整理spring大部分日常使用到的注解说明 自整理spring大部分日常使用到的注解说明 自整理spring大部分日常使用到的注解说明
其中 @ComponentScan 让 spring Boot 扫描到 Configuration 类并把它加入到程序上下文。 @Configuration U等同于 spring 的 XML 配置文件;使用 Java 代码可以检查类型安全。 @EnableAutoConfiguration 自动配置。...
Spring注解驱动开发本文整理于csdn博主 让优秀成为你的习惯的笔记来源组件注册@Configuration&@Bean给容器类注册组件创建一个Maven项
想要好好了解spring注解的一本必备资料,也是我同事推荐给我的,是整理过的资料。 大家都知道spring的注释配置相对于 XML 配置具有很多的优势,本书就详细阐述了 在这里跟大家分享了,希望大家多支持!!
主要是整理spring 注解注意事项、值的接受传递不同方式
本文是我在业余时间学习spring注解注入之后的整理总结,希望能给对spring注入技术感兴趣和正在学习spring注入的同学们一些帮助。文中的内容都是我自己的摸索总结,当中难免会有偏差和错误,希望spring达人能够及时...
本文是我在业余时间学习spring注解注入之后的整理总结,希望能给对spring注入技术感兴趣和正在学习spring注入的同学们一些帮助。文中的内容都是我自己的摸索总结,当中难免会有偏差和错误,希望spring达人能够及时...
介绍spring 5.0中所有注解的使用和功能
主要介绍了浅谈spring注解之@profile,@profile通过配置来改变参数,这里整理的详细的用法,有兴趣的可以了解一下
spring方面介绍 常用注解 生命周期 AOP DI
其中SSM为Myeclipse9.0工程(工程在页面层有bug,建议用Debug模式,跟踪查看其效果) ...(注:这次太匆忙没整理好,spring和struts的注解方式都没用上。下次会上传Spring3+struts2+mybatis3的全注解方式)
Wicket6.0_Spring3.1_Hibernate4.1_EJB全注解实例。采用JTA事务管理,在glassfish3.1.2和postgresql9测试通过。参考网上的资料整理。
内容概要:学习Spring的一些学习笔记,主要学习Spring ...适用人群:比较适合与我一样的在校普通大学生进行学习整理,以及适合初学spring的朋友进行巩固加深印象! 阅读建议:需要有一定的代码基础,一定的知识储备
spring 3.0 注解,和配置文件的配置