Snip of spring.xml -
Snip of App code -
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Course course = (Course) context.getBean("course"); course.display();
Output -
I am a tutor.
Name is: James, and ID is: 1
I am a student.
Name is: Tom, and ID is: 1
However, for security or whatever reason you don't want to expose the attribute elsewhere. Inner bean here is what you are after.
Inner Bean -
You can't point to the inner bean since no id attribute specified for it.
In addition to id, we can also use alias and name to get an object.
All work fine.
Course course = (Course) context.getBean("course"); Course courseAlias = (Course) context.getBean("course-alias"); Course c1 = (Course) context.getBean("c1"); Course c2 = (Course) context.getBean("c2");
The element idref is confusing, many people think it is almost the same with ref, but only can use id of another bean as reference. It is completely wrong. Find more explanation at beans-idref-element. Generally speaking, you can treat <idref bean="beanID" /> as value="beanID".
No comments:
Post a Comment