Spring4.X学习(一):JavaConfig module相关注解
2018-01-29 15:03
369 查看
1、@Configuration
@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)
,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器
2、@Bean
@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的<bean>,作用为:注册bean对象
@Configuration启动容器+@Bean注册Bean
3、@DependsOn
@DependsOn用于强制初始化其他Bean。可以修饰Bean类或方法,使用该Annotation时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean
4、@Primary
@Primary的意思是在众多相同的bean中,优先使用用@Primary注解的bean
5、@Lazy
@Lazy用于指定该Bean是否取消预初始化。主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为,使用该Annotation时可以指定一个boolean型的value属性,该属性决定是否要预初始化该Bean。
6、@Import
@Import注解帮助我们将多个配置文件(可能是按功能分,或是按业务分)导入到单个主配置中,以避免将所有配置写在一个配置中。
7、@ImportResource
@ImportResource就是引入一个这种资源,资源对应一个xml文件
8、@Value
@value注解的方式来获取properties文件中的配置值
代码示例如下:
main类
@Configuration类
@Bean类
@Import类
执行结果
@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)
,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器
2、@Bean
@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的<bean>,作用为:注册bean对象
@Configuration启动容器+@Bean注册Bean
3、@DependsOn
@DependsOn用于强制初始化其他Bean。可以修饰Bean类或方法,使用该Annotation时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean
4、@Primary
@Primary的意思是在众多相同的bean中,优先使用用@Primary注解的bean
5、@Lazy
@Lazy用于指定该Bean是否取消预初始化。主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为,使用该Annotation时可以指定一个boolean型的value属性,该属性决定是否要预初始化该Bean。
6、@Import
@Import注解帮助我们将多个配置文件(可能是按功能分,或是按业务分)导入到单个主配置中,以避免将所有配置写在一个配置中。
7、@ImportResource
@ImportResource就是引入一个这种资源,资源对应一个xml文件
8、@Value
@value注解的方式来获取properties文件中的配置值
代码示例如下:
main类
import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyMain { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); MyBean tb = (MyBean) context.getBean("myBean"); } }
@Configuration类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Scope; @Configuration @Import(MyImportConfig.class) public class MyConfig { public MyConfig() { System.out.println("MyConfig容器启动初始化。。。"); } @Bean @Scope("prototype") public MyBean myBean() { return new MyBean(); } }
@Bean类
public class MyBean { public MyBean() { System.out.println("Bean初始化。。。"); } }
@Import类
@Configuration public class MyImportConfig { public MyImportConfig() { System.out.println("MyImportConfig容器启动初始化。。。"); } @Bean @Scope("prototype") public MyBean myBean() { return new MyBean(); } }
执行结果
一月 29, 2018 3:03:13 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Mon Jan 29 15:03:13 GMT+08:00 2018]; root of context hierarchy 一月 29, 2018 3:03:13 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition 信息: Overriding bean definition for bean 'myBean' with a different definition: replacing [Root bean: class [null]; scope=prototype; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.my.myProject.MyImportConfig; factoryMethodName=myBean; initMethodName=null; destroyMethodName=(inferred); defined in com.my.myProject.MyImportConfig] with [Root bean: class [null]; scope=prototype; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=myConfig; factoryMethodName=myBean; initMethodName=null; destroyMethodName=(inferred); defined in com.my.myProject.MyConfig] MyConfig容器启动初始化。。。 MyImportConfig容器启动初始化。。。 Bean初始化。。。
相关文章推荐
- java学习基础篇之WEB开发中常用Jsp/Bean相关技术总结(转)
- JAVA学习提高之----Tomcat版本信息查看及Tomcat相关配置
- Java编程思想学习笔记——枚举、注解
- java学习笔记1——JDK相关
- 学习java前的准备工作及相关概念
- Java学习从入门到精通---学习java相关技术的方法及步骤
- Java相关-轻松学习XML(转贴)
- 学习java相关知识的网站
- Java容器学习笔记(二) Set接口及其实现类的相关知识总结
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- java 之 注解Annotation学习示例
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- java学习应用相关的网站收集
- Java组件外观代码的相关学习
- Java基础-学习使用Annotation注解对象
- 学习Java的相关知识
- jsf学习总结(web.xml,content.xml,faces-config.xml等相关配置)
- java学习笔记(annotation 注解_1)
- Java工作相关学习
- Java基础-学习使用Annotation注解对象