2 Commits fd54441fdd ... 84e5cf737b

Auteur SHA1 Bericht Datum
  starlight_0208 84e5cf737b 2024年4月8日 21点06分 5 maanden geleden
  starlight_0208 45c12cd1e6 2024年4月8日 14点51分 5 maanden geleden

+ 15 - 0
pom.xml

@@ -24,6 +24,21 @@
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.1.16</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.33</version>
+        </dependency>
+        <dependency>
+            <groupId>c3p0</groupId>
+            <artifactId>c3p0</artifactId>
+            <version>0.9.1.2</version>
+        </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>

+ 12 - 25
src/main/java/org/starter/learning/AppTest.java

@@ -1,35 +1,22 @@
 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.starter.learning.dao.BookDao;
-import org.starter.learning.pojo.Book;
-import org.starter.learning.service.BookService;
-
-import java.util.List;
+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) {
-        // 获取IoC容器
-        // ApplicationContext 是一个接口需要使用new创建一个对应的实现类对象
-        // ClassPathXmlApplicationContext 是一个ApplicationContext的实现,用于从Classpath中的xml获取应用配置
-        ClassPathXmlApplicationContext 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();
-//        BookService bookService1 = (BookService) ctx.getBean("bookService");
-//        bookService1.listBook();
-//        System.out.println(BookService.class);
-//        BookDao bookDao1 = (BookDao) ctx.getBean("bookDao");
-//        BookDao bookDao2 = (BookDao) ctx.getBean("bookDao");
-//        System.out.println(bookDao1);
-//        System.out.println(bookDao2);
-        ctx.registerShutdownHook();
-        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
-        bookDao.getBooks();
-//        ctx.close();
+//        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
+        ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\Java Project\\learning\\src\\main\\resources\\applicationContext.xml");
+//        TestBase testBase = (TestBase) ctx.getBean("testBase");
+//        TestBase testBase = ctx.getBean("testBase", TestBase.class);
+        TestBase testBase = ctx.getBean(TestBase.class);
+        testBase.output();
+        System.out.println(System.getenv().get("USERNAME"));
     }
 }

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

@@ -0,0 +1,17 @@
+package org.starter.learning;
+
+
+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("application.xml");
+        BeanFactory bf = new XmlBeanFactory(resource);
+        TestBase testBase = bf.getBean(TestBase.class);
+        testBase.output();
+    }
+}

+ 5 - 0
src/main/java/org/starter/learning/test/TestBase.java

@@ -0,0 +1,5 @@
+package org.starter.learning.test;
+
+public interface TestBase {
+    void output();
+}

+ 40 - 0
src/main/java/org/starter/learning/test/TestBaseImpl.java

@@ -0,0 +1,40 @@
+package org.starter.learning.test;
+
+import java.util.*;
+
+public class TestBaseImpl implements TestBase {
+    private int[] array;
+    private List<String> list;
+    private Set<String> set;
+    private Map<String, String> map;
+    private Properties properties;
+
+    public void setArray(int[] array) {
+        this.array = array;
+    }
+
+    public void setList(List<String> list) {
+        this.list = list;
+    }
+
+    public void setSet(Set<String> set) {
+        this.set = set;
+    }
+
+    public void setMap(Map<String, String> map) {
+        this.map = map;
+    }
+
+    public void setProperties(Properties properties) {
+        this.properties = properties;
+    }
+
+    @Override
+    public void output() {
+        System.out.println("遍历数组:" + Arrays.toString(array));
+        System.out.println("遍历list:" + list);
+        System.out.println("遍历set:" + set);
+        System.out.println("遍历map:" + map);
+        System.out.println("遍历Properties:" + properties);
+    }
+}

+ 19 - 0
src/main/java/org/starter/learning/test/TestBaseImpl2.java

@@ -0,0 +1,19 @@
+package org.starter.learning.test;
+
+public class TestBaseImpl2 implements TestBase {
+    private String username;
+    private String password;
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    @Override
+    public void output() {
+        System.out.println(username + " " + password);
+    }
+}

+ 17 - 7
src/main/resources/applicationContext.xml

@@ -1,8 +1,18 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       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 id="bookDao" class="org.starter.learning.dao.impl.BookDaoImpl"/>
-    <bean id="bookService" class="org.starter.learning.service.impl.BookServiceImpl" autowire="byType" />
-    <bean id="bookDao2" class="org.starter.learning.dao.impl.BookDaoImpl" />
-</beans>
+    <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="testBase" class="org.starter.learning.test.TestBaseImpl2">
+        <property name="username" value="${username}" />
+        <property name="password" value="${jdbc.password}" />
+    </bean>
+
+    </beans>

+ 5 - 0
src/main/resources/jdbc.properties

@@ -0,0 +1,5 @@
+jdbc.driver = com.mysql.cj.jdbc.Driver
+jdbc.url = jdbc:mysql://localhost:3306/testdb
+jdbc.username = root
+jdbc.password = 123456
+username = root666