根据 【动力节点】最新Spring框架教程,全网首套Spring6教程,跟老杜从零学spring入门到高级 以及老杜的原版笔记 https://www.yuque.com/docs/share/866abad4-7106-45e7-afcd-245a733b073f?# 《Spring6》 进行整理, 文档密码:mg9b
Spring 相关文章整理汇总归纳于:https://www.yuque.com/u27599042/zuisie
/**
* ClassName: SpringBean
* Package: cw.sspring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:10
* @Version 1.0
*/
public class SpringBean {
}
<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="springBean" class="cw.spring.study.bean.SpringBean"/>
beans>
@org.junit.Test
public void test1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(applicationContext.getBean("springBean", SpringBean.class));
}
/**
* ClassName: Fruit
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:20
* @Version 1.0
*/
public abstract class Fruit {}
/**
* ClassName: Apple
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:20
* @Version 1.0
*/
public class Apple extends Fruit{}
/**
* ClassName: FruitFactory
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:21
* @Version 1.0
*/
public class FruitFactory {
// 创建水果对象的方法
public static Fruit getFruit() {
// 最终的Apple类对象是通过调用该工厂类的该方法new的对象
return new Apple();
}
}
<bean id="apple" class="cw.spring.study.bean.FruitFactory" factory-method="getFruit"/>
@org.junit.Test
public void test1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(applicationContext.getBean("apple", Fruit.class));
}
/**
* ClassName: Fruit
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:20
* @Version 1.0
*/
public abstract class Fruit {}
/**
* ClassName: Apple
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:20
* @Version 1.0
*/
public class Apple extends Fruit{}
/**
* ClassName: FruitFactory
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:21
* @Version 1.0
*/
public abstract class FruitFactory {
/**
* 水果工厂生产水果的方法
*
* @return 水果
*/
public abstract Fruit getFruit();
}
/**
* ClassName: AppleFactory
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:37
* @Version 1.0
*/
public class AppleFactory extends FruitFactory{
// 实例方法
@Override
public Fruit getFruit() {
// 生产苹果
return new Apple();
}
}
<bean id="appleFactory" class="cw.spring.study.bean.AppleFactory"/>
<bean id="apple" factory-bean="appleFactory" factory-method="getFruit"/>
@org.junit.Test
public void test1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(applicationContext.getBean("apple", Fruit.class));
}
/**
* ClassName: Person
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:56
* @Version 1.0
*/
public class Person {}
import org.springframework.beans.factory.FactoryBean;
/**
* ClassName: PersonFactoryBean
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-25 18:57
* @Version 1.0
*/
public class PersonFactoryBean implements FactoryBean<Person> {
// 创建bean的工厂方法
@Override
public Person getObject() throws Exception {
return new Person();
}
@Override
public Class<?> getObjectType() {
return null;
}
}
public interface FactoryBean<T> {
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
@Nullable
T getObject() throws Exception;
@Nullable
Class<?> getObjectType();
// 该方法接口进行了默认实现,工厂bean创建的对象是否为单例的
// 默认情况下,创建的对象是单例的
// 如果要让工厂bean创建的对象为多例,则重写该方法,返回false即可
default boolean isSingleton() {
return true;
}
}
<bean id="person" class="cw.spring.study.bean.PersonFactoryBean"/>
@org.junit.Test
public void test1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(applicationContext.getBean("person", Person.class));
}
/**
* ClassName: Student
* Package: cw.spring.study.bean
* Description:
*
* @Author tcw
* @Create 2023-05-26 9:04
* @Version 1.0
*/
public class Student {
private Date birth;
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student{" + "birth=" + birth + '}';
}
}
<bean id="student" class="cw.spring.study.bean.Student">
<property name="birth" value="Mon Oct 10 14:30:26 CST 2022"/>
bean>
@org.junit.Test
public void test2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(applicationContext.getBean("student", Student.class));
}
<bean id="studentBirth" class="java.util.Date"/>
/**
* ClassName: DateFactoryBean
* Package: cw.spring.study.bean
* Description:
* DateFactoryBean 这是一个工厂Bean,
* 用于协助Spring创建Date对象
*
* @Author tcw
* @Create 2023-05-26 9:13
* @Version 1.0
*/
public class DateFactoryBean implements FactoryBean<Date> {
private String dateStr; // 日期字符串(简单类型)
public void setDateStr(String dateStr) {
this.dateStr = dateStr;
}
@Override
public Date getObject() throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.parse(dateStr);
}
@Override
public Class<?> getObjectType() {
return null;
}
}
<bean id="date" class="cw.spring.study.bean.DateFactoryBean">
<property name="dateStr" value="2023-05-22"/>
bean>
<bean id="student" class="cw.spring.study.bean.Student">
<property name="birth" ref="date"/>
bean>