spring autowire注入失败
Posted On 2015年6月2日
autowire注入失败, 导致此错误的最主要原因,是因为new的操作符导致。
举个例子
//第一个类 public class Test { @Autowired private Testbean testbean; } //第二个类, 引用第一个类 public class AnotherTest{ public static main() { Test test = new Test(); // 此时, test中的testbean不会自动注入,因为使用了new操作符导致。 } }
注意: 大家可以注意到我们在@controller 中 的 @autowired 却可以正确注入。 这是因为@controller 被spring是自动加载的, 容器并不是以new的方式获取到controller。
好吧,解决此问题的三种方法。
1. 直接注入bean。 这时候比如我们可以将service 中需要autowired的成员 放入到 controller中。
2. Use @Configurable http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable 本人才浅,也无法看明白。 大家可以自己学习。 基于aop的方法。
3. 手动查找bean (从spring bean 工厂获取bean), 不使用new便是了。
http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null
此篇文章已被阅读4550 次
One Comment
关于 ad hoc wireless network 和wireless mesh network 的对比:
In wireless networking, Ad-Hoc is one of the modes of operation for an 802.11 radio. It happens at OSI layer 1, the physical layer, and it basically means that all devices can communicate directly to any other device that is within radio range. Normally, in Infrastructure mode, wireless devices can only communicate with a central Access Point or Router and that device is responsible for re-transmitting packets from one client device to another client device (even if they are right next to each other). Ad-Hoc networks get rid of the middle-man that is the AP, however they don’t have any inherent capability for multi-hop. That means, if device A can reach device B, and device B can reach device C, but A cannot reach C, then A and C cannot communicate because B will not re-transmit any packets.
Mesh Networking, also know as Mesh Routing happens at OSI layer 3, the network layer. Mesh Routing allows each device on a network (also called nodes) to act as a router and re-transmit packets on behalf of any other devices. Mesh Routing provide the multi-hop facility that Ad-Hoc mode lacks. By combining Ad-Hoc mode at layer 1 and Mesh Routing at layer 3 we can create wireless mesh networks purely between client devices without any need for centralized Access Points or Routers.
P2P or Peer-to-Peer simply means that clients talk directly to each other without the use of a central server. Both Ad-Hoc and Mesh Routing can be described as P2P as they are both instances of clients-to-client communication, just at different layers of the OSI model.