我有以下豆:春天注入不工作
Bean.java
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Bean {
private String arg;
}
Service.java
import lombok.Getter;
import javax.inject.Inject;
public class Service {
@Inject @Getter
private Bean bean;
private String arg;
public Service(String arg) {
this.arg = arg;
}
}
這是我如何實例化那些事:
test-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.example.Bean">
<constructor-arg value="bean param"/>
</bean>
<bean class="com.example.Service">
<constructor-arg value="service param"/>
</bean>
</beans>
但是,當我創建上下文,看看裏面是什麼Service
例如:
ApplicationContext context = new ClassPathXmlApplicationContext("test-context.xml");
System.out.println(context.getBean(Bean.class));
System.out.println(context.getBean(Service.class).getBean());
第二System.out
給我空。
爲什麼Bean
實例沒有注入?