Finology 大数据金融

通过大数据以量化金融

在 Java 程序中,我们可以通过如下代码获取本地的 ip 地址:

1
InetAddress.getLocalHost().getHostAddress()

但由于安装了虚拟机,或者由于本地回环网的问题,获取的 ip 地址可能不是想要的那个。可以采用如下方法获取到所有的 ip 地址列表,再做筛选。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package gy.finolo.ipdemo;

import java.net.*;
import java.util.Enumeration;

public class IPDemo {

public static void main(String[] args) throws UnknownHostException, SocketException {

System.out.println(InetAddress.getLocalHost().getHostAddress());

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();

if (inetAddress instanceof Inet4Address) {

System.out.println(inetAddress.getHostAddress() + " loopback: " + inetAddress.isLoopbackAddress() +
" linklocal: " + inetAddress.isLinkLocalAddress() +
" sitelocal: " + inetAddress.isSiteLocalAddress());
}

if (inetAddress instanceof Inet6Address) {
System.out.println(((Inet6Address) inetAddress).getScopedInterface());
}
}
}
}
}

如果还不能区分,那就可能需要通过网卡名字来过滤了,这个是需要用到 Inet6Address 了。

我们使用 @Autowired 注解来注入依赖,下面通过代码,来简单演示一下其最基础的注入逻辑。

这里我将创建一个新的注解 @MyAutowired

MyAutowired.java

1
2
3
4
5
6
7
8
9
10
package gy.finolo.autowireddemo;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
@Documented
public @interface MyAutowired {
}

一个 Service 实现类,里面没有任何方法,主要用于测试。

UserService.java

1
2
3
4
5
package gy.finolo.autowireddemo;

public class UserService {
}

一个 Controller 类,我们把上面的 Service 类注入此类中。测试 main 方法也在这里面,方便测试。

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package gy.finolo.autowireddemo;


import java.util.stream.Stream;

public class UserController {

@MyAutowired
private UserService userService;

public static void main(String[] args) {

UserController userController = new UserController();
Class<? extends UserController> clazz = userController.getClass();
Stream.of(clazz.getDeclaredFields()).forEach(field -> {
// check the member variable whether annotated with @MyAutowired
MyAutowired annotation = field.getAnnotation(MyAutowired.class);
if (annotation != null) {
field.setAccessible(true);
// get the class type of the field
Class<?> type = field.getType();
try {
Object o = type.newInstance();
field.set(userController, o);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}
});

System.out.println(userController.userService);
}
}

运行后,可以看到非 null 的输出,证明已经成功把 UserService 依赖注入。

Springboot 版本 2.1.5.RELEASE

引用的 spring-boot-starter-test 版本为:2.1.2.RELEASE

依赖的 junit 版本为 4.12

必须要按如下写注解,才可以把类注入成功。

1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootApplication.class)
public class ControllerTests {

@Autowired
private UserController userController;

@Test
public void test() {
System.out.println(userController);
}
}
  • 需要写 @RunWith 注解
  • @SpringBootTest 注解需要添加 classes 属性,值为启动类的 class 对象。
  • test() 方法必须为 public

上述几点在新版本的 Springboot 中(比如2.2及以上)可能有变化。

0%