依赖:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency>
学生案例一:
package cn. spring. entity; 9/** * Bean */ public class StudentBean { //普通属性 private Integer stu_ id; private String stu_ name;| //方法 public void say(){ System. out. print1n("大家好,本人编号: "+stu_ id+" 名字: "+stu_ name); public Integer getStu_ id() { return stu_ _id; } public void setStu_ id(Integer stu_ id) { this.stu_ id = stu_ _id; }
public class SpringIOC { @Test public void iocTest(){ //步骤一:加载配置文件applicationContext. xmL ApplicationContext ctx=new ClassPathXm1ApplicationContext( configLocation: " applicationContext . xm1"); //步骤二:获取bean StudentBean bean=(StudentBean) ctx. getBean( s: "studentBean"); bean.say(); }
教师案例二:
< ?xml version="1. 0" encoding="UTF-8"?> <beans xmIns= "http://www. springfr amework . 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相当于当前bean唯一标识 class是bean的全路径 property注入bean中的属性 必须封装 --> <bean id="teacherBean class="cn. spring . entity.TeacherBean"> property name="t_ _id" value= " 250" ></ property> <property name="t_ name" value=" 刘磊" ></property> </bean> </beans>
* Bean public class StudentBean { //普通属性 private Integer stu_ id; private String stu_ name; //域属性 private TeacherBean teacherBean; public TeacherBean getTeacherBean() { return teacherBean ; , public void setTeacherBean(TeacherBean teacherBean) { this . teacherBean = teacherBean; |}
打印机案例三:
/* Ink 墨盒接口 */ public interface Ink { //获取墨盒颜色 public String getInk(); } / ** *彩色墨盒实现类 */ public class ColorInk implements Ink{ @Override public String getInk() { return "彩色"; } /** *黑白墨盒实现类 */ public class GrayInk implements Ink{ public String getInk() { return” 黑白"; } /** *纸张接口 / public interface Paper { //获取纸张大小 public String getPaper();| } /** * A4实现类. */ public class A4Paper implements Paper{ public String getPaper() { return "A4"; } } /** *B5实现类 */ public class B5Paper implements Paper{ public String getPaper(){ retuen "B5"; } } /** *打印机类 */ public class Printer { //纸张对象 private Paper paper; //墨盒对象 private Ink ink; //打印方法 public void print(){ System. out . print1n("您正在使用: "+paper . getPaper( )+"纸张和" } public Paper getPaper() { } <!--注入彩色墨盒 Bean的注入都是实现类--> <bean id="colorInk" class="cn. spring。print. ink. ColorInk"></bean> <bean id="grayInk" class="cn. spring. print . ink . GrayInk"></bean> <!--注入纸张--> <bean id="a4Paper" class="cn. spring . print.paper.A4Paper"></bean> bean id="b5Paper" class="cn. spring. print. paper. B5Paper"></bean> <!--打印机--> <bean id="printer" class="cn. spring. print. printer . Printer">| <!--将纸张和墨盒组合起来--> property name="paper" ref= "b5Paper"></property> <property name="ink" ref=" grayInk" ></ property> </bean> public class PrinterTest { public static void main(String[] args) { //步骤- :加载配置文件 ApplicationContext context=new ClassPathXmlApplicationContext( configLocation: " applicationContext . xml"); //调用bean Printer printer = (Printer) context. getBean( s: "printer"); printer . print();| }
Spring框架
Spring是最受欢迎的轻量级的企业级Java应用程序开发框架;Spring框架的 核心特性可以用于开发任何Java应用程序,但是在JavaEE平台上构建Web应 用程序是需要扩展的。Spring框架的目标是使J2EE开发变得更容易使用,通 过启用基于POJO编程模型来促进良好的编程实践
Spring核心概念
I控制反转IoC:
Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。
依赖注入DI:
DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。