mirror of
https://github.com/Estom/notes.git
synced 2026-04-24 02:21:45 +08:00
spring 完结
This commit is contained in:
17
Spring/Spring5/code/shangguigu02/src/Main.java
Normal file
17
Spring/Spring5/code/shangguigu02/src/Main.java
Normal file
@@ -0,0 +1,17 @@
|
||||
import com.ykl.User;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version $Id: ${NAME}, v 0.1 ${YEAR}-${MONTH}-${DAY} ${TIME} yinkanglong Exp $
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
//自己创建对象
|
||||
// User user = new User();
|
||||
// user.add();
|
||||
|
||||
//通过spring创建对象
|
||||
|
||||
}
|
||||
}
|
||||
23
Spring/Spring5/code/shangguigu02/src/bean01.xml
Normal file
23
Spring/Spring5/code/shangguigu02/src/bean01.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?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">
|
||||
|
||||
<!-- 配置User bean -->
|
||||
<bean id="user" class="com.ykl.User"></bean>
|
||||
|
||||
<!-- 配置Book对象和属性注入-->
|
||||
<bean id="book" class="com.ykl.Book">
|
||||
<!--
|
||||
name:类里面属性的名称
|
||||
value:向属性注入的值
|
||||
-->
|
||||
<property name="name" value="shuming"></property>
|
||||
</bean>
|
||||
|
||||
<bean id="order" class="com.ykl.Order">
|
||||
<constructor-arg name="oname" value="abc"></constructor-arg>
|
||||
<constructor-arg name="address" value="China"></constructor-arg>
|
||||
</bean>
|
||||
</beans>
|
||||
|
||||
19
Spring/Spring5/code/shangguigu02/src/bean02.xml
Normal file
19
Spring/Spring5/code/shangguigu02/src/bean02.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?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">
|
||||
|
||||
<!-- 配置User bean -->
|
||||
<bean id="userService" class="com.ykl.service.UserService">
|
||||
<!--
|
||||
注入userDao的对象
|
||||
name属性值:类里面的属性名称
|
||||
ref属性:创建UserDao对象bean标签的id值。
|
||||
-->
|
||||
<property name="userDao" ref="userDao"></property>
|
||||
|
||||
</bean>
|
||||
|
||||
<bean id="userDao" class="com.ykl.dao.UserDaoImpl"></bean>
|
||||
</beans>
|
||||
|
||||
26
Spring/Spring5/code/shangguigu02/src/bean03.xml
Normal file
26
Spring/Spring5/code/shangguigu02/src/bean03.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?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">
|
||||
|
||||
<!-- 配置User bean -->
|
||||
<bean id="emp" class="com.ykl.bean.Emp">
|
||||
<!--
|
||||
普通属性
|
||||
-->
|
||||
<property name="ename" value="lucy"></property>
|
||||
<property name="gender" value="nv"></property>
|
||||
<!--
|
||||
级联对象的创建方法
|
||||
-->
|
||||
<property name="dept">
|
||||
<bean id="dept" class="com.ykl.bean.Dept">
|
||||
<property name="dname" value="security"></property>
|
||||
</bean>
|
||||
</property>
|
||||
|
||||
</bean>
|
||||
|
||||
<bean id="userDao" class="com.ykl.dao.UserDaoImpl"></bean>
|
||||
</beans>
|
||||
|
||||
45
Spring/Spring5/code/shangguigu02/src/com/ykl/Book.java
Normal file
45
Spring/Spring5/code/shangguigu02/src/com/ykl/Book.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Book, v 0.1 2022-10-08 14:32 yinkanglong Exp $
|
||||
*/
|
||||
public class Book {
|
||||
/**
|
||||
* Getter method for property <tt>name</tt>.
|
||||
*
|
||||
* @return property value of name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
//set方法注入
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//有参构造函数注入
|
||||
public Book(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Book(){
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
//使用set方法注入
|
||||
Book book = new Book();
|
||||
book.setName("123");
|
||||
|
||||
//使用有参构造函数注入
|
||||
Book book2 = new Book("abc");
|
||||
}
|
||||
}
|
||||
23
Spring/Spring5/code/shangguigu02/src/com/ykl/Order.java
Normal file
23
Spring/Spring5/code/shangguigu02/src/com/ykl/Order.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Order, v 0.1 2022-10-08 14:47 yinkanglong Exp $
|
||||
*/
|
||||
public class Order {
|
||||
private String oname;
|
||||
private String address;
|
||||
|
||||
public Order(String oname, String address) {
|
||||
this.oname = oname;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public void test(){
|
||||
System.out.println(this.oname+this.address);
|
||||
}
|
||||
}
|
||||
42
Spring/Spring5/code/shangguigu02/src/com/ykl/User.java
Normal file
42
Spring/Spring5/code/shangguigu02/src/com/ykl/User.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : User, v 0.1 2022-10-08 11:22 yinkanglong Exp $
|
||||
*/
|
||||
public class User {
|
||||
public User(String username) {
|
||||
Username = username;
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
/**
|
||||
* Getter method for property <tt>Username</tt>.
|
||||
*
|
||||
* @return property value of Username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return Username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>counterType</tt>.
|
||||
*
|
||||
* @param Username value to be assigned to property Username
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
Username = username;
|
||||
}
|
||||
|
||||
private String Username;
|
||||
|
||||
|
||||
public void add(){
|
||||
System.out.println("add user");
|
||||
}
|
||||
}
|
||||
29
Spring/Spring5/code/shangguigu02/src/com/ykl/bean/Dept.java
Normal file
29
Spring/Spring5/code/shangguigu02/src/com/ykl/bean/Dept.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.bean;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Dept, v 0.1 2022-10-08 15:33 yinkanglong Exp $
|
||||
*/
|
||||
public class Dept {
|
||||
private String dname;
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>counterType</tt>.
|
||||
*
|
||||
* @param dname value to be assigned to property pname
|
||||
*/
|
||||
public void setDname(String dname) {
|
||||
this.dname = dname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dept{" +
|
||||
"pname='" + dname + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
38
Spring/Spring5/code/shangguigu02/src/com/ykl/bean/Emp.java
Normal file
38
Spring/Spring5/code/shangguigu02/src/com/ykl/bean/Emp.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.bean;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Emp, v 0.1 2022-10-08 15:34 yinkanglong Exp $
|
||||
*/
|
||||
public class Emp {
|
||||
private String ename;
|
||||
private String gender;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Emp{" +
|
||||
"ename='" + ename + '\'' +
|
||||
", gender='" + gender + '\'' +
|
||||
", dept=" + dept +
|
||||
'}';
|
||||
}
|
||||
|
||||
public void setDept(Dept dept) {
|
||||
this.dept = dept;
|
||||
}
|
||||
|
||||
private Dept dept;
|
||||
|
||||
public void setEname(String ename) {
|
||||
this.ename = ename;
|
||||
}
|
||||
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserDao, v 0.1 2022-10-08 15:15 yinkanglong Exp $
|
||||
*/
|
||||
public interface UserDao {
|
||||
public void update();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserDaoImpl, v 0.1 2022-10-08 15:15 yinkanglong Exp $
|
||||
*/
|
||||
public class UserDaoImpl implements UserDao{
|
||||
|
||||
@Override
|
||||
public void update(){
|
||||
System.out.println("dao update ...");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.service;
|
||||
|
||||
import com.ykl.dao.UserDao;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserService, v 0.1 2022-10-08 15:14 yinkanglong Exp $
|
||||
*/
|
||||
public class UserService {
|
||||
|
||||
public UserDao getUserDao() {
|
||||
return userDao;
|
||||
}
|
||||
|
||||
public void setUserDao(UserDao userDao) {
|
||||
this.userDao = userDao;
|
||||
}
|
||||
|
||||
UserDao userDao;
|
||||
|
||||
|
||||
public void add(){
|
||||
System.out.println("service add ...");
|
||||
|
||||
userDao.update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.testdemo;
|
||||
|
||||
import com.ykl.Book;
|
||||
import com.ykl.Order;
|
||||
import com.ykl.User;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Test01, v 0.1 2022-10-08 11:27 yinkanglong Exp $
|
||||
*/
|
||||
public class Test01 {
|
||||
@Test
|
||||
public void testAdd(){
|
||||
// 加载spring的配置文件
|
||||
// ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
BeanFactory context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
|
||||
// 获取配置创建的对象
|
||||
User user = context.getBean("user", User.class);
|
||||
System.out.println(user);
|
||||
user.add();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBook(){
|
||||
|
||||
// 在创建对象的过程中就完成了属性的注入
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
|
||||
Book book = context.getBean("book", Book.class);
|
||||
|
||||
System.out.println(book.getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrder(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
|
||||
|
||||
Order order = context.getBean("order", Order.class);
|
||||
|
||||
order.test();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.testdemo;
|
||||
|
||||
import com.ykl.User;
|
||||
import com.ykl.service.UserService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Tst02, v 0.1 2022-10-08 15:23 yinkanglong Exp $
|
||||
*/
|
||||
public class Test02 {
|
||||
@Test
|
||||
public void testAdd(){
|
||||
// 加载spring的配置文件
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");
|
||||
|
||||
// 获取配置创建的对象
|
||||
UserService user = context.getBean("userService", UserService.class);
|
||||
user.add();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.testdemo;
|
||||
|
||||
import com.ykl.bean.Emp;
|
||||
import com.ykl.service.UserService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Test03, v 0.1 2022-10-08 15:45 yinkanglong Exp $
|
||||
*/
|
||||
public class Test03 {
|
||||
@Test
|
||||
public void testAdd(){
|
||||
// 加载spring的配置文件
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean03.xml");
|
||||
|
||||
// 获取配置创建的对象
|
||||
Emp emp = context.getBean("emp", Emp.class);
|
||||
System.out.println(emp);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user