注解是代码的特殊标记,可以简化xml配置,格式:@注解名称(属性名称=属性值),可以作用在类、方法、属性上
以下注解都可以创建bean实例
创建一个bean2.xml文件,其中component-scan是组件扫描,默认是全扫描,会扫描包中所有的注解并将它们加入到bean容器,可以扫描多个包(包用逗号隔开),此外,还可以通过use-default-filters=“false” 和context:include-filter 设置只扫描哪些内容或context:exclude-filter设置哪些内容不进行扫描
<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">
<context:component-scan base-package="springstudy1">context:component-scan>
<context:component-scan base-package="springstudy1">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Component"/>
context:component-scan>
<context:component-scan base-package="springstudy">
<context:exclude-filter type="assignable"
expression="springstudy.User"/>
context:component-scan>
<context:component-scan base-package="springstudy1">
<context:exclude-filter type="regex"
expression="springstudy.*"/>
context:component-scan>
beans>
注:上述代码中注解已经加入bean容器,之后通过exclude-filter排除注解没有用,context:include-filter和exclude-filter用法类似,type的属性值相同,不过多说明
创建User类,添加@Component注解,其中value可以省略,默认值是类名称,首字母小写
package springstudy1;
import org.springframework.stereotype.Component;
@Component(value = "user") //等价于在xml文件中添加
public class User {
public void add() {
System.out.println("add...");
}
}
创建Test类
package springstudy1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
User user = context.getBean("user", User.class);
user.add();
}
}
补充:通过@Bean注解创建对象
创建Card类,使用@Bean注解创建User对象,name默认是类名(首字母小写)
package springstudy1;
import org.springframework.context.annotation.Bean;
public class Card {
@Bean(name="test")
public User test() {
return new User();
}
}
修改Test类
package springstudy1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Card.class);
User user = (User) context.getBean("test");
user.add();
}
}
@Autowired根据属性类型选择bean,但有时容器中会有多个相同类型的bean,在这种情况下,注入不会成功,因为 Spring 不知道需要注入哪个 bean。这时需要通过@Qualifier根据名称选择注入哪个bean,也可以使用 @Primary 注解来简化这种情况
@Component
@Primary
class Car implements Vehicle {}
@Component
class Bike implements Vehicle {}
@Component
class Driver {
@Autowired
Vehicle vehicle;
}
@Component
class Biker {
@Autowired
@Qualifier("bike")
Vehicle vehicle;
}
上述代码中,Car注解了@Primary。因此,在Driver类中,Spring 注入了一个Car bean。在 Biker bean 中,vehicle 字段的值将是 Bike 对象,因为它注解了 @Qualifier 并且指定了bean 名称。
此外还可以在声明bean和注入bean时添加@Qualifier也可以起到过滤其他同类型bean的效果
@Component
@Primary
class Car implements Vehicle {}
@Component
@Qualifier
class Bike implements Vehicle {}
@Component
class Driver {
@Autowired
@Qualifier
Vehicle vehicle;
}
上述代码会注入Bike bean
@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字(id属性值),而type属性则解析为bean的类型(class属性值)。如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不制定name也不制定type属性,这时会尝试按名称查找,如果未找到,则按类型查找。
效率好低。。。