<bean id="userDao" class="com.zhj.dao.impl.UserDaoImpl" ></bean>
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println(" save success");
}
}
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao =(UserDao) app.getBean("userDao");
userDao.save();
}
}
<bean id="userDao" class="com.zhj.factory.Factory" factory-method="factory" ></bean>
public class Factory {
public static UserDao factory(){
return new UserDaoImpl();
}
}
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao =(UserDao) app.getBean("userDao");
userDao.save();
}
}
<bean id="factory" class="com.zhj.factory.PlainFactory" ></bean>
<bean id="userDao" factory-bean="factory" factory-method="factory"></bean>
public class PlainFactory {
public UserDao factory(){
return new UserDaoImpl();
}
}
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao =(UserDao) app.getBean("userDao");
userDao.save();
}
}