catalogue
2. Scope and life cycle of bean object
1. How to create bean s
(1) Use default constructor
• The bean tag is used in the Spring configuration file. After the id and class attributes are configured, there is no configuration For other properties and tags, the default constructor is used to create bean objects. At this time, if the class Object cannot be created without a default constructor in.(2) The set method is injected, and the transfer collection (dynamically creating the factory method and indirectly calling userserviceimpl4) is managed by bean.
main method
public static void main(String[] args) { // 1. Get the core container object ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //4.set method injection and transfer collection (dynamically create factory method and indirectly call userserviceimpl4) IUserService service5 = (IUserService) context.getBean("userService5"); service5.queryUser(); // 3. Close the container (if you don't remember to close the container, the most typical problem is that the database connection cannot be released) ((ClassPathXmlApplicationContext) context).close(); }
applicationContext.xml main configuration file
<bean id="myFactory"> </bean> <bean id="userService5" factory-bean="myFactory" factory-method="createIUserService"> <property name="strings"> <array> <value>verb Rush</value> <value>verb Roll, roll</value> <value>verbFactoty in fact</value> </array> </property> </bean>
(3) The set method is injected, and the transfer collection (the factory method is created statically and calls userserviceimpl4 indirectly) is managed through bean s.
main method
public static void main(String[] args) { // 1. Get the core container object ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //5.set method injection and transfer collection (create factory method statically and directly call userserviceimpl4) IUserService service6 = (IUserService) context.getBean("userService6"); service6.queryUser(); // 3. Close the container (if you don't remember to close the container, the most typical problem is that the database connection cannot be released) ((ClassPathXmlApplicationContext) context).close(); }
applicationContext.xml main configuration file
<bean id="userService6" factory-method="createIUserService"> <property name="strings"> <array> <value> static Rush</value> <value> static Operation room</value> <value> staticFactoty Fang Gang</value> </array> </property> </bean>
UserServiceImpl4 code
public class UserServiceImpl4 implements IUserService{ private String[] strings; private List<String> list; private Set<String> set; private Map< String, String> map; private Properties properties; public void setStrings(String[] strings) { this.strings = strings; } public void setList(List<String> list) { this.list = list; } public void setSet(Set<String> set) { this.set = set; } public void setMap(Map<String, String> map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public String[] getStrings() { return strings; } public List<String> getList() { return list; } public Set<String> getSet() { return set; } public Map<String, String> getMap() { return map; } public Properties getProperties() { return properties; } @Override public String toString() { return "UserServiceImpl4 [strings=" + Arrays.toString(strings) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + "]"; } @Override public String queryUser() { System.out.println(Arrays.toString(strings)); System.out.println(list); System.out.println(set); System.out.println(map); System.out.println(properties); return ""; } }