依赖:
<!-- 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+"\t名字: "+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是最受欢迎的轻量级的企业级Java应用程序开发框架;Spring框架的 核心特性可以用于开发任何Java应用程序,但是在JavaEE平台上构建Web应 用程序是需要扩展的。Spring框架的目标是使J2EE开发变得更容易使用,通 过启用基于POJO编程模型来促进良好的编程实践
Spring核心概念
Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。
DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。
AOP意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
概念:1.方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring的Advisor或拦截器实现。
2.切入点(Pointcut):指定一个通知将被引发的一系列连接点的集合。
3.连接点(Joinpoint):程序执行过程中明确的点,如方法的调用或特定的异常被抛出。
4.通知(Advice):在特定的连接点,AOP框架执行的动作。
5.目标对象(Target Object):包含连接点的对象,也被称作被通知或被代理对象。
6.AOP代理(AOP Proxy):AOP框架创建的对象,包含通知。在Spring中,AOP代理可以是JDK动态代理或CGLIB代理。
7.引入(Introduction):添加方法或字段到被通知的类。Spring允许引入新的接口到任何被通知的对象。
8.编织(Weaving):组装方面来创建一个被通知对象。
原文:https://www.cnblogs.com/dabrk/p/11731788.html