2 Revize fdaab98bc0 ... 7d2771fb16

Autor SHA1 Zpráva Datum
  Starlight-0208 7d2771fb16 2024年4月9日 19点06分 před 5 měsíci
  Starlight-0208 58e592b05d 2024年4月9日 12点58分 před 5 měsíci

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

@@ -1,17 +1,12 @@
 package org.starter.learning;
 
 
-import com.alibaba.druid.pool.DruidDataSource;
-import com.mchange.v2.c3p0.ComboPooledDataSource;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
-import org.springframework.context.support.FileSystemXmlApplicationContext;
-import org.starter.learning.test.TestBase;
-import org.starter.learning.test.TestBaseImpl2;
 
 public class AppTest {
     public static void main(String[] args) {
-        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
+        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml.bak");
 //        TestBase testBase = ctx.getBean(TestBase.class);
 //        testBase.output();
     }

+ 17 - 0
src/main/java/org/starter/learning/AppTestForAnnotaion.java

@@ -0,0 +1,17 @@
+package org.starter.learning;
+
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.starter.learning.config.SpringConfig;
+import org.starter.learning.dao.BookDao;
+
+public class AppTestForAnnotaion {
+    public static void main(String[] args) {
+        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
+        BookDao bookDao = ctx.getBean(BookDao.class);
+        System.out.println(bookDao);
+        bookDao.getBooks();
+    }
+}

+ 1 - 2
src/main/java/org/starter/learning/AppTestForBeanFactory.java

@@ -5,11 +5,10 @@ import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.xml.XmlBeanFactory;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
-import org.starter.learning.test.TestBase;
 
 public class AppTestForBeanFactory {
     public static void main(String[] args) {
-        Resource resource = new ClassPathResource("applicationContext.xml");
+        Resource resource = new ClassPathResource("applicationContext.xml.bak");
         BeanFactory bf = new XmlBeanFactory(resource);
 //        TestBase testBase = bf.getBean(TestBase.class);
 //        testBase.output();

+ 9 - 0
src/main/java/org/starter/learning/config/SpringConfig.java

@@ -0,0 +1,9 @@
+package org.starter.learning.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@ComponentScan("org.starter.learning")
+public class SpringConfig {
+}

+ 8 - 0
src/main/java/org/starter/learning/dao/impl/BookDaoImpl.java

@@ -1,11 +1,17 @@
 package org.starter.learning.dao.impl;
 
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
 import org.starter.learning.dao.BookDao;
 import org.starter.learning.pojo.Book;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import java.util.ArrayList;
 import java.util.List;
 
+@Component("bookDao")
+@Scope("singleton")
 public class BookDaoImpl implements BookDao {
     public BookDaoImpl() {
         System.out.println("BookDao object created!");
@@ -20,11 +26,13 @@ public class BookDaoImpl implements BookDao {
     }
 
     // Bean创建后执行的方法
+    @PostConstruct
     public void init() {
         System.out.println("After create...");
     }
 
     // Bean销毁前执行的方法
+    @PreDestroy
     public void destroy() {
         System.out.println("Before destroy...");
     }

+ 2 - 1
src/main/java/org/starter/learning/factory/BookServiceFactory.java

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

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

@@ -20,4 +20,8 @@ public class BookServiceImpl implements BookService {
         System.out.println("Setting bookDao into BookService Implement...");
         this.bookDao = bookDao;
     }
+
+    public BookServiceImpl(BookDao bookDao) {
+        this.bookDao = bookDao;
+    }
 }

+ 0 - 57
src/main/resources/applicationContext.xml

@@ -1,57 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-    <beans xmlns="http://www.springframework.org/schema/beans"
-           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-           xmlns:context="http://www.springframework.org/schema/context"
-           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
-    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER" />
-<!--        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
-<!--            <property name="driverClassName" value="${jdbc.driver}" />-->
-<!--            <property name="url" value="${jdbc.url}" />-->
-<!--            <property name="username" value="${jdbc.username}" />-->
-<!--            <property name="password" value="${jdbc.password}" />-->
-<!--        </bean>-->
-<!--    <bean id="testBaseOld" class="org.starter.learning.test.TestBaseImpl2">-->
-<!--        <property name="username" value="${jdbc.username}" />-->
-<!--        <property name="password" value="${jdbc.password}" />-->
-<!--    </bean>-->
-
-    <bean id="testBase" class="org.starter.learning.test.TestBaseImpl" >
-        <property name="array">
-            <array>
-                <value>100</value>
-                <value>200</value>
-                <value>300</value>
-            </array>
-        </property>
-        <property name="list">
-            <list>
-                <value>string1</value>
-                <value>string2</value>
-                <value>string3</value>
-            </list>
-        </property>
-        <property name="set">
-            <set>
-                <value>value1</value>
-                <value>value2</value>
-                <value>value2</value>
-                <value>value3</value>
-            </set>
-        </property>
-        <property name="map">
-            <map>
-                <entry key="country" value="China" />
-                <entry key="province" value="Shanxi" />
-                <entry key="city" value="Datong" />
-            </map>
-        </property>
-        <property name="properties">
-            <props>
-                <prop key="country" >China</prop>
-                <prop key="province">ShanXi</prop>
-                <prop key="city">Datong</prop>
-            </props>
-        </property>
-    </bean>
-    <bean id="bookDao" class="org.starter.learning.dao.impl.BookDaoImpl" lazy-init="true" />
-    </beans>

+ 8 - 0
src/main/resources/applicationContext.xml.bak

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+    <beans xmlns="http://www.springframework.org/schema/beans"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xmlns:context="http://www.springframework.org/schema/context"
+           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
+    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER" />
+    <context:component-scan base-package="org.starter.learning.dao.impl" />
+</beans>