From 2a2f9db103b72594878112d697b7d7d2982a6ed5 Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Sat, 12 Nov 2022 19:59:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/kwan/spring5/Book.java | 35 ++++++++++++++++++ src/main/java/com/kwan/spring5/Orders.java | 35 ++++++++++++++++++ src/main/java/com/kwan/spring5/User.java | 15 ++++++++ .../java/com/kwan/spring5/dao/UserDao.java | 12 +++++++ .../kwan/spring5/dao/impl/UserDaoImpl.java | 10 ++++++ .../com/kwan/spring5/service/UserService.java | 27 ++++++++++++++ src/main/resources/spring1.xml | 19 ++++++++++ src/main/resources/spring2-p.xml | 13 +++++++ src/main/resources/spring3.xml | 26 ++++++++++++++ src/main/resources/spring4.xml | 21 +++++++++++ src/test/java/BeanTest.java | 14 -------- src/test/java/BookTest_01.java | 23 ++++++++++++ src/test/java/BookTest_02.java | 23 ++++++++++++ src/test/java/BookTest_03.java | 22 ++++++++++++ src/test/java/OrdersTest.java | 23 ++++++++++++ src/test/java/UserServiceTest.java | 23 ++++++++++++ src/test/java/UserTest.java | 36 +++++++++++++++++++ target/classes/spring1.xml | 19 ++++++++++ 18 files changed, 382 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/kwan/spring5/Book.java create mode 100644 src/main/java/com/kwan/spring5/Orders.java create mode 100644 src/main/java/com/kwan/spring5/dao/UserDao.java create mode 100644 src/main/java/com/kwan/spring5/dao/impl/UserDaoImpl.java create mode 100644 src/main/java/com/kwan/spring5/service/UserService.java create mode 100644 src/main/resources/spring2-p.xml create mode 100644 src/main/resources/spring3.xml create mode 100644 src/main/resources/spring4.xml delete mode 100644 src/test/java/BeanTest.java create mode 100644 src/test/java/BookTest_01.java create mode 100644 src/test/java/BookTest_02.java create mode 100644 src/test/java/BookTest_03.java create mode 100644 src/test/java/OrdersTest.java create mode 100644 src/test/java/UserServiceTest.java create mode 100644 src/test/java/UserTest.java diff --git a/src/main/java/com/kwan/spring5/Book.java b/src/main/java/com/kwan/spring5/Book.java new file mode 100644 index 0000000..ef4005f --- /dev/null +++ b/src/main/java/com/kwan/spring5/Book.java @@ -0,0 +1,35 @@ +package com.kwan.spring5; +/** + * + * 书籍 set方法注入属性 + *@version : 2.2.0 + *@author : qinyingjie + *@date : 2022/11/12 19:22 + */ +public class Book { + /** + * 姓名 + */ + private String bname; + /** + * 作者 + */ + private String bauthor; + + //创建属性对应的set方法 + public void setBname(String bname) { + this.bname = bname; + } + + public void setBauthor(String bauthor) { + this.bauthor = bauthor; + } + + @Override + public String toString() { + return "Book{" + + "bname='" + bname + '\'' + + ", bauthor='" + bauthor + '\'' + + '}'; + } +} diff --git a/src/main/java/com/kwan/spring5/Orders.java b/src/main/java/com/kwan/spring5/Orders.java new file mode 100644 index 0000000..a870123 --- /dev/null +++ b/src/main/java/com/kwan/spring5/Orders.java @@ -0,0 +1,35 @@ +package com.kwan.spring5; + + +/** + * 有参构造函数注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 19:26 + */ +public class Orders { + /** + * 姓名 + */ + private String oname; + /** + * 地址 + */ + private String address; + + //有参数构造 + public Orders(String oname, String address) { + this.oname = oname; + this.address = address; + } + + + @Override + public String toString() { + return "Orders{" + + "oname='" + oname + '\'' + + ", address='" + address + '\'' + + '}'; + } +} diff --git a/src/main/java/com/kwan/spring5/User.java b/src/main/java/com/kwan/spring5/User.java index 9f1da90..19612b2 100644 --- a/src/main/java/com/kwan/spring5/User.java +++ b/src/main/java/com/kwan/spring5/User.java @@ -1,8 +1,23 @@ package com.kwan.spring5; +/** + * 用户 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 19:22 + */ public class User { + /** + * 普通方法 + */ public void say() { System.out.println("访问user成功"); } + + @Override + public String toString() { + return "User{}"; + } } diff --git a/src/main/java/com/kwan/spring5/dao/UserDao.java b/src/main/java/com/kwan/spring5/dao/UserDao.java new file mode 100644 index 0000000..8d062a8 --- /dev/null +++ b/src/main/java/com/kwan/spring5/dao/UserDao.java @@ -0,0 +1,12 @@ +package com.kwan.spring5.dao; + +/** + * user + * dao + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 19:34 + */ +public interface UserDao { + void update(); +} diff --git a/src/main/java/com/kwan/spring5/dao/impl/UserDaoImpl.java b/src/main/java/com/kwan/spring5/dao/impl/UserDaoImpl.java new file mode 100644 index 0000000..1afa6d6 --- /dev/null +++ b/src/main/java/com/kwan/spring5/dao/impl/UserDaoImpl.java @@ -0,0 +1,10 @@ +package com.kwan.spring5.dao.impl; + +import com.kwan.spring5.dao.UserDao; + +public class UserDaoImpl implements UserDao { + @Override + public void update() { + System.out.println("dao update..........."); + } +} diff --git a/src/main/java/com/kwan/spring5/service/UserService.java b/src/main/java/com/kwan/spring5/service/UserService.java new file mode 100644 index 0000000..ac8aed3 --- /dev/null +++ b/src/main/java/com/kwan/spring5/service/UserService.java @@ -0,0 +1,27 @@ +package com.kwan.spring5.service; + +import com.kwan.spring5.dao.UserDao; + +/** + * user服务类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 19:34 + */ +public class UserService { + + /** + * 创建UserDao类型属性,生成set方法 + */ + private UserDao userDao; + + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } + + public void add() { + System.out.println("service add..............."); + userDao.update();//调用dao方法 + } +} diff --git a/src/main/resources/spring1.xml b/src/main/resources/spring1.xml index 66b0724..2967571 100644 --- a/src/main/resources/spring1.xml +++ b/src/main/resources/spring1.xml @@ -7,5 +7,24 @@ http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/spring2-p.xml b/src/main/resources/spring2-p.xml new file mode 100644 index 0000000..d10f653 --- /dev/null +++ b/src/main/resources/spring2-p.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/spring3.xml b/src/main/resources/spring3.xml new file mode 100644 index 0000000..fb27139 --- /dev/null +++ b/src/main/resources/spring3.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + >]]> + + + + + \ No newline at end of file diff --git a/src/main/resources/spring4.xml b/src/main/resources/spring4.xml new file mode 100644 index 0000000..a3c235f --- /dev/null +++ b/src/main/resources/spring4.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/BeanTest.java b/src/test/java/BeanTest.java deleted file mode 100644 index eb59fca..0000000 --- a/src/test/java/BeanTest.java +++ /dev/null @@ -1,14 +0,0 @@ -import com.kwan.spring5.User; -import org.junit.Test; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class BeanTest { - - @Test - public void test1() { - ClassPathXmlApplicationContext ctx = - new ClassPathXmlApplicationContext("spring1.xml"); - User user = (User) ctx.getBean("user"); - user.say(); - } -} diff --git a/src/test/java/BookTest_01.java b/src/test/java/BookTest_01.java new file mode 100644 index 0000000..0be8158 --- /dev/null +++ b/src/test/java/BookTest_01.java @@ -0,0 +1,23 @@ +import com.kwan.spring5.Book; +import com.kwan.spring5.User; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * 测试ioc注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 18:43 + */ +public class BookTest_01 { + + @Test + public void test1() { + ApplicationContext ctx = + new ClassPathXmlApplicationContext("spring1.xml"); + Book book = (Book) ctx.getBean("book"); + System.out.println(book); + } +} \ No newline at end of file diff --git a/src/test/java/BookTest_02.java b/src/test/java/BookTest_02.java new file mode 100644 index 0000000..646038e --- /dev/null +++ b/src/test/java/BookTest_02.java @@ -0,0 +1,23 @@ +import com.kwan.spring5.Book; +import com.kwan.spring5.Orders; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * 测试ioc注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 18:43 + */ +public class BookTest_02 { + + @Test + public void test1() { + ApplicationContext ctx = + new ClassPathXmlApplicationContext("spring2-p.xml"); + Book book = (Book) ctx.getBean("book"); + System.out.println(book); + } +} \ No newline at end of file diff --git a/src/test/java/BookTest_03.java b/src/test/java/BookTest_03.java new file mode 100644 index 0000000..d0e001f --- /dev/null +++ b/src/test/java/BookTest_03.java @@ -0,0 +1,22 @@ +import com.kwan.spring5.Book; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * 测试ioc注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 18:43 + */ +public class BookTest_03 { + + @Test + public void test1() { + ApplicationContext ctx = + new ClassPathXmlApplicationContext("spring3.xml"); + Book book = (Book) ctx.getBean("book"); + System.out.println(book); + } +} \ No newline at end of file diff --git a/src/test/java/OrdersTest.java b/src/test/java/OrdersTest.java new file mode 100644 index 0000000..73a7f61 --- /dev/null +++ b/src/test/java/OrdersTest.java @@ -0,0 +1,23 @@ +import com.kwan.spring5.Book; +import com.kwan.spring5.Orders; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * 测试ioc注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 18:43 + */ +public class OrdersTest { + + @Test + public void test1() { + ApplicationContext ctx = + new ClassPathXmlApplicationContext("spring1.xml"); + Orders orders = (Orders) ctx.getBean("orders"); + System.out.println(orders); + } +} \ No newline at end of file diff --git a/src/test/java/UserServiceTest.java b/src/test/java/UserServiceTest.java new file mode 100644 index 0000000..29eae6d --- /dev/null +++ b/src/test/java/UserServiceTest.java @@ -0,0 +1,23 @@ +import com.kwan.spring5.User; +import com.kwan.spring5.service.UserService; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * 测试ioc注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 18:43 + */ +public class UserServiceTest { + + @Test + public void test2() { + ApplicationContext ctx = + new ClassPathXmlApplicationContext("spring4.xml"); + UserService userService = (UserService) ctx.getBean("userService"); + userService.add(); + } +} \ No newline at end of file diff --git a/src/test/java/UserTest.java b/src/test/java/UserTest.java new file mode 100644 index 0000000..3445525 --- /dev/null +++ b/src/test/java/UserTest.java @@ -0,0 +1,36 @@ +import com.kwan.spring5.User; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * 测试ioc注入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/11/12 18:43 + */ +public class UserTest { + + @Test + public void test1() { + System.out.println("test1"); + ClassPathXmlApplicationContext ctx = + new ClassPathXmlApplicationContext("spring1.xml"); + User user = (User) ctx.getBean("user"); + user.say(); + } + + + @Test + public void test2() { + System.out.println("test2"); + ApplicationContext ctx = + new ClassPathXmlApplicationContext("spring1.xml"); + User user = (User) ctx.getBean("user"); + user.say(); + } +} + +//1.修改快捷键爱你 +//2.修改测试类名 \ No newline at end of file diff --git a/target/classes/spring1.xml b/target/classes/spring1.xml index 66b0724..2967571 100644 --- a/target/classes/spring1.xml +++ b/target/classes/spring1.xml @@ -7,5 +7,24 @@ http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> + + + + + + + + + + + + + + + + \ No newline at end of file -- GitLab