一、注解法:
如果有成千上万个bean,难道要在配置文件中写上成千上万条bean标签吗?
答案是否定的,这就需要今天介绍的注解法来解决了。
注解法要求:
配置文件中
<context:component-scan
base-package="com.albb.*"></context:component-scan>
需要有这个扫描器,什么样的类才会被扫描到呢?下面举个例子介绍两种注解类的方法。
①默认的注解方法:
package com.albb.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component()
* ①这个默认的注解相当于在xml中写入了这句话 <bean id="类名首字母小写" class="包名.类名" ><bean/>
*/
public class Computer {
private String cpu;
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
@Override
public String toString() {
return "Computer [cpu=" + cpu + "]";
}
}
可以看到类名上面有一个@Component这个符号,这个就是说明该类是个注解类,@Component()这个括号没东西,这就是默认的注解方式,代表的含义是这个默认的注解相当于在xml中写入了这句话 <bean id="类名首字母小写" class="包名.类名" ><bean/>。默认的注解法id为该类的类名的首字母小写,那在这里id=“computer”。这在函数调用是要注意。
②非默认的注解方法
还是上面那个类,举例说明一下
package com.albb.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("com")/*②这样是非默认的,这样写相当于在xml中写入了这句话 <bean id="com" class="包名.类名"
* ><bean/>建议用非默认的。
*/
public class Computer {
private String cpu;
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
@Override
public String toString() {
return "Computer [cpu=" + cpu + "]";
}
}
非默认的注解法@Component(“com”),括号里有东西com,这样写相当于在xml中写入了这句话 <bean id="com" class="包名.类名"><bean/>建议用非默认的。这里id=“com”而不是computer。切记!!
二、既然可以用扫描注解类的方法实现springIoc的整合,那是不是需要很多个<context:component-scan base-package="com.albb.entity"></context:component-scan> 这样的扫描?当然不是啦,这就需要在建立类的时候命名的规范性了。
可以这样命名前面都是一样的,后面的不一样,这样在写扫描的时候那个 basepackage="com.albb.* "可以写成这样,这个含义是扫描所有以com.albb开头的包的注解类。
三、举例说明
①举个不相关的类的例子
给一个类中的各个属性赋值,其中属性不是其他实体类的对象!!!
package com.albb.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("com")/*②这样是非默认的,这样写相当于在xml中写入了这句话 <bean id="com" class="包名.类名"
* ><bean/>建议用非默认的。
*/
public class Computer {
@Value("高通")
private String cpu; /*
* 这个Value相当于<bean id="com" class="com.albb.controller.Computer"> <properity
* name="cpu" value="高通"><properity/><bean/> 这个只有一个属性cpu。
* 那如果有多个属性呢?还要一个一个写吗?
* 如果有多个属性的话,也可以在每一个属性前用value赋值
*
*/
@Value("hh")
private String a;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
@Override
public String toString() {
return "Computer [cpu=" + cpu + ", a=" + a + "]";
}
}
@Value()只能给属性赋值,不能给对象赋值,要想给对象赋值需要用@resource()。下面介绍一下@Resource().
②两个类相关,耦合性强
package com.albb.entity;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.albb.controller.Computer;
@Component("stu")
public class Student {
@Resource(name = "com")
/*
* 相当于<bean id="stu" class="com.albb.entity.Student"> <properity name="com"
* ref="com"><properity/><bean/>
*
*/
private Computer com;
@Value("lzl")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Computer getCom() {
return com;
}
public void setCom(Computer com) {
this.com = com;
}
@Override
public String toString() {
return "Student [com=" + com + ", name=" + name + "]";
}
}
这个com是computer类的一个对象,要想给他赋值只能用@Resource(),而name在student类中是一个普通的属性,则可以使用@Value(),也就是说可以混合使用,使用时每一个注解可以放在相对于的声明变量的前面,也可放在相对应的set方法的前面。注意一个注解对应一个变量。
这样的话由于@Value没有紧跟变量name,输出的name则为null。
这样每一个注解都紧跟其所对应的变量就不会产生null,name输出为lzl