Spring Core配置笔记

来源:互联网 发布:mac卸载百度输入法 编辑:程序博客网 时间:2024/06/07 09:09

Configruation类配置

@Configuration 表明一个类是配置类,应该包含Spring应用上下文中如何创建bean的细节。

@ComponentScan(basePackages = “com.habuma.soundsystem”,
excludeFilters = { @Filter(Configuration.class) })

@Import({xxx.class, yyy.class}) 引入其他配置类
@ImportResource(“classpath:xxyy.xml”) 引入其他xml配置文件

@Profile(“dev”) 可以注解在类上或者方法上,用来表明某一配置类下所有Bean,或者某一个Bean要在具体的Profile下才能被激活。
激活具体的profile需要设置spring.profiles.default或者spring.profiles.active

@PropertySource(“classpath:/com/xxx.properties”) 引入外部属性源

@Bean(name=”xxx”) 在上下文中装配一个Bean

Component类的注解

@Component(“name”)

两种自动装配
@Autowired 只有一种实现是使用
@Qualifier(“”) 通过名字来注入具体的实现

String型资源装配
@Value(“${datasource.url}”)
String url;

JUnit测试配置

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=KnightConfig.class,loader=AnnotationConfigContextLoader.class)

启动一个Java配置的Spring

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);HelloWorld obj = (HelloWorld) context.getBean("helloBean");
原创粉丝点击