spring的值注入与组件扫描

本文详细介绍了Spring框架的基本配置方法,包括依赖注入、组件扫描、作用域管理、生命周期回调、注解使用等核心概念。同时,深入探讨了不同类型的值注入方式,如基本类型、集合类型、引用方式及通过读取properties文件和使用Spring表达式进行注入的技巧。

jar 包依赖

<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>
		<!-- 数据库连接池 -->
		<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<!--mysql  -->
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.19</version>
		</dependency>
	</dependencies>
</project>

注入基本类型的值

package basic;

public class Student {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
}

<?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 class="basic.Student" id="stu">
		<property name="name" value="梨花"/>
		<property name="age" value="17"/>
	</bean>
</beans>
package test;

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

import basic.Student;

public class TestCase {
	String configLocation="applicationContext.xml";
	@Test
	public void test() {
		ApplicationContext ac=new ClassPathXmlApplicationContext(configLocation);
		Student s=ac.getBean("stu",Student.class);
		System.out.println(s);
	}
}

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

注入集合类型的值

List,Set,Map,Properties

直接注入

package basic;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Student {
	private String name;
	private int age;
	private List<String> interest;
	private Set<String> city;
	private Map<String,Double> score;
	private Properties db;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public List<String> getInterest() {
		return interest;
	}
	public void setInterest(List<String> interest) {
		this.interest = interest;
	}
	public Set<String> getCity() {
		return city;
	}
	public void setCity(Set<String> city) {
		this.city = city;
	}
	public Map<String, Double> getScore() {
		return score;
	}
	public void setScore(Map<String, Double> score) {
		this.score = score;
	}
	public Properties getDb() {
		return db;
	}
	public void setDb(Properties db) {
		this.db = db;
	}
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ",\n interest=" + interest + ",\n city=" + city + ", score="
				+ score + ", db=" + db + "]";
	}
	
}

<?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 class="basic.Student" id="stu">
		<property name="name" value="梨花" />
		<property name="age" value="17" />
		<property name="interest">
			<list>
				<value>读书</value>
				<value>写字</value>
				<value>画画</value>
			</list>
		</property>
		<property name="city">
			<set>
				<value>上海</value>
				<value>北京</value>
				<value>广州</value>
			</set>
		</property>
		<property name="score">
			<map>
				<entry key="语文" value="97" />
			</map>
		</property>
		<property name="db">
			<props>
				<prop key="username">root</prop>
				<prop key="password">root</prop>
			</props>
		</property>
	</bean>
</beans>
package test;

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

import basic.Student;

public class TestCase {
	String configLocation="applicationContext.xml";
	@Test
	public void test() {
		ApplicationContext ac=new ClassPathXmlApplicationContext(configLocation);
		Student s=ac.getBean("stu",Student.class);
		System.out.println(s);
	}
}

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

引用的方式注入

修改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">


	<!-- 引用的方式注入集合类型的值 -->
	<util:list id="listBean">
		<value>读书</value>
		<value>写字</value>
		<value>画画</value>
	</util:list>
	<util:set id="setBean">
		<value>上海</value>
		<value>北京</value>
		<value>广州</value>
	</util:set>
	<util:map id="mapBean">
		<entry key="语文" value="97" />
	</util:map>
	<util:properties id="propertiesBean">
		<prop key="username">root</prop>
		<prop key="password">root</prop>
	</util:properties>
	<bean class="basic.Student" id="stu">
		<property name="name" value="梨花" />
		<property name="age" value="17" />
		<property name="interest" ref="listBean" />
		<property name="city" ref="setBean" />
		<property name="score" ref="mapBean" />
		<property name="db" ref="propertiesBean"/>
	</bean>
</beans>

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

读取properties文件

config.properties

biz1.pagesize=10
<?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">

	<!-- 读取properties文件 --> 
		<!--
			容器依据location所指定的位置读取
			properties文件的内容,并将这些内容
			添加到Properties对象里面。 
			注:
				classpath:依据类路径去查找。
		 -->
		<util:properties id="config" 
		location="classpath:config.properties"/> 
	
</beans>
package test;

import java.util.Properties;

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

public class TestCase {
	String configLocation = "applicationContext.xml";

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Properties props = ac.getBean("config", Properties.class);
		System.out.println(props);
	}
}

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

Spring表达式

读取bean的属性值或者读取集合类型的值

####数据库连接参数设置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

ds.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">

	<!-- 读db.properties文件 -->
	<util:properties location="classpath:db.properties" id="db"/>
	<!-- 配置数据库连接池 -->
	<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="ds">
		<property name="driverClassName" value="#{db['jdbc.driver']}"/>
		<property name="url" value="#{db['jdbc.url']}"/>
		<property name="username" value="#{db['jdbc.username']}"/>
		<property name="password" value="#{db['jdbc.password']}"/>
	</bean>
</beans>
package test;


import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCase {
	String configLocation = "ds.xml";

	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		BasicDataSource ds = ac.getBean("ds", BasicDataSource.class);
		System.out.println(ds);
	}
}

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

使用注解简化配置文件

组件扫描

1)什么是组件扫描?
容器会扫描base-package指定的包及其子包
下面所有的类,如果该类前面有一些特定的注解
(比如 @Component),则纳入容器进行管理(相当于
在配置文件当中有一个对应的bean元素一样)。

步骤:

  1. 在配置文件当中,配置组件扫描
  2. 在类前面添加一些特定的注解

@Component 通用。
@Repository 持久层。
@Service 业务层。
@Controller 控制层。

@Component

<?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">

	<!-- 配置组件扫描 -->
		<!-- 
			容器会扫描base-package指定的包及其子包
			下面所有的类,如果该类前面有一些特定的注解
			(比如 @Component),则纳入容器进行管理(相当于
			在配置文件当中有一个对应的bean元素一样)。
		 -->
		 
	<context:component-scan base-package="basic"/>
</beans>
package basic;

import org.springframework.stereotype.Component;
//@Component默认的bean的id为类名的首字母小写
@Component
public class Student {
	public Student() {
		System.out.println("Student()");
	}
}

测试程序

package test;


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

import basic.Student;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Student stu = ac.getBean("student", Student.class);
		System.out.println(stu);
	}
}

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

@Scope

作用域

作用域介绍

package basic;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//@Component默认的bean的id为类名的首字母小写
@Component
@Scope("singleton")
public class Student {
	public Student() {
		System.out.println("Student()");
	}
}

测试程序

package test;


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

import basic.Student;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Student stu = ac.getBean("student", Student.class);
		Student stu2 = ac.getBean("student", Student.class);
		System.out.println(stu==stu2);
	}
}

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

package basic;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author 86182
 * @Component默认的bean的id为类名的首字母小写
 *@Scope:作用域模式,singleton只创建一个实例
 *prototype:创建多个实例
 */
@Component
@Scope("prototype")
public class Student {
	public Student() {
		System.out.println("Student()");
	}
}

测试程序

package test;


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

import basic.Student;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Student stu = ac.getBean("student", Student.class);
		Student stu2 = ac.getBean("student", Student.class);
		System.out.println(stu==stu2);
	}
}

测试结果

@Componet:定义spring的bean,默认id为带有@Component的注解的类的id为类名的首字母小写
也可以@Component(“stu”)自定义bean的id为stu

在这里插入图片描述

@Lazy

package basic;

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author 86182
 * @Component默认的bean的id为类名的首字母小写
 *@Scope:作用域模式,singleton只创建一个实例
 *prototype:创建多个实例
 *@Lazy:延迟加载
 */
@Component
@Scope("singleton")
@Lazy(true)
public class Student {
	public Student() {
		System.out.println("Student()");
	}
}

测试程序

package test;


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


public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		System.out.println(ac);
	}
}

测试结果

@Lazy为true,不会调用Student的构造方法,创建Student
当@Lazy为false并且作用域@Scope为singleton时,创建spring容器时才会调用Student的构造方法创建Student对象

在这里插入图片描述

@PostConstruct和@PreDestroy

package basic;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author 86182
 * @Component默认的bean的id为类名的首字母小写
 * @Scope:作用域模式,singleton只创建一个实例 ,prototype:创建多个实例
 * @Lazy:延迟加载
 */
@Component
@Scope("singleton")
@Lazy(true)
public class Student {
	public Student() {
		System.out.println("Student()");
	}

	/*
	 * @PostConstruct:初始化方法
	 */
	@PostConstruct
	public void init() {
		System.out.println("初始化方法init()");
	}

	/*
	 * @PreDestroy:销毁方法
	 */
	@PreDestroy
	public void destory() {
		System.out.println("销毁方法");
	}
}

测试程序

package test;


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

import basic.Student;


public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test(){
		AbstractApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Student stu=ac.getBean("student",Student.class);
		ac.close();
	}
}

测试结果

@PostConstruct修饰的方法会在构造函数之后
@PreDestroy修饰的方法会在容器被销毁时候调用,并且所在的类@Scope(“singleton”),才会调用

在这里插入图片描述

依赖注入

@Autowired @Qualifier

  • 支持set方法注入

第一种方式,直接在属性上添加

package basic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("rest")
public class Restaurant {
	/*
	 * spring利用java反射机制,找到id为wt的bean之后,
	 * 直接给属性赋值
	 */
	@Autowired
	@Qualifier("wt")
	private Waiter wt;
	public Restaurant() {
		System.out.println("Restaurant()");
	}
	public String toString() {
		return "Restaurant [wt=" + wt + "]";
	}
	
}
package basic;

import org.springframework.stereotype.Component;

@Component("wt")
public class Waiter {

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

测试程序

package test;

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

import basic.Restaurant;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Restaurant rest = ac.getBean("rest", Restaurant.class);
		System.out.println(rest);
	}
}

测试结果
在这里插入图片描述
第二种方式,在注入属性的setter方法上添加@Autowired,方法的参数前面添加@Qualifier(指定被注入的bean的id)

package basic;

import org.springframework.stereotype.Component;

@Component("wt")
public class Waiter {

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

package basic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("rest")
public class Restaurant {
	private Waiter wt;
	public Restaurant() {
		System.out.println("Restaurant()");
	}
	@Autowired
	public void setWt(@Qualifier("wt") Waiter wt) {
		this.wt = wt;
	}

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

测试程序和第一种一样,结果输出也一样

支持构造器注入

@Autowired放在构造器上,@Qualifier(“wt”)放在被注入的参数前面

package basic;

import org.springframework.stereotype.Component;

@Component("wt")
public class Waiter {

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

package basic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("rest")
public class Restaurant {
	private Waiter wt;
	@Autowired
	public Restaurant(@Qualifier("wt")Waiter wt) {
		System.out.println("Restaurant()");
		this.wt=wt;
	}
	public String toString() {
		return "Restaurant [wt=" + wt + "]";
	}
	
}

测试程序

package test;

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

import basic.Restaurant;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Restaurant rest = ac.getBean("rest", Restaurant.class);
		System.out.println(rest);
	}
}

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

@Resource

放在字段上,这样就不用写setter方法
可以放在setter方法上

放在字段上

package basic;

import org.springframework.stereotype.Component;

@Component("wt")
public class Waiter {

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

package basic;


import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component("rest")
public class Restaurant {
	/*
	 * @Resource(name = "wt"),
	 * name为被指定注入的bean的id,
	 * 如果没有使用name属性,则先按照byName方式茶轴,
	 * 找不到,则尝试byType
	 */
	@Resource(name = "wt")
	private Waiter wt;
	public String toString() {
		return "Restaurant [wt=" + wt + "]";
	}
	
}

测试程序

package test;

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

import basic.Restaurant;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Restaurant rest = ac.getBean("rest", Restaurant.class);
		System.out.println(rest);
	}
}

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

放在setter方法上

package basic;

import org.springframework.stereotype.Component;

@Component("wt")
public class Waiter {

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

package basic;


import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component("rest")
public class Restaurant {
	
	private Waiter wt;
	@Resource(name = "wt")
	public void setWt(Waiter wt) {
		this.wt = wt;
	}

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

测试程序

package test;

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

import basic.Restaurant;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Restaurant rest = ac.getBean("rest", Restaurant.class);
		System.out.println(rest);
	}
}

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

@Value

config.properties

biz.pageSize=10
<?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">


	<!-- 配置组件扫描 -->
		<!-- 
			容器会扫描base-package指定的包及其子包
			下面所有的类,如果该类前面有一些特定的注解
			(比如 @Component),则纳入容器进行管理(相当于
			在配置文件当中有一个对应的bean元素一样)。
		 -->
	<context:component-scan base-package="basic"/>
	<util:properties id="config" 
		location="classpath:config.properties"/>
</beans>
package basic;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class Student {
	/*
	 * @Value可以注入基本类型的值和spring表达式的值,
	 * 也可以将该注解添加到set方法前面
	 */
	@Value("梨花")
	private String name;
	@Value("#{config['biz.pageSize']}")
	private int pageSize;
	public String toString() {
		return "Student [name=" + name + ", pageSize=" + pageSize + "]";
	}
	
}

测试程序

package test;

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

import basic.Student;

public class TestCase {
	String configLocation = "annotation.xml";

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext(configLocation);
		Student stu = ac.getBean("student", Student.class);
		System.out.println(stu);
	}
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值