几种注释都在文件中:
package com.yuan.ba01;
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;
import javax.annotation.Resource;
/**
* @Component:表示常见对象,对象放到的容器中。作用是<bean>
* 属性:value,表示对象名称,也就是bean的id属性值
* 位置:在累的上面,表示创建此类的对象。
*
* @Component(value = "myStudent") 等同于
* <bean id= "myStudent" class="com.yuan.ba01.Student"/>
*
*
*
*
*
*/
/*
* 1使用value 指定对象的名称
* 2@Component(value="myStudent")
* 3省略value
* */
//@Component("myStudent")
//没有提供自定义对象名称,使用框架的默认对象名称:类名首字母小写
@Component
//student
public class Student {
/*
简单类型属性赋值:@Value
@Value:简单类型属性赋值:
属性:1,在属性定义的上面,无需set方法,推荐使用
2.在set方法上面
*/
//使用外部属性文件中的数据,语法@Value(${"key"})
/*
引用类型:
@Autowired :spring框架提供的,给引用类型赋值的,使用自动注入原理。
支持byName,byType,默认是byType.
位置:1.在属性定义的上面,无需set方法,推荐使用
2.在set方法上面
byName自动注入:
1.@AutoWired:给给引用类型赋值
2.@Qualifer(value="bean的id"):从容器中找到指定名称的对象
把这个对象赋值给引用类型
*/
/*
引用关型
@Autowired:spring框架提供的,给引用类型赋值的,使用自动注入原理。
支持byName,byType。默认是by Type.
属性:required : boolean类型的属性,默认true
true: spring在启动的时候,创建容器对象时候,会检查引用类型是否赋值成功。如果赋值失败,终止程序执行,并报错。
false:引用类型赋值失败,程序正常执行,不报错。引用类型的值是nulL
位置:1)在属性定义的上面,无需set方法,推荐使用
2)在set方法的上面
byName自动注入:
1)@Autowired:给引用类型赋值
2)@QuaLifer(value="bean的id"):从容器中找到指定名称的对象,
把这个对象赋值给引用类型。
*/
/**
引用类型@Resource:
来自jdk中,给引用类型赋值的,支持byName, byType.
默认是byNamespring支持这个注解的使用。
位置:1)在属性定义的上面,无需set方法,推荐使用
2)在iset方法的上面
说明,使用jdk1.8带有@Resource注解,高于jdk1.8没有这个@Resource,需要加入一个依赖。
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
@Resource只使用byName赋值
使用注解属性name="bean的id"
*/
//@Value("李四")
// @Value("${myname}")
private String name;
// @Value("20")
// @Value("${myage}")
private int age;
// @Autowired(required = false)
// @Qualifier(value = "mySchool")
/*byName按名称注入*/
// @Resource
//默认使用byName自动注入
//先使用byName赋值,如果赋值失败,在使用byType
//本例使用byType赋值成功
//只是用byName自动注入
@Resource(name="mySchool")
private School school;
/* public void setSchool(School school) {
this.school = school;
}*/
public Student() {
System.out.println("Student无参数构造方法");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
@Value("李四")
public void setName(String name) {
this.name = name;
}
@Value("18")
public void setAge(int age) {
this.age = age;
}
}
package com.yuan.ba01;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("mySchool")
public class School {
private String name;
private String address;
public School() {
}
@Value("${sname}")
public void setName(String name) {
this.name = name;
}
@Value("${saddress}")
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
myconf.properties文件:
配置文件: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">
<!--当前文件是总的文件,目的是包含其他的多个配置文件,一般不声明bean
语法:
<import resource="classpath:其他文件的路径"/>
classpath:表示类路径,类文件所在的目录,spring通过类路径
加载配置文件。
-->
<!--<import resource="classpath:ba01/spring-school.xml"/>-->
<!--<import resource="classpath:ba01/spring-student.xml"/>-->
<!--包含关系的配置文件,可使用通配符(*:表示任意字符)
注意:总的文件名称,不能包含在通配符范围内(applicationContext.xml不能叫做
spring-spring-applicationContext.xml)
-->
<!--<import resource="classpath:ba09/spring-*.xml"/>-->
<!--
声明组件扫描器:使用注解必须加入这个语句
component-scan:翻译过来是组件扫描器,组件是java对象。
属性:base-package 注解在你的项目中的包名。
框架会扫描这个包和子包中的所有类,找类中的所有注解。
遇到注解后,按照注解表示的功能,去创建对象,给属性赋值。
-->
<context:component-scan base-package="com.yuan.ba01"/>
<!--扫描多个包的三种方式-->
<!--第一种,使用多次组件扫描器-->
<!--<context:component-scan base-package="com.yuan.ba01"/>-->
<!--<context:component-scan base-package="ba02"/>-->
<!--第二种,使用分隔符(:或,),指定多个包-->
<!--<context:component-scan base-package="com.yuan.ba01;ba02"/>-->
<!--第三种,指定父包,但这样会是扫描时间加长-->
<!--<context:component-scan base-package="com.yuan"/>-->
<!--注意:最好不要进行重复扫描,会浪费时间-->
<!--读数据配置文件-->
<!--<context:property-placeholder location="classpath:/myconf.properties"/>-->
<context:property-placeholder location="classpath:/myconf.properties" />
<!--需要Student类中有school的set方法-->
<!-- <bean id="shcool" class="com.yuan.ba01.School">
<property name="name" value="北京大学"/>
<property name="address" value="北京的海淀"/>
</bean>-->
</beans>
测试文件: