spring https://spring.io/

The loC Container https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans

Spring官方文档(中文版!!!) https://blog.csdn.net/li1376417539/article/details/104951358/








4.0.0
org.example
lesson0820_spring
1.0-SNAPSHOT
8
8
org.springframework
spring-context
5.3.18
package com.entity;
public class Student {
public Student(){
System.out.println("这是Student的构造方法...");
}
public void introduce(){
System.out.println("我是一个学生");
}
}
package com.factory;
import com.entity.Student;
/**
* 静态工厂
*/
public class MyStaticFactory {
/**
* 静态返回学生实例
* @return
*/
public static Student createInstance(){
System.out.println("调用静态工厂.....MyStaticFactory...createInstance");
return new Student();
}
}
package com.factory;
import com.entity.Student;
/**
* 实例工厂
*/
public class MyInstanceFactory {
/**
* 定义创建对象的实例方法
* @return
*/
public Student createInstance(){
return new Student();
}
}
package com.test;
import com.entity.Student;
import com.factory.MyInstanceFactory;
import com.factory.MyStaticFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//Student student = new Student();
//创建spring bean容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-1.xml");
// Student student = ac.getBean(Student.class);
//静态工厂创建的对象,通过id名字,获取bean对象
// Student s = (Student) ac.getBean("stu");
// s.introduce();
//实例工厂方式创建对象
Student s2 = (Student) ac.getBean("stu2");
s2.introduce();
}
}






package com.entity;
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
public Student(){
System.out.println("这是Student的构造方法...");
}
//自我介绍
public void introduce(){
System.out.println("我是一个学生");
}
//学生初始化
public void initStu(){
System.out.println("调用了学生的初始化方法");
}
//学生销毁
public void destroyStu(){
System.out.println("调用了学生的销毁方法");
}
}
package com.test;
import com.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
public static void main(String[] args) {
//创建spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-2.xml");
//根据id获取学生对象
Student s1 =(Student) ctx.getBean("stu");
System.out.println("s1:"+s1);
//根据类型获取学生对象
Student s2 = (Student) ctx.getBean(Student.class);
System.out.println("s2:"+s2);
//根据id和类型获取学生对象
Student s3 = ctx.getBean("stu",Student.class);
System.out.println("s3:"+s3);
//根据name属性获取学生对象
Student s4 = (Student) ctx.getBean("s1");
Student s5 = (Student) ctx.getBean("s2");
System.out.println("s4:"+s4);
System.out.println("s5:"+s5);
}
}



package com.singleton;
public class God {
public void createWorld(){
System.out.println("上帝创造了世界....");
}
}
package com.singleton;
public class Test {
public static void main(String[] args) {
God god = new God();
god.createWorld();
System.out.println(god);
God god2 = new God();
god2.createWorld();
System.out.println(god2);
}
}
package com.singleton;
public class God {
//3.定义静态私有对象
private static God god;
//1.构造私有化
private God(){
}
//2.定义静态工厂获取实例的方法,用于获取对象
public static God createInstance(){
if (god==null){
god = new God();
}
return god;
}
public void createWorld(){
System.out.println("上帝创造了世界....");
}
}
package com.singleton;
public class Test {
public static void main(String[] args) {
God god = God.createInstance();
god.createWorld();
System.out.println(god);
God god2 = God.createInstance();
god2.createWorld();
System.out.println(god2);
}
}








// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';