Bean injection based on xml form
@Data @AllArgsConstructor @NoArgsConstructor public class PersonBean { private Integer id; private String name; private String address; }
<bean class="com.luna.annotation.PersonBean" id="personBean"> <property name="name" value="张三"/> <property name="address" value="上海"/> <property name="id" value="1"/> </bean>
// Complete data bean injection based on xml @Test public void test(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); PersonBean personBean = (PersonBean) applicationContext.getBean("personBean"); System.out.println(personBean.getAddress()); }
Bean injection based on Annotation form
@Configuration public class PersonBeanConfig { @Bean(value = "person") public PersonBean personBean(){ return new PersonBean(1,"李四","北京"); } }
// Complete data injection based on pure annotation @Test public void test(){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonBeanConfig.class); PersonBean personBean = applicationContext.getBean(PersonBean.class); System.out.println(personBean.getAddress()); PersonBean person = (PersonBean)applicationContext.getBean("person"); System.out.println(person.getAddress()); }