spring创建bean的三种方式
1、使用构造器创建bean
1.1、使用无参构造器创建
package com.ly.spring; public class Person { private String name; public void say(String name) { System.out.println("你好,我叫"+name); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--若没有无参构造器会报错--> <bean id="person" class="com.ly.spring.Person"></bean> </beans>
1.2、使用有参构造器创建
package com.ly.spring; public class Person { private String name; public Person(String name) { this.name = name; } public void say() { System.out.println("我的名字叫"+this.name); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.ly.spring.Person"> <!--指定构造器对应参数名对应的值--> <constructor-arg name="name" value="有参构造"></constructor-arg> </bean> </beans>
2、使用静态工厂方法创建bean
Person:
package com.ly.spring; public class Person { private String name; public Person(String name) { this.name = name; } public void say() { System.out.println("我的名字叫"+this.name); } }
Chinese:
package com.ly.spring; public class Chinese implements Person { private String name; public void setName(String name) { this.name = name; } @Override public void say() { System.out.println("我的名字叫"+this.name); } }
American:
package com.ly.spring; public class American implements Person { private String name; public void setName(String name) { this.name = name; } @Override public void say() { System.out.println("我的名字叫"+this.name); } }
工厂类PersonFactory:
package com.ly.spring; public class PersonFactory { public static Person getPerson(String name) { if("Chinese".equals(name)) { return new Chinese(); }else if("American".equals(name)) { return new American(); } return null; } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- class指定工厂类类路径 factory-method指定工厂类的静态方法 --> <bean id="chinese" class="com.ly.spring.PersonFactory" factory-method="getPerson"> <!--给工厂的静态方法注入参数--> <constructor-arg name="name" value="Chinese"></constructor-arg> <!--通过set方法给Chinese类的name属性注入值--> <property name="name" value="中国人"></property> </bean> <bean id="american" class="com.ly.spring.PersonFactory" factory-method="getPerson"> <constructor-arg name="name" value="American"></constructor-arg> <property name="name" value="美国人"></property> </bean> </beans>
测试类:
package com.ly.spring.test; import com.ly.spring.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Person p1 = context.getBean("chinese", Person.class);//获取到的其实是Chinese类 p1.say();//执行时会调用Chinese类的方法 Person p2 = context.getBean("american",Person.class);//获取到的其实是American类 p2.say();//执行时会调用American类的方法 } }
3、使用实例工厂方法创建bean
修改后的工厂类PersonFactory:
package com.ly.spring; public class PersonFactory { //这里不再是静态方法 public Person getPerson(String name) { if("Chinese".equals(name)) { return new Chinese(); }else if("American".equals(name)) { return new American(); } return null; } }
修改后的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置工厂bean--> <bean id="personFactory" class="com.ly.spring.PersonFactory"></bean> <!-- factory-bean指定配置的工厂bean factory-method指定工厂类的实例方法 --> <bean id="chinese" factory-bean="personFactory" factory-method="getPerson"> <!--给工厂的实例方法注入参数--> <constructor-arg name="name" value="Chinese"></constructor-arg> <!--通过set方法给Chinese类的name属性注入值--> <property name="name" value="中国人"></property> </bean> <bean id="american" factory-bean="personFactory" factory-method="getPerson"> <constructor-arg name="name" value="American"></constructor-arg> <property name="name" value="美国人"></property> </bean> </beans>
4、扩展:利用工厂bean的知识注入Date类型的属性
Person:
package com.ly.spring; import java.util.Date; public class Person { private Date birthday; public void setBirthday(Date birthday) { this.birthday = birthday; } public void showBirthday() { System.out.println("我的生日是:"+this.birthday); } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--SimpleDateFormat可看作日期的工厂bean--> <bean id="simpleDateFormat" class="java.text.SimpleDateFormat"> <constructor-arg name="pattern" value="yyyy-MM-dd HH:mm:ss"></constructor-arg> </bean> <bean id="date" factory-bean="simpleDateFormat" factory-method="parse"> <constructor-arg name="source" value="2020-02-19 13:17:30"></constructor-arg> </bean> <bean id="person" class="com.ly.spring.Person"> <!--使用Person类的set方法注入属性值--> <property name="birthday" ref="date"></property> </bean> </beans>
测试类:
package com.ly.spring.test; import com.ly.spring.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Person person = context.getBean("person",Person.class); person.showBirthday(); } }
相识是缘