-
代码示例1: 如果是抽象类那么抽象类上得是Lookup注解,此时才能将resource注册成一个
Appconfig.java
@Configuration
@ComponentScan(basePackages= "com.luban")
public class AppConfig {
}
UserService.java
@Component
public abstract class UserService{
public void test(){
System.out.println("test");
}
}
Test.java
public class Test {
public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.test();
}
/* 打印Connected to the target VM, address: '127.0.0.1:53158', transport: 'socket'
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:833)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1334)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1153)
*/
如果此时修改UserService类时:
@Component
public abstract class UserService{
public void test(){
System.out.println("test");
}
@Lookup
public void a(){
System.out.println("a");
}
}
// 成功输出test
2.代码示例
UserServie.java
@Component
public class UserService{
@Autowired
private OrderService orderService;
public void test(){
System.out.println(orderService);
}
}
OrderService.java
@Component
@Scope("prototype")
public class OrderService {
}
Tset.java
public class Test {
public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.test();
}
/* 打印
com.luban.service.OrderService@3bb9a3ff
com.luban.service.OrderService@3bb9a3ff
com.luban.service.OrderService@3bb9a3ff
*/
有没有很奇怪这边打印三次的OrderService 是同一个,我们不再在OrderService类上面加了@@Scope("prototype")
其实这个问题很简单,只要我们想一下就能知道了,因为UserService 在spring容器中的对象是非懒加载的单例bean,所以对应的OrderService属性注入只能够注入一次,这一点而都不奇怪。
修改代码
UserService.java
@Component
public class UserService{
@Autowired
private OrderService orderService;
public void test(){
System.out.println(a());
}
@Lookup("orderService")
public OrderService a(){
return null;
}
}
OrderService.java
@Component
@Scope("prototype")
public class OrderService {
}
Test.java
public class Test {
public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.test();
}
/* 打印
com.luban.service.OrderService@123ef382
com.luban.service.OrderService@dbf57b3
com.luban.service.OrderService@384ad17b
*/