package com.study.dao.impl;
import com.study.dao.BookDao;
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("无参构造!");
}
public void save() {
System.out.println("book dao save...");
}
}
<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl" ></bean>
注意:
如果无参构造方法不存在则抛出BeabCreationException。
package com.study.factory;
import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
public class BookDaoFactory {
public static BookDao getBookDao(){
return new BookDaoImpl();
}
}
<bean id="bookDao" name="dao" class="com.study.factory.BookDaoFactory" factory-method="getBookDao"></bean>
package com.study.factory;
import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
public class BookDaoFactory {
public BookDao getBookDao(){
return new BookDaoImpl();
}
}
}
<bean id="bookDao" name="dao" class="com.study.factory.BookDaoFactory"></bean>
<bean id="book" factory-method="getBookDao" factory-bean="bookDao"></bean>
package com.study.factory;
import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
import org.springframework.beans.factory.FactoryBean;
public class BookDaoFactoryBean implements FactoryBean<BookDao> {
public BookDao getObject() throws Exception {
return new BookDaoImpl();
}
public Class<?> getObjectType() {
return BookDao.class;
}
}
<bean id="bookDao" class="com.study.factory.BookDaoFactoryBean"></bean>