Browse Source

2024年4月8日 21点06分

starlight_0208 5 months ago
parent
commit
84e5cf737b

+ 11 - 5
src/main/java/org/starter/learning/AppTest.java

@@ -3,14 +3,20 @@ package org.starter.learning;
 
 
 import com.alibaba.druid.pool.DruidDataSource;
 import com.alibaba.druid.pool.DruidDataSource;
 import com.mchange.v2.c3p0.ComboPooledDataSource;
 import com.mchange.v2.c3p0.ComboPooledDataSource;
+import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 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 class AppTest {
     public static void main(String[] args) {
     public static void main(String[] args) {
-        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
-        ctx.registerShutdownHook();
-//        DruidDataSource dataSource = (DruidDataSource) ctx.getBean("dataSource");
-        ComboPooledDataSource dataSource = (ComboPooledDataSource) ctx.getBean("dataSource");
-        System.out.println(dataSource);
+//        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();
+    }
+}

+ 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);
+    }
+}

+ 16 - 56
src/main/resources/applicationContext.xml

@@ -1,58 +1,18 @@
 <?xml version="1.0" encoding="UTF-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"
-       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" />
-    <bean id="bookDao2" class="org.starter.learning.dao.impl.BookDaoImpl" />
-    <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>
+    <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>
     </bean>
-    <bean id="dataSourceDeprecated" class="com.alibaba.druid.pool.DruidDataSource">
-        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
-        <property name="url" value="jdbc:mysql://localhost:3306/testdb" />
-        <property name="username" value="root" />
-        <property name="password" value="123456" />
-    </bean>
-    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
-        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
-        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb" />
-        <property name="user" value="root" />
-        <property name="password" value="123456" />
-    </bean>
-</beans>
+
+    </beans>

+ 2 - 1
src/main/resources/jdbc.properties

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