注解的使用
A:@Resource使用规则:
1.在spring的xml配置文件中导入命名空间及规范
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2.在spring的xml配置文件中引入注解解析器。
<context:annotation-config></context:annotation-config>
3.在spring的xml配置文件中把bean引入进来。
4.在一个类的属性上加
@Resource(name=“student_annotation”)
public Student student;
从该注解本身
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
String name() default “”;
}
可以看出:
1.该注解可以用于属性或者方法上,但一般用于属性上。
2.该注解的作用范围是runtime。
3.该注解有一个属性name,默认值为“”。
有点要注意@Resource注解只能作用于引用类型的属性上。
B:加入了注解之后的整个执行过程:
1.当启动spring容器的时候,spring容器加载了配置文件。
2.在spring配置文件中,只要遇到bean的配置,就会为该bean创建对象。
3.在纳入spring容器的范围内查找所有的bean,看哪些bean的属性或者方法上加有@Resource。
4.找到@Resource注解之后,判断该注解的name属性是否为“”(name没有写)
如果没有写name属性,则会让属性的名称和spring容器中bean的Id值做匹配,匹配成功则赋值;如果匹配不成功,则按照类型匹配,如果匹配不成功,就报错。
如果写了name属性,则会按照name属性的值和spring的bean中ID进行匹配,匹配成功,则赋值,不成功则报错。
C:@Resource与@Autowired
@Resource与@Autowired都是做bean的注入时使用。其实@Resource并不是spring的注解,它是jdk扩展包javax.annotation.Resource中的,需要导入使用,但 spring支持该注解的使用。
1.@Autowired
@Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。
@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。
如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起使用 ,如下:
@Autowired
@Qualifier(“student_annotation”) 等价于 @Resource(name=“student_annotation”)
2.@Resource
@Resource默认按照ByName自动注入,由J2EE提供,需要导入包javax.annotation.Resource。 @Resource有两个重要的属性:name和type,而Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。 所以,如果使用name属性,则使用byName的自动注入策略,而使用type属性时则 使用byType自动注入策略。 如果既不制定name也不制定type属性,这时将通过反射机制使用byName自动注入策略。
A:@Component的使用规则
1.在spring的配置文件中导入命名空间及规范
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
2.引入类扫描及注解解析器
<context:component-scan base-package=“com.olymtech.spring.annotation.scan”></context:component-scan>
a:该注解解析器包含了两个功能:依赖注入和类扫描(包含了<context:annotation-config></context:annotation-config>)
b:base-package:在该包及子包中扫描
3.如果一个类或者接口上(一般都是类上),加了@Component注解,就会进行如下法则:
如果其value属性的值为“”
@Component
public class Person(){
}
等价于
(如果value值不写,默认的是类名首字母小写,其他不变)
如果其value属性的值不为“”
@Component(“p”)
public class Person(){
}
等价于
4.按照上面的@Resource法则再次进行操作。
2.3.注解与XML的区别
A:注解书写简单,但效率低(应为解析器要扫描类注解,完了再扫描属性注解),但比起网络的效率,它完全可以忽略不计(你服务器效率再高,我提交一个http请求, 咔,网断了,或网络阻塞,服务器效率再高有啥用?) 。
B:xml书写麻烦,但效率高(不存在扫描)
依赖注入
https://blog.youkuaiyun.com/wu1317581750/article/details/89927352
无配置注解
所谓不使用配置文件,其实是换了一种形式,不用xml文件,而是建了个java类来代替它。具体如下:
1.首先我们在com.ly.demo包下创建了ApplicationConfig.java类
package com.ly.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration//使用java类作配置文件时需要注解Configuration
//注:类名不可以命名为Configuration 否则会报错
public class ApplicationConfig{
//等价于<bean id="myPhone class="com.ly.demo.Phone"></bean>
@Bean(name="myPhone")
public Phone myPhone(){
return new Phone("mate9","Huawei","4000");
}
}
2.Phone类代码如下:
package com.ly.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("thePhone") //与<bean id="phone" class="com.ly.demo.Phone" ></bean> 等效
public class Phone {
@Value(value = "mate9")//与<property name="name" value="mate9" ></property>等效
private String name;
@Value(value = "Huawei")//与<property name="brand" value="Huawei" ></property>等效
private String brand;
@Value(value = "4000")//与<property name="price" value="4000" ></property>等效
private String price;
public Phone(){
}
public Phone(String name, String brand, String price) {
super();
this.name = name;
this.brand = brand;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
3.测试类Test.java代码如下:
package com.ly.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);//此处的方法与前文中使用的不同!
Phone phone = (Phone)ac.getBean("thePhone");
System.out.println(phone.getName());
}
}