当前位置:网站首页>Three methods of bean instantiation
Three methods of bean instantiation
2022-06-21 07:51:00 【Xiao Zhao can order Java】
The first construction method ( Commonly used ):
step 1: Create an interface BookDao
package com.cdcas.dao;
public interface BookDao {
void save();
}
step 2: Create implementation classes BookDaoImpl
package com.cdcas.dao.impl;
import com.cdcas.dao.BookDao;
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("Spring Container instantiation by construction method BookImpl object ");
}
public void save(){
System.out.println("book dao save...");
}
}
step 3: stay xml Configuration in file bean
<bean id="bookDao" class="com.cdcas.dao.impl.BookDaoImpl"/>
step 4: To test
@org.junit.Test
public void test2(){
// obtain IoC Containers
ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
// Get through the construction method bean
BookDao bookDao=(BookDao) appCon.getBean("bookDao");
}
result 
The second kind of static factory ( understand ):
step 1: establish OrderDao Interface
package com.cdcas.dao;
public interface OrderDao {
void save();
}
step 2: establish OrderDaoImpl Implementation class
package com.cdcas.dao.impl;
import com.cdcas.dao.OrderDao;
public class OrderDaoImpl implements OrderDao {
@Override
public void save() {
System.out.println("Spring Containers are instantiated through static factories OderDaoImpl object ");
}
}
step 3: stay xml Configuration in file bean
<bean id="orderDao" class="com.cdcas.factory.OrderDaoFactory" factory-method="getOrderDao"/>
step 4: To test
@org.junit.Test
public void test3(){
// obtain IoC Containers
ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
// Obtained through a static factory bean
OrderDao orderDao=(OrderDao) appCon.getBean("orderDao");
orderDao.save();
}
result 
The third example factory ( understand )
step 1: establish UserDao Interface
package com.cdcas.dao;
public interface UserDao {
void save();
}
step 2: establish UserDaoImpl Implementation class
package com.cdcas.dao.impl;
import com.cdcas.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("Spring The container is instantiated through the instance factory UserDaoImpl object ");
}
}
step 3: Create an instance factory UserDaoFactory
package com.cdcas.factory;
import com.cdcas.dao.UserDao;
import com.cdcas.dao.impl.UserDaoImpl;
public class UserDaoFactory {
private static final UserDao userDao=new UserDaoImpl();
public UserDao getUserDao(){
return userDao;
}
}
step 4: To configure xml
<!-- Instance factory -->
<bean id="userFactory" class="com.cdcas.factory.UserDaoFactory"/>
<bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>
step 5: test
// Instance factory instance object
@org.junit.Test
public void test4(){
// obtain IoC Containers
ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
// Obtained through a static factory bean
UserDao userDao=(UserDao) appCon.getBean("userDao");
userDao.save();
}
result :
Implementation of the fourth method FactoryBean Interface :
By observing the example of xml To configure :
<!-- Instance factory -->
<bean id="userFactory" class="com.cdcas.factory.UserDaoFactory"/>
<bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>
We can find out , Only by instantiating a factory object can you call the methods in the factory to get UserDao Example . It's a hassle , therefore Spring
Provides FactoryBean Interface to solve this problem . Here is the implementation process :
step 1: establish UserDaoFactoryBean Implementation class implementation FactoryBean Interface
import com.cdcas.dao.UserDao;
import com.cdcas.dao.impl.UserDaoImpl;
import org.springframework.beans.factory.FactoryBean;
public class UserDaoFactoryBean implements FactoryBean {
private static UserDao userDao=new UserDaoImpl();
// Instead of creating objects in the original instance factory
@Override
public Object getObject() throws Exception {
return userDao;
}
@Override
public Class<?> getObjectType() {
return UserDao.class;
}
}
step 2xml:
<!-- Use FactoryBean Instantiation bean-->
<bean id="userDao2" class="com.cdcas.factory.UserDaoFactoryBean"/>
step 3:
// example FactoryBean Instance object
@org.junit.Test
public void test5(){
// obtain IoC Containers
ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
// Obtained through a static factory bean
UserDao userDao=(UserDao) appCon.getBean("userDao2");
userDao.save();
}
result :
边栏推荐
- Yyds dry goods inventory rapid establishment of CEPH cluster
- 2021-06-17 STM32F103 USART serial port code using firmware library
- MATLAB 三维图(非常规)
- How to start wireless network service after win10 system installation
- How to write circular statements in MySQL stored procedures
- [Redis]-[Redis底层数据结构]-SDS
- QML control type: drawer
- Illustration of Google V8 16: how does V8 improve function execution efficiency through inline caching?
- How to write the statement of executing stored procedure in MySQL
- Yyds dry goods inventory junit5 learning 3: assertions class
猜你喜欢

2021-07-28 STM32F103配置信息

RISC-V 的MMU

18 statistics and its sampling distribution chi square distribution-t distribution-f distribution

dried food! Neuron competitive initialization strategy based on information bottleneck theory

Dynamic programming to solve the problem of looting

ANSA二次开发 - 外部程序采用socket与ANSA实现通信

应用程序卡死,如何快速退出?

What is a multi domain SSL certificate?

Upgrade Jenkins steps and problems encountered

Weekly update | showmebug officially launched Tencent conference audio and video
随机推荐
Deploy ZABBIX enterprise level distributed monitoring
ETF operation practice record: February 22, 2022
AutoCAD - drawing units and drawing boundaries
Illustration Google V8 14: bytecode (2): how does the interpreter interpret and execute bytecode?
How to view the MySQL installation path
2021-07-28 STM32F103 I2C Hardware Transfer Include previous IO Clock EXIT USB use firmware library
MATLAB 三维图(非常规)
Practical application cases of digital Twins - coal mine
Flutter returns to the black screen of the previous page
mysql数据库拉链表是什么
19 statistics and its sampling distribution -- distribution of sample mean and central limit theorem
ANSA二次开发 - 外部程序采用socket与ANSA实现通信
2021-06-16 STM32F103 exti interrupt identification using firmware library
. Net 4.5 asynchronous programming pilot (async and await)
Market trend report, technical innovation and market forecast of inorganic microporous adsorbents in China
RDKit | 基于Murcko骨架聚类化合物库
How to use lerna to manage multiple packages
图解 Google V8 # 16:V8是怎么通过内联缓存来提升函数执行效率的?
图解 Google V8 # 15:隐藏类:如何在内存中快速查找对象属性?
2021-07-28 STM32F103 configuration information