spring 完结

This commit is contained in:
法然
2022-10-14 23:39:59 +08:00
parent b91ab7307e
commit 24bc4f24fe
265 changed files with 7316 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
/**
* @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!");
}
}

View File

@@ -0,0 +1,54 @@
<?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="student" class="com.ykl.collectiontype.Student">
<property name="courses" >
<array>
<value>java</value>
<value>c</value>
</array>
</property>
<property name="list" >
<list>
<value>zhangsan</value>
<value>xiaosan</value>
</list>
</property>
<property name="maps">
<map>
<entry key="java" value="java"></entry>
<entry key="name" value="hel"></entry>
</map>
</property>
<property name="sets">
<set>
<value>msyql</value>
<value>redis</value>
</set>
</property>
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
<ref bean="course3"></ref>
</list>
</property>
</bean>
<!-- 创建多个course对象-->
<bean id="course1" class="com.ykl.collectiontype.Course">
<property name="cname" value="c1"></property>
</bean>
<bean id="course2" class="com.ykl.collectiontype.Course">
<property name="cname" value="c2"></property>
</bean>
<bean id="course3" class="com.ykl.collectiontype.Course">
<property name="cname" value="c3"></property>
</bean>
</beans>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="bookList">
<value>zhangsan</value>
<value>xiaosan</value>
</util:list>
<bean id="book" class="com.ykl.collectiontype.Book">
<property name="bookList" ref="bookList">
</property>
</bean>
</beans>

View File

@@ -0,0 +1,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="mybean" class="com.ykl.factorybean.MyBean"></bean>
</beans>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="book" class="com.ykl.collectiontype.Book" scope="prototype">
</bean>
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="order" class="com.ykl.bean.Order" scope="singleton" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="hello world"></property>
</bean>
<!-- 配置后置处理器当前配置文件中的所有bean都会添加后置处理器-->
<bean id="mybeanpost" class="com.ykl.bean.MyBeanPost"></bean>
</beans>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="emp" class="com.ykl.autowire.Emp" autowire="byName"></bean>
<bean id="dept" class="com.ykl.autowire.Dept">
<property name="dname" value="hello"></property>
</bean>
</beans>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/context"
xmlns:context="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/user"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--引入外部属性文件-->
<context:property-path path="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>

View File

@@ -0,0 +1,38 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.autowire;
/**
* @author yinkanglong
* @version : Detp, v 0.1 2022-10-09 10:55 yinkanglong Exp $
*/
public class Dept {
private String dname;
/**
* Getter method for property <tt>dname</tt>.
*
* @return property value of dname
*/
public String getDname() {
return dname;
}
/**
* Setter method for property <tt>counterType</tt>.
*
* @param dname value to be assigned to property dname
*/
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}

View File

@@ -0,0 +1,38 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.autowire;
/**
* @author yinkanglong
* @version : Emp, v 0.1 2022-10-09 10:55 yinkanglong Exp $
*/
public class Emp {
private Dept dept;
/**
* Getter method for property <tt>dept</tt>.
*
* @return property value of dept
*/
public Dept getDept() {
return dept;
}
/**
* Setter method for property <tt>counterType</tt>.
*
* @param dept value to be assigned to property dept
*/
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"dept=" + dept +
'}';
}
}

View File

@@ -0,0 +1,28 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
/**
* @author yinkanglong
* @version : MyBeanPost, v 0.1 2022-10-09 10:42 yinkanglong Exp $
*/
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后置处理器,前置任务");;
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后置处理器,后置任务");
return bean;
}
}

View File

@@ -0,0 +1,40 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.bean;
/**
* @author yinkanglong
* @version : order, v 0.1 2022-10-09 10:17 yinkanglong Exp $
*/
public class Order {
private String oname;
public Order(String oname) {
this.oname = oname;
}
public Order() {
System.out.println("第一步 执行无参构造函数创建bean实例");
}
public void setOname(String oname) {
this.oname = oname;
System.out.println("第二步 调用set方法设置属性值");
}
public String getOname() {
return oname;
}
public void initMethod(){
System.out.println("第三部 调用初始化方法");
}
public void destroyMethod(){
System.out.println("第五步 调用销毁方法");
}
}

View File

@@ -0,0 +1,27 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.collectiontype;
import java.util.List;
/**
* @author yinkanglong
* @version : Book, v 0.1 2022-10-08 16:29 yinkanglong Exp $
*/
public class Book {
private List<String > bookList;
public void setBookList(List<String> bookList) {
this.bookList = bookList;
}
@Override
public String toString() {
return "Book{" +
"bookList=" + bookList +
'}';
}
}

View File

@@ -0,0 +1,29 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.collectiontype;
/**
* @author yinkanglong
* @version : Course, v 0.1 2022-10-08 16:15 yinkanglong Exp $
*/
public class Course {
private String cname;
/**
* Setter method for property <tt>counterType</tt>.
*
* @param cname value to be assigned to property cname
*/
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}

View File

@@ -0,0 +1,61 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.collectiontype;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author yinkanglong
* @version : Student, v 0.1 2022-10-08 16:01 yinkanglong Exp $
*/
public class Student {
private String[] courses;
private List<String> list;
private Map<String,String> maps;
private Set<String> sets;
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public Student() {
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
@Override
public String toString() {
return "Student{" +
"courses=" + Arrays.toString(courses) +
", list=" + list +
", maps=" + maps +
", sets=" + sets +
", courseList=" + courseList +
'}';
}
}

View File

@@ -0,0 +1,33 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.factorybean;
import com.ykl.collectiontype.Course;
import org.hamcrest.Factory;
import org.springframework.beans.factory.FactoryBean;
/**
* @author yinkanglong
* @version : MyBean, v 0.1 2022-10-08 16:42 yinkanglong Exp $
*/
public class MyBean implements FactoryBean<Course> {
//定义返回bea
@Override
public Course getObject() throws Exception {
Course course = new Course();
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}

View File

@@ -0,0 +1,25 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.collectiontype.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test01, v 0.1 2022-10-08 16:09 yinkanglong Exp $
*/
public class Test01 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
Student student = context.getBean("student",Student.class);
System.out.println(student);
}
}

View File

@@ -0,0 +1,26 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.collectiontype.Book;
import com.ykl.collectiontype.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test01, v 0.1 2022-10-08 16:09 yinkanglong Exp $
*/
public class Test02 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");
Book book = context.getBean("book",Book.class);
System.out.println(book);
}
}

View File

@@ -0,0 +1,26 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.collectiontype.Book;
import com.ykl.collectiontype.Course;
import com.ykl.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test03, v 0.1 2022-10-09 09:39 yinkanglong Exp $
*/
public class Test03 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean03.xml");
Course myBean = context.getBean("mybean", Course.class);
System.out.println(myBean);
}
}

View File

@@ -0,0 +1,27 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.collectiontype.Book;
import com.ykl.collectiontype.Course;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test04, v 0.1 2022-10-09 10:01 yinkanglong Exp $
*/
public class Test04 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean04.xml");
Book book01 = context.getBean("book", Book.class);
System.out.println(book01.hashCode());
Book book02 = context.getBean("book",Book.class);
System.out.println(book02.hashCode());
}
}

View File

@@ -0,0 +1,25 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.bean.Order;
import com.ykl.collectiontype.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test05, v 0.1 2022-10-09 10:22 yinkanglong Exp $
*/
public class Test05 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean05.xml");
Order order = context.getBean("order", Order.class);
System.out.println("第四步 获取bean实例对象");
((ClassPathXmlApplicationContext)context).close();
}
}

View File

@@ -0,0 +1,24 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.ykl.autowire.Emp;
import com.ykl.bean.Order;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test05, v 0.1 2022-10-09 10:22 yinkanglong Exp $
*/
public class Test06 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean06.xml");
Emp emp = context.getBean("emp", Emp.class);
System.out.println(emp);
}
}

View File

@@ -0,0 +1,24 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.ykl.test;
import com.alibaba.druid.pool.DruidDataSource;
import com.ykl.autowire.Emp;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yinkanglong
* @version : Test07, v 0.1 2022-10-09 11:27 yinkanglong Exp $
*/
public class Test07 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean07.xml");
DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
System.out.println(dataSource);
}
}

View File

@@ -0,0 +1,4 @@
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/user
prop.username=root
prop.password=123456