✨博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞🎀收藏⭐留言✒
✨系列专栏:👉Spring专栏
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
✨欢迎大佬指正,一起学习!一起加油!
@TOC
✨构造方法实例化Bean(无参构造)
🔥提供可访问的构造方法:
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。
✨使用静态工厂实例化Bean
🔥静态工厂
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>
✨使用实例化工厂实例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>
✨使用FactoryBean实例化Bean
🔥FactoryBean
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>