使用步骤:
1、加入maven依赖:spring-context(注解的使用必须加入spring-aop依赖,该依赖在引入spring-context时,已经被间接引入)
2、在类中加入spring注解(多个不同功能的注解)
3、在spring的配置文件中,加入一个组件扫描器的标签,来说明注解在项目中的位置。
注解创建Java对象
定义对象并添加注解
package com.phy.service.impl;
import com.phy.service.Animal;
import org.springframework.stereotype.Component;
@Component(value = "dog")
public class Dog implements Animal {
private String name;
private String sex;
private String age;
public Dog() {
}
public Dog(String name, String sex, String age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public void run() {
System.out.println("dog can run");
}
}
定义spring的配置文件 application-context.xml
声明组件扫描器,组件就是Java对象
<!--
关键字:component-scan 工作方式:spring会扫描遍历base-package指定的包,
把包和子包中所有的类,找到类中的注解,按照注解的功能创建对象或者赋值
属性:base-package :指定注解所在的包名
-->
<context:component-scan base-package="com" />
(了解就可以)当配置文件添加完context标签之后会:
1、加入一个新的约束文件spring-context.xsd
2、给这个新的约束起一个命名空间的别名
<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">
</beans>
编写测试类
此时会默认调用该对象的无参构造方法
//使用spring容器创建的对象
@Test
public void run1(){
//指定bean配置文件
String beanConfig = "beans.xml";
//创建ApplicationContext spring容器对象,通过它获取dog对象
//ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(beanConfig);
//从容器中获取对象,getBean("配置文件中的id");
Dog dog = (Dog) ac.getBean("dog");
dog.run();
}
对象赋值
简单类型赋值
@value(“name”)注解
public class Cat {
@Value("tom")
private String name;
private String age;
public String getName() {
return name;
}
public String getAge() {
return age;
}
@Value("20")
public void setAge(String age) {
this.age = age;
}
}
引用类型赋值
使用自动注入原理,支持byName、byType
@Autowired
功能:
引用类型赋值,默认使用byType
属性:
required:默认为Boolean类型的默认为true(true:假如引用类型赋值失败,程序报错并终止执行;false:假如赋值失败,引用类型赋值为空,程序继续执行)【建议使用true,这样可以提前暴露程序中的问题。】
位置:
1、用在属性名称上面,无需set方法
2、用在set方法上面
public class Animal {
@Autowired
private Dog dog;
private Mouse mouse;
//Autowired的byNmae方式
@Autowired
@Qualifier("name")
private Cat cat;
public Mouse getMouse() {
return mouse;
}
@Autowired
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
}
@Resource
功能:
引用类型赋值,JDK中的注解,使用自动注入的原理,默认使用byName
原则:先使用byName使用,如果找不到的话,会使用byType
位置:
1、用在属性名称上面,无需set方法
2、用在set方法上面
public class Animal {
@Resource
private Dog dog;
private Mouse mouse;
@Resource(name="cat")
private Cat cat;
public Mouse getMouse() {
return mouse;
}
@Resource
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
}