From ebd76e8ed8da670232dc4e5b3962a7b4684d1223 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BB=96=E9=9B=A8=E5=BF=83?= <3562787443@qq.com>
Date: Thu, 10 Oct 2024 10:50:05 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E9=AA=8C1=E7=9A=84=E4=BB=BB=E5=8A=A11?=
=?UTF-8?q?=E6=8F=90=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 32 ++++++++++++++++
src/main/java/com/hnucm/spring/App.java | 33 ++++++++++++++++
src/main/java/com/hnucm/spring/UserDao.java | 15 ++++++++
.../java/com/hnucm/spring/UserService.java | 23 +++++++++++
src/main/resources/app.xml | 22 +++++++++++
src/test/java/com/hnucm/spring/AppTest.java | 38 +++++++++++++++++++
6 files changed, 163 insertions(+)
create mode 100644 pom.xml
create mode 100644 src/main/java/com/hnucm/spring/App.java
create mode 100644 src/main/java/com/hnucm/spring/UserDao.java
create mode 100644 src/main/java/com/hnucm/spring/UserService.java
create mode 100644 src/main/resources/app.xml
create mode 100644 src/test/java/com/hnucm/spring/AppTest.java
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..0cd6210
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,32 @@
+
+ 4.0.0
+
+ com.hnucm.spring
+ spring
+ 1.0-SNAPSHOT
+ jar
+
+ spring
+ http://maven.apache.org
+
+
+ UTF-8
+
+
+
+
+ junit
+ junit
+ 3.8.1
+ test
+
+
+
+ org.springframework
+ spring-context
+ 6.1.12
+
+
+
+
diff --git a/src/main/java/com/hnucm/spring/App.java b/src/main/java/com/hnucm/spring/App.java
new file mode 100644
index 0000000..23b3f5c
--- /dev/null
+++ b/src/main/java/com/hnucm/spring/App.java
@@ -0,0 +1,33 @@
+package com.hnucm.spring;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Hello world!
+ *
+ */
+public class App
+{
+ public static void main( String[] args ) {
+ // UserDao userDao2 = new UserDao();
+ // userDao.findUser();
+ // Spring容器创建对象xml文件中
+
+ // Spring容器中取出对象 -》 解耦 控制反转
+ // 在使用Spring框架之后,对象的实例不再由调用者来创建,而是由Spring容器来创建
+
+ ApplicationContext applicationContext =
+ new ClassPathXmlApplicationContext("app.xml");
+
+ UserDao userDao = (UserDao) applicationContext.getBean("userDao");
+ userDao.findUser();
+
+ UserService userService = (UserService) applicationContext.getBean("userService");
+ // // 调用注入值 -》 Spring 注入
+ // // userService.setUserDao(userDao);
+ userService.findUser();
+ }
+
+
+}
diff --git a/src/main/java/com/hnucm/spring/UserDao.java b/src/main/java/com/hnucm/spring/UserDao.java
new file mode 100644
index 0000000..e515082
--- /dev/null
+++ b/src/main/java/com/hnucm/spring/UserDao.java
@@ -0,0 +1,15 @@
+package com.hnucm.spring;
+
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Repository;
+
+// Component Spring容器创建UserDao对象 对象的名字id = userDao
+@Repository
+public class UserDao {
+
+ // 调用findUser()
+ public void findUser(){
+ System.out.println("查询用户");
+ }
+}
diff --git a/src/main/java/com/hnucm/spring/UserService.java b/src/main/java/com/hnucm/spring/UserService.java
new file mode 100644
index 0000000..3cf4964
--- /dev/null
+++ b/src/main/java/com/hnucm/spring/UserService.java
@@ -0,0 +1,23 @@
+package com.hnucm.spring;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Controller;
+import org.springframework.stereotype.Repository;
+import org.springframework.stereotype.Service;
+
+// id = userService
+// @Component =Service =Controller =Repository
+@Service
+public class UserService {
+
+ // Autowired Spring自动注入 + setter+getter
+ @Autowired
+ private UserDao userDao;
+
+
+ // 如何调用Spring容器 finduser
+ public void findUser(){
+ userDao.findUser();
+ }
+}
diff --git a/src/main/resources/app.xml b/src/main/resources/app.xml
new file mode 100644
index 0000000..8e0b646
--- /dev/null
+++ b/src/main/resources/app.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/java/com/hnucm/spring/AppTest.java b/src/test/java/com/hnucm/spring/AppTest.java
new file mode 100644
index 0000000..c047147
--- /dev/null
+++ b/src/test/java/com/hnucm/spring/AppTest.java
@@ -0,0 +1,38 @@
+package com.hnucm.spring;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite( AppTest.class );
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue( true );
+ }
+}