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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.qiang.ba05"/>
<!--
<!-- 声明组件扫描器 (component-scan) ,组件就是Java对象
base-package:指定注解在你的项目中的包名
component-scan工作方式:spring会扫描遍历base-package指定的包,
把包中和子包中的所有类,找到类中的注解,按照注解的功能创建对象,或给属性赋值。
加入了component-scan标签,配置文件的变化:
1.加入一个新的约束文件spring-Context。xsd
2.给这个新的约束文件起个命名空间的名称
-->
<context:component-scan base-package="com.qiang.ba01"/>
<!-- 指定多个包的三种方式-->
<!-- 第一种方式:使用多次组件扫描器,指定不同的包-->
<context:component-scan base-package="com.qiang.ba01"/>
<context:component-scan base-package="com.qiang.ba02"/>
<!-- 第二种方式:使用分隔符(;或者,)分隔多个包名-->
<context:component-scan base-package="com.qiang.ba01;com.qiang.ba02"/>
<!-- 第三种方式:指定父包-->
<context:component-scan base-package="com.qiang"/>
-->
</beans>
两个类
package com.qiang.ba05;
import com.qiang.ba05.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Component:创建对象的,等同于<bean>的功能 属性: values 就是对象的名称,也就是bean的id值
* values的值是唯一的,创建的对象在整个spring容器中一个
* 位置:在类的上面
* @Component(value = "myStudent")
* <bean id"="mySudent" class = "com.qiang.ba01.Student"/>
* <p>
* spring中和@Component功能一致,创建的对象的注解还有
* 1.@Repository (用在持久层可的上面):放在dao 的实现类,表示创建的dao对象,到对象是能访问数据库的。
* 2.@service(用在业务层类的上面):放在service的实现类的上面,创建爱你service对象,service对象是做业务处理,可以有事务等功能
* 3.@controller(用在控制器的上面):放在控制器(处理器)类的上面,创建控制器对象的,控制器对象,能够接受用户提交的参数,显示请求的处理结果
*
* 以上三个注解使用语法和@Component一样的,都能创建对象,但是这个三个注解还有额外的功能
* @Repository @service @controller是个项目对象分层
*/
@Component(value = "myStudent")
public class Student {
/**
* @value:简单类型的属性赋值 属性:value是String类型的,表示简单类型的属性值
* 位置:1.在属性定义上面,无需set方法,推荐使用
* 2.在set方法的上面
*/
@Value("张飞")
private String name;
@Value("24")
private Integer age;
/**
* 引用类型
* @Autowired:spring框架提供的注解,实现引用类型的赋值
* spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName byType
*
* 属性:required,是一个boolean的,默认true
* required=true:表示引用类型赋值失败,程序报错,并终止执行
* required=false:引用类型如果赋值失败,程序正常执行,引用类型
*
* @Autowired :默认使用的是byType自动注入
*
* 位置:1.在属性的定义的上面,无需set方法,推荐使用
* 2.在set方法上面
*
* 如果要使用byName方式,需要做的是:
* 1.在属性上面加入@Autowired
* 2.在属性上面加入@Qualifier(value="bean的id"):表示使用指定的名称的备案王城赋值
*
*/
@Autowired
@Qualifier("mySchool")
private School school;
public Student() {
System.out.println("===student的无参数构造方法");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
package com.qiang.ba05;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("mySchool")
public class School {
@Value("南京大学")
private String name;
@Value("虎踞龙盘路")
private String address;
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}