1、通过xml配置
2、通过java类配置,用@ComponentScan注解
@Configuration
@ComponentScan(value="com.cap2")
public class Cap2Config {
}
3、用@bean
@Bean
public Person inint(){
return new Person("1","2");
}
4、用@import
@Configuration
//@ComponentScan(value="com.cap2",excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=true)
@Import(value={Person.class})
public class Cap2Config {
}
5、用FactoryBean
@Component
public class FactoryBeanDemo implements FactoryBean<Person> {
public Person getObject() throws Exception {
// TODO Auto-generated method stub
return new Person();
}
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Person.class;
}