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>
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>
Person:java
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:spring
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:ide
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; } }
配置文件:this
<?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>
測試類:spa
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類的方法 } }
修改後的工廠類PersonFactory:code
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; } }
修改後的配置文件:orm
<?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>
Person:xml
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); } }
配置文件:blog
<?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(); } }