commit ebd76e8ed8da670232dc4e5b3962a7b4684d1223
Author: 廖雨心 <3562787443@qq.com>
Date: Thu Oct 10 10:50:05 2024 +0800
实验1的任务1提交
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 );
+ }
+}