单例模式
@Scope("singleton")
BookDaoImpl
@SuppressWarnings("all") | |
@Repository | |
@Scope("singleton") | |
public class BookDaoImpl implements BookDao { | |
public void save(){ | |
System.out.println("BookDaoImpl save rning..."); | |
} | |
} |
测试代码
public void test(){ | |
ApplicationContext c = new AnnotationConfigApplicationContext(SpringConfig.class); | |
BookDao dao = c.getBean(BookDao.class); | |
System.out.println(dao); | |
BookDao dao1 = c.getBean(BookDao.class); | |
System.out.println(dao1); | |
dao.save(); | |
} |
Run 结果
非单例模式
@Scope("prototype")
BookDaoImpl
@SuppressWarnings("all") | |
@Repository | |
@Scope("prototype") | |
public class BookDaoImpl implements BookDao { | |
public void save(){ | |
System.out.println("BookDaoImpl save rning..."); | |
} | |
} |
测试类代码
public void test(){ | |
ApplicationContext c = new AnnotationConfigApplicationContext(SpringConfig.class); | |
BookDao dao = c.getBean(BookDao.class); | |
System.out.println(dao); | |
BookDao dao1 = c.getBean(BookDao.class); | |
System.out.println(dao1); | |
dao.save(); | |
} |
Run Result