一、创建对象
SpringIOC容器,是spring核心内容。
作用: 创建对象 & 处理对象的依赖关系
IOC容器创建对象, 有几种方式:
1) 调用无参数构造器
2) 带参数构造器
3) 工厂创建对象
工厂类,静态方法创建对象
工厂类,非静态方法创建对象
1.1 调用无参构造器
< bean id = " user1" class = " org.jsoft.b_create.User" > </ bean>
>>>>>> applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: p= " http://www.springframework.org/schema/p"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = " user1" class = " org.jsoft.b_create.User" > </ bean>
< bean id = " user2_a" class = " org.jsoft.b_create.User" >
< constructor-arg value = " 王胡塞" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
< bean id = " user2_b" class = " org.jsoft.b_create.User" >
< constructor-arg value = " 12" index = " 1" type = " int" > </ constructor-arg>
< constructor-arg value = " 王胡塞" index = " 0" type = " java.lang.String" > </ constructor-arg>
</ bean>
< bean id = " str" class = " java.lang.String" >
< constructor-arg value = " 王虎" > </ constructor-arg>
</ bean>
< bean id = " user2_c" class = " org.jsoft.b_create.User" >
< constructor-arg ref = " str" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
< bean id = " fac" class = " org.jsoft.b_create.ObjectFactory" > </ bean>
< bean id = " user3_a" factory-bean = " fac" factory-method = " getInstance" > </ bean>
< bean id = " user3_b" class = " org.jsoft.b_create.ObjectFactory" factory-method = " getStaticInstance" > </ bean>
</ beans>
>>>>>> User.java
public class User {
private String name;
private int age;
public User ( ) {
super ( ) ;
System. out. println ( "--user对象创建--" ) ;
}
public User ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public void user_init ( ) {
System. out. println ( "创建对象之后,初始化" ) ;
}
public void user_destoty ( ) {
System. out. println ( "IOC容器销毁,调用" ) ;
}
}
>>>>>> App.java
public class App {
public static void main ( String[ ] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "org/jsoft/b_create/applicationContext.xml" ) ;
User u= ( User) ac. getBean ( "user1" ) ;
System. out. println ( u) ;
}
}
1.2 调用有参构造器
调用有参构造器(参数不能打乱):
< bean id = " user2_a" class = " org.jsoft.b_create.User" >
< constructor-arg value = " 王胡塞" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
调用有参构造器(参数可以打乱):
< bean id = " user2_b" class = " org.jsoft.b_create.User" >
< constructor-arg value = " 12" index = " 1" type = " int" > </ constructor-arg>
< constructor-arg value = " 王胡塞" index = " 0" type = " java.lang.String" > </ constructor-arg>
</ bean>
调用有参构造器(参数引用其他对象):
< bean id = " str" class = " java.lang.String" >
< constructor-arg value = " 王虎" > </ constructor-arg>
</ bean>
< bean id = " user2_c" class = " org.jsoft.b_create.User" >
< constructor-arg ref = " str" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
>>>>>> applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: p= " http://www.springframework.org/schema/p"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = " user2_a" class = " org.jsoft.b_create.User" >
< constructor-arg value = " 王胡塞" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
< bean id = " user2_b" class = " org.jsoft.b_create.User" >
< constructor-arg value = " 12" index = " 1" type = " int" > </ constructor-arg>
< constructor-arg value = " 王胡塞" index = " 0" type = " java.lang.String" > </ constructor-arg>
</ bean>
< bean id = " str" class = " java.lang.String" >
< constructor-arg value = " 王虎" > </ constructor-arg>
</ bean>
< bean id = " user2_c" class = " org.jsoft.b_create.User" >
< constructor-arg ref = " str" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
</ beans>
>>>>>> User.java
public class User {
private String name;
private int age;
public User ( ) {
super ( ) ;
System. out. println ( "--user对象创建--" ) ;
}
public User ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public void user_init ( ) {
System. out. println ( "创建对象之后,初始化" ) ;
}
public void user_destoty ( ) {
System. out. println ( "IOC容器销毁,调用" ) ;
}
}
>>>>>> App.java
public class App {
public static void main ( String[ ] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "org/jsoft/b_create/applicationContext.xml" ) ;
User u= ( User) ac. getBean ( "user2_b" ) ;
System. out. println ( u) ;
}
}
1.3 调用工厂类创建对象
+++ 调用工厂类实例方法,创建对象
< bean id = " fac" class = " org.jsoft.b_create.ObjectFactory" > </ bean>
< bean id = " user3_a" factory-bean = " fac" factory-method = " getInstance" > </ bean>
+++ 调用工厂类静态方法,创建对象
< bean id = " user3_b" class = " org.jsoft.b_create.ObjectFactory" factory-method = " getStaticInstance" > </ bean>
1.3.1 调用工厂类的实例方法,创建对象
>>>>>> applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: p= " http://www.springframework.org/schema/p"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = " fac" class = " org.jsoft.b_create.ObjectFactory" > </ bean>
< bean id = " user3_a" factory-bean = " fac" factory-method = " getInstance" > </ bean>
</ beans>
>>>>>> ObjectFactory .java
public class ObjectFactory {
public User getInstance ( ) {
System. out. println ( "工厂类,实例方法" ) ;
return new User ( ) ;
}
public static User getStaticInstance ( ) {
System. out. println ( "工厂类,静态方法" ) ;
return new User ( ) ;
}
}
>>>>>> User.java
public class User {
private String name;
private int age;
public User ( ) {
super ( ) ;
System. out. println ( "--user对象创建--" ) ;
}
public User ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public void user_init ( ) {
System. out. println ( "创建对象之后,初始化" ) ;
}
public void user_destoty ( ) {
System. out. println ( "IOC容器销毁,调用" ) ;
}
}
>>>>>> App.java
public class App {
public static void main ( String[ ] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "org/jsoft/b_create/applicationContext.xml" ) ;
User u= ( User) ac. getBean ( "user3_a" ) ;
System. out. println ( u) ;
}
}
1.3.2 调用工厂类的静态方法,创建对象
>>>>>> applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: p= " http://www.springframework.org/schema/p"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = " user3_b" class = " org.jsoft.b_create.ObjectFactory" factory-method = " getStaticInstance" > </ bean>
</ beans>
>>>>>> ObjectFactory .java
public class ObjectFactory {
public User getInstance ( ) {
System. out. println ( "工厂类,实例方法" ) ;
return new User ( ) ;
}
public static User getStaticInstance ( ) {
System. out. println ( "工厂类,静态方法" ) ;
return new User ( ) ;
}
}
>>>>>> User.java
public class User {
private String name;
private int age;
public User ( ) {
super ( ) ;
System. out. println ( "--user对象创建--" ) ;
}
public User ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public void user_init ( ) {
System. out. println ( "创建对象之后,初始化" ) ;
}
public void user_destoty ( ) {
System. out. println ( "IOC容器销毁,调用" ) ;
}
}
>>>>>> App.java
public class App {
public static void main ( String[ ] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "org/jsoft/b_create/applicationContext.xml" ) ;
User u= ( User) ac. getBean ( "user3_b" ) ;
System. out. println ( u) ;
}
}
1.3.3 利用工厂类的方法创建Bean,给方法传参
利用构造方法创建Bean:
< bean id = " user2_c" class = " org.jsoft.b_create.User" >
< constructor-arg ref = " str" > </ constructor-arg>
< constructor-arg value = " 12" > </ constructor-arg>
</ bean>
利用工厂类创建对象时,如果工厂类的方法带参数,则传参方式和构造方法传参相同。
< bean id = " userDao_proxy" class = " org.jsoft.d_proxy_e_myaop_2.ObjectFactory" factory-method = " getInstance" >
< constructor-arg index = " 0" ref = " userDao" > </ constructor-arg>
< constructor-arg index = " 1" ref = " myAop" > </ constructor-arg>
</ bean>
< bean id = " user3_b" class = " org.jsoft.b_create.ObjectFactory" factory-method = " getStaticInstance" > </ bean>
>>>>>> applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: p= " http://www.springframework.org/schema/p"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
< bean id = " user" class = " org.jsoft.c_property_e_param.ObjectFactory" factory-method = " createUser" >
< constructor-arg index = " 0" value = " 12" > </ constructor-arg>
< constructor-arg index = " 1" value = " 小白" > </ constructor-arg>
</ bean>
</ beans>
>>>>>> ObjectFactory .java
public class ObjectFactory {
public static User createUser ( int age, String name) {
return new User ( age, name) ;
}
}
>>>>>> User.java
public class User {
private int age;
private String name;
public User ( ) {
super ( ) ;
}
public User ( int age, String name) {
super ( ) ;
this . age = age;
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
}
>>>>>> App.java
public class App {
public static void main ( String[ ] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "org/jsoft/c_property_e_param/applicationContext.xml" ) ;
User u= ( User) ac. getBean ( "user" ) ;
System. out. println ( u. getName ( ) ) ;
}
}
1.4 总结
>>>>>> applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: p= " http://www.springframework.org/schema/p"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
< bean id = " user1" class = " org.jsoft.c_property_e_param.User" > </ bean>
< bean id = " user2" class = " org.jsoft.c_property_e_param.User" >
< constructor-arg index = " 0" value = " 12" > </ constructor-arg>
< constructor-arg index = " 1" value = " 小黑" > </ constructor-arg>
</ bean>
< bean id = " beanFac" class = " org.jsoft.c_property_e_param.ObjectFactory" > </ bean>
< bean id = " user3" factory-bean = " beanFac" factory-method = " getInstance" > </ bean>
< bean id = " user4" class = " org.jsoft.c_property_e_param.ObjectFactory" factory-method = " getInstanceByStatic" > </ bean>
< bean id = " user5" class = " org.jsoft.c_property_e_param.ObjectFactory" factory-method = " getInstanceByStatic" >
< constructor-arg index = " 0" value = " 12" > </ constructor-arg>
< constructor-arg index = " 1" value = " 小白" > </ constructor-arg>
</ bean>
</ beans>
>>>>>> ObjectFactory .java
public class ObjectFactory {
public User getInstance ( ) {
return new User ( ) ;
}
public static User getInstanceByStatic ( ) {
return new User ( ) ;
}
public static User getInstanceByStatic ( int age, String name) {
return new User ( age, name) ;
}
}
>>>>>> User.java
public class User {
private int age;
private String name;
public User ( ) {
super ( ) ;
}
public User ( int age, String name) {
super ( ) ;
this . age = age;
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
}
>>>>>> App.java
public class App {
public static void main ( String[ ] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "org/jsoft/c_property_e_param/applicationContext.xml" ) ;
User u= ( User) ac. getBean ( "user3" ) ;
System. out. println ( u. getName ( ) ) ;
}
}