本文共 598 字,大约阅读时间需要 1 分钟。
基于xml的形式定义Bean的信息
去容器中读取Bean
public static void main( String[] args ){ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");System.out.println(ctx.getBean("person"));}
基于读取配置类的形式定义Bean信息
@Configurationpublic class MainConfig { @Bean public Person person(){ return new Person(); }}
注意: 通过@Bean的形式是使用的话, bean的默认名称是方法名,若@Bean(value="bean的名称")那么bean的名称是指定的
去容器中读取Bean的信息(传入配置类)
public static void main( String[] args ){ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class); System.out.println(ctx.getBean("person"));}
转载地址:http://mzfzb.baihongyu.com/