完成setXxxx功能,即配置文件的 <property name="xxx" ref="xxx" /> 可省略, eg:
public class Boss {
//required = false 代表 xml 文件可以不配置 car bean,如不定义且没有 car bean,则跑出异常
@Autowired(required = false)
private Car car;
// @Qualifier("office2")代表当 xml 文件有多个 office 类型bean时,选择 id="office2" 的bean
//@Autowired
// public void setOffice(@Qualifier("office2")Office office) {
@Autowired
@Qualifier("office2")
private Office office;
@Override
public String toString() {
return "Car :" + car + "\noffice: " + office;
}
}
XML配置:
<bean id="boss" class="com.qtj.spring.annotations.Boss"/>
<bean id="office" class="com.qtj.spring.annotations.Office">
<property name="officeNo" value="001" />
</bean>
<bean id="office2" class="com.qtj.spring.annotations.Office">
<property name="officeNo" value="002" />
</bean>
<bean id="car" class="com.qtj.spring.annotations.Car" scope="singleton">
<property name="brand" value=" 红旗 CA72" />
<property name="price" value="2000" />
</bean>
@Resource
类似 @Autowired,只不过 Resource 是 byName的,且默认可以不需要 属性
public class Boss {
//定义类型
@Resource(type=Car.class)
private Car car;
// 多个同一类型bean时,指定特定 bean,相当于 @Autowried + @Qualifier("office1")
@Resource(name="office1")
private Office office;
public Car getCar() {
return car;
}
public Office getOffice() {
return office;
}
@Override
public String toString() {
return "car:" + car + "\n" + "office:" + office;
}
}
@PostConstruct, @PreDestroy
Bean在初始化后和销毁前做的工作
public class Boss {
@Resource
private Car car;
@Resource(name = "office")
private Office office;
public Car getCar() {
return car;
}
public Office getOffice() {
return office;
}
@Override
public String toString() {
return "car:" + car + "\n" + "office:" + office;
}
@PostConstruct
public void postConstruct1() {
System.out.println("postConstruct1");
}
//初始化后,功能同 <bean ... init-method="postContruct2" .. />
@PostConstruct
public void postConstruct2() {
System.out.println("postConstruct2");
}
//销毁前,功能同 <bean ... destroy-method="preContruct2" .. />
@PreDestroy
public void preDestroy1() {
System.out.println("preDestroy1");
}
}
Main 函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] locations = { "annoations5/beans.xml" };
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
// ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
ctx.destroy();
}
测试结果:
postConstruct1
postConstruct2
car:brand: 红旗 CA72, price: 2000.0
office:Office No: 002
preDestroy1
@Component
某个属性类不需要在 XML 中定义对应的 bean
@Component
public class Car {
private String brand;
private double price;
@Override
public String toString() {
return "brand: " + brand + ", price: " + price;
}
...
@Scope("prototype") @Component("boss") public class Boss { private Car car; @Resource(name="office2") private Office office; @Override public String toString() { return "Car :" + car + "\noffice: " + office; } ...
XML配置
<context:component-scan base-package="com.qtj.spring.annotations6">
<context:include-filter type="regex" expression="com\.qtj\.spring\.annotations6\..*"/>
<!-- need aspectj jar -->
<!-- context:exclude-filter type="aspectj" expression="com\.qtj\.spring\.annotations6\.util..*"/ -->
</context:component-scan>
<bean id="boss2" class="com.qtj.spring.annotations6.Boss"/>@Repository
、@Service
和 @Controller
在目前的 Spring 版本中,这 3 个注释和 @Component
是等效的
详见参考文档
spring2.5 context配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<!--
该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
该 BeanPostProcessor 将自动起作用,对标注 @Resource 的 Bean 进行自动注入
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
-->
</beans>