實體類java
public class User { private String username; private String password; public User() { System.out.println("User類的無參構造方法User()執行了"); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
配置文件spring
<?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="user" class="entity.User"></bean> </beans>
測試類app
public class TestSpring { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = applicationContext.getBean("user", User.class); System.out.println(user); } }
輸出結果測試
User類的無參構造方法User()執行了 entity.User@29774679
實體類this
public class User { private String username; private String password; public User() { System.out.println("User類的無參構造方法User()執行了"); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
工廠類code
public class UserFactory { private static User user; static { user = new User(); } public static User getInstance() { return user; } }
配置文件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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- factory-method:用於指定一個靜態工廠方法,Spring容器會調用這個靜態方法建立個對象。--> <bean id\="user" class\="entity.UserFactory" factory-method\="getInstance"\></bean> </beans>
測試類對象
public class TestSpring { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = applicationContext.getBean("user", User.class); System.out.println(user); } }
輸出結果get
User類的無參構造方法User()執行了 entity.User@5622fdf
實體類it
java.util.Calendar public final Date getTime()
配置文件
<?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="calendar" class="java.util.Calendar" factory-method="getInstance"></bean> <!-- 實例工廠方法註冊bean: factory-bean:用於指定一個bean的id factory-method:用於指定一個方法 --> <bean id="time" factory-bean="calendar" factory-method="getTime"></bean> </beans>
測試類
@Test public void test03() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Date time = ac.getBean("time",Date.class); System.out.println(time); }
測試結果
Mon Jan 21 00:28:07 CST 2019