您的位置:首页 > 编程语言 > Java开发

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类

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初始化。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息