spring-day01

spring_day02 Spring框架提供解决方案,先导入Spring-web.jar包,提供类 立即下载

Spring是什么

开源的,用来简化企业级应用开发的应用开发框架。

注:

  1. 简化开发: Spring框架对常用的api都做了封装,比如
  2. 对jdbc就做了一套封装(Spring Jdbc),使用SpringJdbc访问数据库
  3. 就不再需要考虑如何获取连接与关闭连接等操作了
  4. 解耦: Spring帮我们管理对象之间的依赖关系(对象之间的耦合度降低了),这样软件的维护性得到提高
  5. 集成其它框架:Spring可以很好地和其它的一些框架进行集成,这样使用这些框架就更方便了

Spring容器

什么是Spring容器

Spring框架中的一个核心模块,用来管理对象。
注:
管理对象:对象的创建、销毁、初始化等等操作,以及
对象之间的依赖关系。

如何启动Spring容器

1. 导包

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.company</groupId>
	<artifactId>TestDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<!-- junit:单元测试 -->
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!--spring-webmvc -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>
	</dependencies>
</project>

2. 添加Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	
	
</beans>

3. 启动容器

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		// 启动Spring容器
		/*
		 * ApplicationContext:容器接口 ClassPathXmlApplicationContext:是实现了 上述接口的类。
		 * 注:依据类路径查找配置文件,然后启动Spring 容器。
		 */
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println(ac);
	}
}

上述程序执行之后,控制台输出
在这里插入图片描述
项目结构图
在这里插入图片描述

如何创建对象

方式一 无参构造器

  1. 为类添加无参构造器或者直接使用缺省构造器
package basic;

public class Apple {

}
  1. 在配置文件当中,添加bean元素
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 利用无参构造器创建对象 -->
	<!-- bean: 被容器管理的对象。 
	id:bean的名称,要求唯一。 class:要写的类的完整的名字。
	 注(了解): Spring容器利用java反射来查找类的字节码 
		文件,然后完成类的加载。 -->
	<bean class="basic.Apple" id="apple"></bean>
</beans>
  1. 启动容器,调用容器提供的一个方法(getBean)
package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		// 启动Spring容器
		/*
		 * ApplicationContext:容器接口 ClassPathXmlApplicationContext:是实现了 上述接口的类。
		 * 注:依据类路径查找配置文件,然后启动Spring 容器。
		 */
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//通过容器获得对象
		Apple apple=ac.getBean("apple",Apple.class);
		System.out.println(apple);
	}
}

方式二 静态工厂方法

1、在applicationContext.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 利用静态工厂方法创建对象 -->
	<!-- 
		factory-method:指定一个静态方法。
		注: 容器会调用该方法来创建对象。
		Calendar c = Calendar.getInstance();
	 -->
	<bean id="cal" class="java.util.Calendar" 
	factory-method="getInstance"/>
</beans>

2、测试程序

package basic;

import java.util.Calendar;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Calendar cal=ac.getBean("cal",Calendar.class);
		System.out.println(cal.get(Calendar.YEAR));
	}
}

3、测试结果
在这里插入图片描述

方式三实例工厂方法

1、applicationContext.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="cal" class="java.util.Calendar" 
	factory-method="getInstance"/>
	<!-- 利用实例工厂方法来创建对象 -->
	<!-- 
		factory-bean:指定一个bean的id。
		容器会调用该bean的实例方法来创建对象。
	 -->
	<bean id="time" 
	factory-bean="cal" 
	factory-method="getTime"/>
	
</beans>

2、测试程序

package basic;

import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Date date=ac.getBean("time",Date.class);
		System.out.println(date.getTime());
	}
}

3、测试结果
在这里插入图片描述

作用域

  1. 默认情况下,对于某个bean元素,容器只会创建一个实例。
  2. 如果将作用域设置为"prototype",容器会创建多个实例
    applicationContext.xml添加如下代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 
			scope:用来设置作用域。缺省值是singleton(单例),
			如果值是prototype,则容器会创建多个实例。
		 -->
	<bean class="basic.Apple" id="apple"></bean>
	
</beans>

测试程序

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Apple apple1=ac.getBean("apple",Apple.class);
		Apple apple2=ac.getBean("apple",Apple.class);
		System.out.println(apple1==apple2);
	}
}

测试结果
在这里插入图片描述
修改作用域为prototype
在这里插入图片描述
测试结果为false
在这里插入图片描述

生命周期相关的两个方法

  • 初始化
  • 销毁
    applicationContext.xml添加如下代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 
			init-method:指定初始化方法名。
			destroy-method:指定销毁方法名。
			注意:只有作用域为singleton时,销毁方法
				才会执行。
		 -->
	<bean class="basic.Apple" id="apple" scope="singleton" init-method="init" destroy-method="destory"></bean>
	
</beans>
package basic;

public class Apple {
	public void init() {
		System.out.println("Apple()初始化");
	}
	public void destory() {
		System.out.println("Apple销毁");
	}
}

测试程序

package basic;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		/*
		 * ApplicationContext接口没有提供关闭
		 * 容器的方法(close方法),需要使用其子接口
		 * AbstractApplicationContext。
		 */
		AbstractApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Apple apple=ac.getBean("apple",Apple.class);
		System.out.println(apple);
		ac.close();
		
	}
}

执行结果
在这里插入图片描述

延迟加载

默认情况下
  • 容器启动之后,会将所有作用域为singleton的bean先创建好。
    applicationContext.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- 
		 	lazy-init:如果值为true,表示延迟加载。
		  -->
	<bean class="basic.Apple" id="apple"  lazy-init="true"/>
	<bean class="basic.ExampleBean" id="example"/>
</beans>
package basic;

public class Apple {
	public Apple() {
		System.out.println("Apple()");
	}
}

package basic;

public class ExampleBean {
	public ExampleBean() {
		System.out.println("ExampleBean()");
	}
}

测试程序

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
}

测试结果
在这里插入图片描述

容器启动之后,对于作用域为singleton的bean,不再创建
  • (当调用getBean方法时才会创建)

测试程序

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Apple apple=ac.getBean("apple",Apple.class);
	}
}

测试结果
在这里插入图片描述

IOC和DI

什么是IOC (Inversion Of Controll 控制反转)

对象之间的依赖关系由容器来建立。

什么是DI(Dependency Injection 依赖注入)

容器通过调用set方法或者构造器来建立对象之间的依赖关系。
注:
IOC是目标,DI是手段

在这里插入图片描述

依赖注入的两种方式

set方法注入

  1. 添加set方法
  2. 在配置文件当中,使用来配置set方法注入
  3. 启动容器,调用getBean方法
package basic;

public class A {
	private B b;

	/**
	 * 容器会调用该方法,将b作为参数传进来。
	 */
	public void setB(B b) {
		System.out.println("setB() " + b);
		this.b = b;
	}

	public A() {
		System.out.println("A()");
	}
	public void execute(){
		//调用B的f1方法
		b.f1();
	}
}
package basic;

public class B {
	public B() {
		System.out.println("B()");
	}

	public void f1() {
		System.out.println("B's f1()");
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- property: 配置set方法注入,
	其中 name属性指定调用的方法(会将name属性值的首字母大写,然后前面添加set来构造一个方法名),
	而ref属性 指定被注入的bean的id。 -->
	<bean id="a" class="basic.A">
		<property name="b" ref="b" />
	</bean>
	<bean id="b" class="basic.B" />
</beans>

测试程序

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		A a1 = ac.getBean("a",A.class);
		a1.execute();
	}
}

测试结果
在这里插入图片描述

构造器注入

  1. 添加相应的构造器
  2. 在配置文件当中,使用来配置构造器注入
  3. 启动容器,调用getBean方法
package basic;

public class Waiter {
   public Waiter() {
   	System.out.println("Waiter()");
   }
}

package basic;

public class Restaurant {
	private Waiter wt;

	/**
	 * 容器会调用构造器将Waiter对象传递进来。
	 */
	public Restaurant(Waiter wt) {
		System.out.println("Restaurant() " + wt);
		this.wt = wt;
	}
	public String toString() {
		return "Restaurant [wt=" + wt + "]";
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	
	<bean id="wt" class="basic.Waiter"/>
	<!-- 
			constructor-arg:配置构造器注入,index
			属性用来指定参数的位置。
		 -->
	<bean class="basic.Restaurant" id="restuarant">
		<constructor-arg index="0"  ref="wt"/>
	</bean>
</beans>

测试程序

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Restaurant rest = 
				ac.getBean("restuarant",Restaurant.class);
		System.out.println(rest);
	}
}

测试结果
在这里插入图片描述

自动装配

  • 容器默认情况下,是不会自动装配的
  • 可以让容器依据某些规则,自动建立对象之间的依赖关系(仍然需要调用set方法或者构造器)
  • 注:自动装配建议少用,如果要使用,建议使用byName。
package basic;

public class Waiter {
	public Waiter() {
		System.out.println("Waiter()");
	}
}

package basic;

public class Restaurant {
	private Waiter wt;
	
	public void setWt(Waiter wt) {
		this.wt = wt;
	}

	public String toString() {
		return "Restaurant [wt=" + wt + "]";
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	
	<bean id="wt" class="basic.Waiter"/>
	<!-- 
			autowire:配置自动装配。
			byName: 容器依据属性名去查找对应的bean(即
				以属性名作为bean的id),找到之后,调用
				对应的set方法。
				注:如果找不到对应的bean,不会注入。
			byType:容器依据属性类型去查找对应的bean(即
				查找类型与属性类型一致的bean),找到之后,
				调用对应的set方法。
				注:如果找到多个,会出错。
			constructor:类似于byType,找到之后,会调用
				构造器来注入。
		 -->
	<bean class="basic.Restaurant" id="restuarant" autowire="byType"/>
</beans>

测试程序

package basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpring {
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Restaurant rest = 
				ac.getBean("restuarant",Restaurant.class);
		System.out.println(rest);
	}
}

测试结果
在这里插入图片描述

SpringSecurity.pdf 立即下载

相关推荐

NVMe与PCIe的完美结合:高速存储背后的技术解析

本文深入解析了NVMe与PCIe技术如何完美结合,实现高速存储性能的飞跃。从机械硬盘到NVMe SSD的进化历程出发,详细介绍了PCIe总线的高带宽优势与NVMe协议的低延迟设计,以及两者协同工作带来的显著性能提升。通过实测数据对比,展示了NVMe SSD在消费级和企业级应用中的卓越表现,并提供了选购与优化建议。

weixin_29216353的博客 376

spring security——学习笔记(day01)-简介+入门案例

SpringSecurity的个人学习笔记,注意:以下案例均使用IDEA开发工具,均使用springboot框架,版本为2.6.1 ,请注意不同版本之间的使用方法不同,若有补充和建议欢迎评论区留言♪(^∇^*)。 1.SpringSecurity简介 1.1概要 SpringSecurity 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。其核心功能主要包括: 认证 (你是谁) 授权 (你能干什么) 攻击防护 (防止伪造身份)

键上艺术家 1099

CloudSat卫星数据高效下载与自动化处理指南

本文详细介绍了CloudSat卫星数据的高效下载与自动化处理方法,包括交互式图形界面和wget批量下载技巧,以及使用Python脚本实现自动化处理和数据质量控制。内容涵盖从数据准备到实战应用的完整流程,特别适合气象研究和气候建模领域的工作者提升工作效率。

weixin_28700411的博客 233

SpringSecurity超详细入门介绍

  权限管理是我们项目中必不可少的一环,实际项目中我们可以自己设计权限管理模块,也可以使用市面上成熟的权限管理框架,比如 shiro或者 SpringSecurity等,前面已经详细的介绍过了 shiro 的使用,本文开始就给大家详细的来介绍下SpringSecurity的使用。 内容包括 spring+springmvc基于配置的方式详细介绍SpringSecurity springboot整...

波波烤鸭的博客 2万+

30分钟搭建AI科研写作工作台:基于WorkBuddy的论文全流程加速实践

在人工智能技术日益普及的今天,如何利用智能工具提升专业领域工作效率成为关键课题。其核心原理在于通过模块化设计,将复杂的任务流程拆解为可复用、可编排的标准化技能单元,并结合精准的指令控制,实现从通用对话到深度任务执行的转变。这一技术价值在于显著降低重复性劳动强度,将人的创造力聚焦于核心决策与创新环节。在科研写作、数据分析、代码开发等场景中,这类智能工作台能有效辅助完成文献调研、内容生成、格式校验等繁琐步骤。本文以科研论文写作为具体应用场景,深入探讨如何利用WorkBuddy这一智能工作台,通过配置核心科研技能

weixin_29032489的博客 1494

DDR5内存调优实战:手把手教你搞定Write Leveling Training Mode(附时序图解析)

本文深入解析DDR5内存调优中的Write Leveling Training Mode技术,详细介绍了其核心原理、硬件准备、实战步骤及常见问题解决方案。通过JESD79-5标准定义的反馈机制,帮助硬件工程师精确对齐DQS与CK信号,提升系统稳定性和数据可靠性。附有时序图解析和代码示例,适合DDR5内存系统设计与调试参考。

weixin_29256771的博客 550

Java---接口

接口 (2)接口的特点: A:接口用关键字interface修饰 interface 接口名 {} B:类实现接口用implements修饰 class 类名 implements 接口名 {} C:接口不能实例化 D:接口的实现类 a:是一个抽象类。 b:是一个具体类,这个类必须重写接口中的所有抽象方法。 (3)接口的成员特点: A:成员变量 只能是常量 默认...

niushao的博客 127

STM32F103C8T6的PC13-15引脚配置避坑指南:为什么你的IO口不工作?

本文详细解析了STM32F103C8T6的PC13-15引脚作为普通IO口不工作的原因及解决方案。这三个引脚默认关联侵入检测和低速外部时钟等特殊功能,需先禁用TAMPER和LSE,并开启AFIO时钟,才能正确配置为普通GPIO。文章提供了标准外设库和HAL库的配置步骤、常见问题排查方法及硬件设计注意事项。

weixin_29169899的博客 522

Spring_day01问题记录

Spring_day01问题记录 1.MyBatis,Quartz? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object...

Json Yao的博客 323

基于STM32F103C8T6单片机设计CAN接口8路继电器控制板硬件(原理图+PCB)+软件源码

基于STM32F103C8T6单片机设计CAN接口8路继电器控制板硬件(原理图+PCB)+软件源码,仅供学习及设计参考。

GJZGRB的博客 1416

告别卡顿闪烁!在Cesium 1.134中集成SOG格式,让400万高斯秒级加载

本文详细介绍了如何在Cesium 1.134中集成SOG格式,实现400万高斯数据的秒级加载,解决传统加载方式导致的卡顿和闪烁问题。通过空间索引重构、属性量化编码和WebGPU原生支持等技术,SOG格式显著提升了三维地理空间可视化的性能。文章还提供了环境准备、模型转换和Cesium集成的实战指南,帮助开发者高效优化项目。

weixin_29313817的博客 498

SSM框架 - Spring_day01:课程介绍与学习总览

对于一门新技术,我们需要从为什么要学学什么以及怎么学这三个方向入手来学习。

tang7mj的博客 1026

YOLOv11 知识蒸馏完整实战:Teacher–Student 训练策略与性能飞跃

在self.alpha = alpha # 蒸馏损失权重self.temperature = temperature # 温度系数,控制分布平滑度# 输出分布蒸馏(KL散度损失)# 中间特征蒸馏(MSE损失,对齐中间层特征)# 总蒸馏损失知识蒸馏是YOLOv11在“小模型高性能部署”场景的“破局之法”——它让小模型能“站在大模型的肩膀上”,兼具速度与精度。从损失函数定义到多阶段训练,再到实战落地,本教程已为你铺就了一条清晰的路径。

m0_64661770的博客 141

Spring day01

在spring的配置文件中使用bean标签,配以id和class属性后,且没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。===================以上三个用于指定给构造函数中哪个参数赋值=========================================以上三个用于指定给构造函数中哪个参数赋值======================改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

Anyway_01的博客 766

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Spring学习Day01

概述 什么是Spring: Spring是分层的JavaSE/EE引用full-stack(全站式)轻量级开源框架,以IoC(控制反转)、AOP(面面向切面编程)为内核。 发展历程: 1997年,IBM提出了EJB的思想。 1998年,SUN制定了规范标准EJB1.0 2004年,Rod Johnson阐述了J2EE开发不使用EJB的解决方式(Spring雏形) 2017年,发布spring5....

风间琉璃の博客 269

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

spring_day01

1.spring的概念 spring 是一个轻量级的一站式框架 百度百科:Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框...

weixin_43071747的博客 232

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

spring day01

XML 和注解方式开发的区别 **xml方式:** 1)缺点: 相比注解配置起来比较麻烦 2) 优点: xml 配置的内容可以随时修改,改完之后不用重新编译,重新发布 **注解方式:** 1)缺点: 还是将配置写在Java类中 ,如果将来一旦发生变化,还需要修改java源文件,改完之后仍需要重新编译,重新发布. 2)优点: 配置起来比xml要简介很多./// 一. Spring简介 1. 什么是Spring Spring是一个用于简化企业级Java开

qq460898027_的博客 92

黑马 Spring_day01

> * 掌握Spring相关概念 > * 完成IOC/DI的入门案例编写 > * 掌握IOC的相关配置与使用 > * 掌握DI的相关配置与使用

weixin_56311692的博客 393

day01 spring

spring

Libra_97的博客 570

Spring的学习-day01

Spring的学习-day01 1.spring核心配置文件详解 set属性注入: <!-- 1.set属性注入 注意:(1)如果对象没有指定id,这个时候以类名作为id (2)set属性注入先调用无参构造器,再进行set方法调用 --> <bean class="cn.itheima.entity.Student" id="student"&...

qq_34222743的博客 199

Spring学习day01

一、前言 Spring的学习,我是通过哔哩哔哩网站上的视频,黑马程序员的,个人觉得很不错,推荐给大家,讲解思路清晰,内容充实,总共四天,让我能够全面的了解Spring的常用操作及应用。 链接如下:https://www.bilibili.com/video/av47952931?from=search&seid=4094573305286061275 二、Spring概述 2.1 spri...

qq_34504626的博客 1837

day01——Spring

Spring 一、框架的概念 框架(framework),框架是一个经过验证的具有公共功能的半成品软件。 二、Spring的概念 Spring是由Java语言开发的开源的、轻量的、非侵入式的、fullstack框架(SSH——Spring-struts-hibernate)/(SSM——Spring-Springmvc-MyBatis) 三、Spring的核心内容 1、IOC:Inversion Of Control,将对象的创建和管理交由Spring容器管理来完成。 四、Spring开发的基本步骤 4.

渺万里层云 253

Spring——day01

Spring——day01 Spring介绍;Spring相关概念;Spring-IOC;Spring-DI;IOC相关内容;DI相关内容

weixin_62993347的博客 303

Spring源码学习Day01

Java初级选手Spring源码学习打卡

Rookie2202的博客 861
上一篇: ssm框架的整合(一)
下一篇: spring的值注入与组件扫描
迷失的小鹿
博客等级 码龄8年 1万+粉丝 199原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值