1.通过创建静态工厂的方法进行创建bean
代码:
- com.phome.dao.DAO -> dao
public class DAO {
// 创建一个当前类的静态的实例对象
private static DAO dao = new DAO();
// 构造方法私有化
private DAO() {
}
// 创建一个外部访问当前对象的方法
public static DAO getDao(){
System.out.println("getDao()的方法被执行了");
return dao;
}
public void add(){
System.out.println("的add方法正在执行...");
}
}
// 创建一个当前类的静态的实例对象
private static DAO dao = new DAO();
// 构造方法私有化
private DAO() {
}
// 创建一个外部访问当前对象的方法
public static DAO getDao(){
System.out.println("getDao()的方法被执行了");
return dao;
}
public void add(){
System.out.println("的add方法正在执行...");
}
}
- com.phome.service ->service
public class DAO {
// 创建一个当前类的静态的实例对象
private static DAO dao = new DAO();
// 构造方法私有化
private DAO() {
}
// 创建一个外部访问当前对象的方法
public static DAO getDao(){
System.out.println("getDao()的方法被执行了");
return dao;
}
public void add(){
System.out.println("的add方法正在执行...");
}
}
// 创建一个当前类的静态的实例对象
private static DAO dao = new DAO();
// 构造方法私有化
private DAO() {
}
// 创建一个外部访问当前对象的方法
public static DAO getDao(){
System.out.println("getDao()的方法被执行了");
return dao;
}
public void add(){
System.out.println("的add方法正在执行...");
}
}
- com.phome.main -> main
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.phome.service.Service;
public class Main {
public static void main(String[] args) {
// 获得IOC容器工厂
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
// 从容器中获取对应id名称的对象
Service service = (Service)factory.getBean("service");
// 执行对象方法
service.regist();
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.phome.service.Service;
public class Main {
public static void main(String[] args) {
// 获得IOC容器工厂
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
// 从容器中获取对应id名称的对象
Service service = (Service)factory.getBean("service");
// 执行对象方法
service.regist();
}
}
- spring.xml中的配置( 新建xml文件命名为spring.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="dao" class="com.phome.dao.DAO" factory-method="getDao"></bean>
<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>
</beans>
<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="dao" class="com.phome.dao.DAO" factory-method="getDao"></bean>
<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>
</beans>
2.通过创建构造方法创建bean(注意里面的service类与main类与第一种方法中的一样这里就不重复写了)
代码:
- com.phome.dao -> DAO
public class DAO {
private String name;
// 提供构造方法
public DAO() {
}
public DAO(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println(name + "的add方法正在执行...");
}
}
private String name;
// 提供构造方法
public DAO() {
}
public DAO(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println(name + "的add方法正在执行...");
}
}
- spring.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="dao" class="com.phome.dao.DAO">
<!-- 方法1 -->
<constructor-arg index="0" value="mysqldependecyinjection"></constructor-arg>
<!-- 方法2 -->
<!-- <constructor-arg type="java.lang.String" value="hello"></constructor-arg> -->
</bean>
<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>
</beans>
<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="dao" class="com.phome.dao.DAO">
<!-- 方法1 -->
<constructor-arg index="0" value="mysqldependecyinjection"></constructor-arg>
<!-- 方法2 -->
<!-- <constructor-arg type="java.lang.String" value="hello"></constructor-arg> -->
</bean>
<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>
</beans>
3.通过创建工厂的方法创建bean(注意里面的service类与main类与第一种方法中的一样这里就不重复写了)
代码:
- com.phome.dao -> DAO
public class DAO {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println(name + "的add方法正在执行...");
}
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println(name + "的add方法正在执行...");
}
}
- com.phome.dao -> DaoFactory
/**
* 生产Dao的工厂
*
*/
public class DaoFactory {
private DAO dao = new DAO();
private DaoFactory() {
}
public DAO getDao() {
System.out.println("DAOFactory的getDao方法执行");
return dao;
}
}
* 生产Dao的工厂
*
*/
public class DaoFactory {
private DAO dao = new DAO();
private DaoFactory() {
}
public DAO getDao() {
System.out.println("DAOFactory的getDao方法执行");
return dao;
}
}
- spring.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="daofactory" class="com.phome.dao.DaoFactory"></bean>
<bean id="dao" factory-bean="daofactory" factory-method="getDao">
<property name="name" value="mysql"></property>
</bean>
<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>
</beans>
<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="daofactory" class="com.phome.dao.DaoFactory"></bean>
<bean id="dao" factory-bean="daofactory" factory-method="getDao">
<property name="name" value="mysql"></property>
</bean>
<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>
</beans>