1、创建项目
2、写几个demo稍微对Spring的作用进行初步了解
1、写一个实体类
/**
* 实体类
* @author Xuan
* @date 2019/9/17 16:32
*/
public class Xuan {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”,还有些书籍翻译成为“控制反向”或者“控制倒置”。
4、不用Spring的IOC容器
/**
* @author Xuan
* @date 2019/9/17 16:33
*/
public class TestMain {
public static void main(String[] args) {
Xuan xuan = new Xuan();
xuan.setName("测试");
System.out.println(xuan.getName());
}
}
5、使用SpringIoc的容器
<bean id="xuan" class="Xuan">
<property name="name" value="小轩哥哥"/>
</bean>
6、取出存在IOC容器里面的具体bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Xuan
* @date 2019/9/17 16:33
*/
public class TestMain {
public static void main(String[] args) {
// Xuan xuan = new Xuan();
// xuan.setName("测试");
// System.out.println(xuan.getName());
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
Xuan xuan = (Xuan)applicationContext.getBean("xuan");
System.out.println(xuan.getName());
}
}
7、简单的注入过程解释
8、输出Spring的IOC容器里所有已经存在的bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Xuan
* @date 2019/9/17 16:33
*/
public class TestMain {
public static void main(String[] args) {
// Xuan xuan = new Xuan();
// xuan.setName("测试");
// System.out.println(xuan.getName());
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
// Xuan xuan = (Xuan)applicationContext.getBean("xuan");
// System.out.println(xuan.getName());
String [] str = applicationContext.getBeanDefinitionNames();
for (String x:str) {
System.out.println("注入的"+x);
}
}
}