1 简介
实例化bean有三种方式:
(1)通过构造函数实例化
(2)使用静态工厂方法实例化
(3)使用实例工厂方法实例化
2 通过构造函数实例化
2.1 准备bean-User类
package com.ws.edu.spring;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
super();
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
2.2 配置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">
<bean id="user" class="com.ws.edu.spring.User" />
</beans>
2.3 配置启动类
package com.ws.edu.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(context.getBean("user",User.class));
}
}
2.4 输出结果
3 使用静态工厂方法实例化
3.1 准备bean-User1类
package com.ws.edu.spring;
public class User1 {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private User1() {
super();
}
private static User1 user1 = new User1();
public static User1 getInstance(){
return user1;
}
@Override
public String toString() {
return "User1 [id=" + id + ", name=" + name + "]";
}
}
3.2 配置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">
<bean id="user" class="com.ws.edu.spring.User" />
<bean id="user1" class="com.ws.edu.spring.User1" factory-method="getInstance"/>
</beans>
3.3 修改启动类
package com.ws.edu.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(context.getBean("user",User.class));
System.out.println(context.getBean("user1",User1.class));
}
}
3.4 输出结果
4 使用实例工厂方法实例化
4.1 准备bean-User类
同2.1
4.2 准备实例工厂类
package com.ws.edu.spring;
public class UserFactory {
private User user = new User();
public User getUserInstance(){
return user;
}
}
4.3 配置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">
<bean id="user" class="com.ws.edu.spring.User" />
<bean id="user1" class="com.ws.edu.spring.User1" factory-method="getInstance"/>
<bean id="userFactory" class="com.ws.edu.spring.UserFactory"/>
<bean id="user2" class="com.ws.edu.spring.User" factory-bean="userFactory" factory-method="getUserInstance"/>
</beans>
4.4 配置启动类
package com.ws.edu.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(context.getBean("user",User.class));
System.out.println(context.getBean("user1",User1.class));
System.out.println(context.getBean("user2",User.class));
}
}