Kaynağa Gözat

2024年4月2日 19点13分

Starlight-0208 7 ay önce
ebeveyn
işleme
12c1c8b429

+ 6 - 4
src/main/java/org/starter/learning/AppTest.java

@@ -16,9 +16,11 @@ public class AppTest {
         // ClassPathXmlApplicationContext 是一个ApplicationContext的实现,用于从Classpath中的xml获取应用配置
         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
         // 获取Bean
-        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
-        List<Book> bookList = bookDao.getBooks();
-        BookService bookService = (BookService) ctx.getBean("bookService");
-        List<Book> bookList1 = bookService.listBook();
+//        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
+//        List<Book> bookList = bookDao.getBooks();
+//        BookService bookService = (BookService) ctx.getBean("bookService");
+//        List<Book> bookList1 = bookService.listBook();
+        BookService bookService1 = (BookService) ctx.getBean("bookService");
+        bookService1.listBook();
     }
 }

+ 9 - 1
src/main/java/org/starter/learning/dao/impl/BookDaoImpl.java

@@ -3,11 +3,19 @@ package org.starter.learning.dao.impl;
 import org.starter.learning.dao.BookDao;
 import org.starter.learning.pojo.Book;
 
+import java.util.ArrayList;
 import java.util.List;
 
 public class BookDaoImpl implements BookDao {
+    public BookDaoImpl() {
+        System.out.println("BookDao object created!");
+    }
     @Override
     public List<Book> getBooks() {
-        return null;
+        List<Book> bookList = new ArrayList<>();
+        bookList.add(new Book("112233", "Hello World", "asm"));
+        bookList.add(new Book("112234", "Hello World2", "asm2"));
+        bookList.add(new Book("112235", "Hello World3", "asm3"));
+        return  bookList;
     }
 }

+ 11 - 0
src/main/java/org/starter/learning/factory/BookDaoFactory.java

@@ -0,0 +1,11 @@
+package org.starter.learning.factory;
+
+import org.starter.learning.dao.BookDao;
+import org.starter.learning.dao.impl.BookDaoImpl;
+
+public class BookDaoFactory {
+    public static BookDao getBookDao() {
+        System.out.println("Get one bean from BookDaoFactory.");
+        return new BookDaoImpl();
+    }
+}

+ 10 - 0
src/main/java/org/starter/learning/factory/BookServiceFactory.java

@@ -0,0 +1,10 @@
+package org.starter.learning.factory;
+
+import org.starter.learning.service.BookService;
+import org.starter.learning.service.impl.BookServiceImpl;
+
+public class BookServiceFactory {
+    public BookService getBookService() {
+        return new BookServiceImpl();
+    }
+}

+ 3 - 0
src/main/java/org/starter/learning/service/impl/BookServiceImpl.java

@@ -9,6 +9,9 @@ import java.util.List;
 
 public class BookServiceImpl implements BookService {
     private BookDao bookDao = new BookDaoImpl();
+    public BookServiceImpl() {
+        System.out.println("BookService object created!");
+    }
     @Override
     public List<Book> listBook() {
         return bookDao.getBooks();

+ 9 - 1
src/main/resources/applicationConfig.xml → src/main/resources/applicationContext.xml

@@ -3,7 +3,15 @@
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <!--配置Bean-->
-    <bean id="bookDao" class="org.starter.learning.dao.impl.BookDaoImpl" />
+<!--    <bean id="bookDao" class="org.starter.learning.dao.impl.BookDaoImpl" />-->
+    <!--配置方式2:工厂模式-->
+    <bean id="bookDao" class="org.starter.learning.factory.BookDaoFactory" factory-method="getBookDao" />
+    <!-- 配置方式3:实例工厂模式 -->
+    <!-- 将实例工厂本身实例化 -->
+    <bean id="bookServiceFactory" class="org.starter.learning.factory.BookServiceFactory" />
+    <!-- 通过工厂本身来创建对象 -->
+    <bean id="book" factory-bean="bookServiceFactory" factory-method="getBookService" />
+
     <bean id="bookService" class="org.starter.learning.service.impl.BookServiceImpl">
         <!--配置Service和Dao之间的关系-->
         <!--property标签表示配置当前bean的属性-->