注解
注解
概述
本文主要讲述一些Spring常用的注解
实践
先看代码 还是原来的背景:一人一猫一狗
猫类
package kuangstudy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* 功能描述
*
* @since 2022-06-26
*/
@Component
@Scope("prototype")
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
狗类
package kuangstudy;
import org.springframework.stereotype.Component;
/**
* 功能描述
*
* @since 2022-06-26
*/
@Component
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
人类
package kuangstudy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* 功能描述
*
* @since 2022-06-26
*/
@Component
@Scope("prototype")
public class Person {
@Value("xiaohong")
private String name;
@Autowired(required = false)
private Cat cat;
//
@Autowired
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
}
测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import kuangstudy.Person;
/**
* 功能描述
*
* @since 2022-06-28
*/
public class Test1 {
@Test
public void test01() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person person = applicationContext.getBean("person", Person.class);
person.getCat().shout();
person.getDog().shout();
Person person1 = applicationContext.getBean("person", Person.class);
System.out.println(person1.equals(person));
System.out.println(person1.getCat().equals(person.getCat()));
System.out.println(person1.getDog().equals(person.getDog()));
}
}
配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="kuangstudy"></context:component-scan>
</beans>
结果
miao~
wang~
false
false
true
重点分析
- 从Spring4之后使用注解配置bean需要导入aop包
- 配置文件中需要配置扫描包路径
<context:component-scan base-package="kuangstudy"></context:component-scan>
- @Value注解注入属性,放在属性上面或者属性的set方法上面,相当于xml中的
<property>
配置 - 衍生的注解, 按照mvc三层架构来分
@Component
@Repository dao层的注解
@Service service层的注解
@Controller controller层的注解 - 自动装配的注解
@Autowired
@Qualifier
@Resource - 作用域 @Scope 可以设置bean的作用域为单例或者原型 跟xml中scope作用相同
小结
xml与注解
xml更加万能,适用于任何场合!维护简单方便
注解更加简洁,需要理解
最佳实践:使用xml来管理bean,比较纯粹,使用注解注入属性