Spring4.X学习(一):JavaConfig module相关注解

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类

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初始化。。。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值