我对@PostConstruct
Spring中的自动布线顺序和逻辑有疑问.例如,下面的演示代码我有一个主要的Spring Boot类:
@SpringBootApplication
public class Demo1Application {
@Autowired
BeanB beanb;
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
和2 @Service
定义:
@Service
public class BeanB {
@Autowired
private BeanA beana ;
@PostConstruct
public void init(){
System.out.println("beanb is called");
}
public void printMe(){
System.out.println("print me is called in Bean B");
}
}
@Service
public class BeanA {
@Autowired
private BeanB b;
@PostConstruct
public void init(){
System.out.println("bean a is called");
b.printMe();
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下输出:
豆a被称为
打印我在Bean B中调用
beanb被称为 …
我有一个HashMap
对象,我想使用转换为JsonNode
树com.fasterxml.jackson.databind.ObjectMapper
.最好的方法是什么?
我找到了以下代码但由于我不熟悉Jackson API,我想知道是否有更好的方法.
mapper.reader().readTree(mapper.writeValueAsString(hashmap))
Run Code Online (Sandbox Code Playgroud) JSON是否需要根元素,如xml情况.据我所知,这是一个有效的json字符串.
{
"email":[
{
"type":"home",
"name":"john.doe@gmail.com"
},
{
"type":"work",
"name":"jdoe@gmail.com"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要将json转换为xml,反之亦然.但是,虽然以上是一个有效的json,当我将其转换为xml时,它是无效的?我错过了什么或这是正常的吗?
当连接到具有Java客户端套接字的服务器时,我有两个不同的连接超时异常.
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我检查了文档但是因为SocketTimeoutException
它写了"表示在套接字读取或接受时发生了超时",但这不是我的情况.因为我在连接建立期间得到它.
这两个例外有什么区别?实际上我期待得到ConnectException
任何连接问题(防火墙,端口关闭等)
我正在开发一个 Web 服务主机应用程序,其中使用 cxf 和 spring boot。当我使用以下代码注册 cxf servlet 时,Web 服务端可以工作,我可以看到已发布的 wsdls。
但是,在设置 cxf servlet Spring boot Actuator 和 Rest 端点之后,它无法工作并返回 404。我该如何解决这个问题?
@Bean
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用嵌入式数据库运行spring boot应用程序.在bean初始化期间(由于某种原因?)我的表创建脚本被调用两次,第二次调用失败,并且"table already exists"错误.下面是我的代码,可能是什么问题.
@Configuration
public class AppConfig {
private static final Logger LOG = LoggerFactory.getLogger(AppConfig.class);
private static EmbeddedDatabase dataSource;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new DbPlaceholderConfigurer(dataSource());
}
@Bean
public static EmbeddedDatabase dataSource() {
if(dataSource == null) {
EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder();
databaseBuilder.addScript("classpath:schema.sql");
//databaseBuilder.setName("test");
databaseBuilder.setType(EmbeddedDatabaseType.H2);
EmbeddedDatabase build = databaseBuilder.build();
initPopulate(build);
dataSource = build;
}
return dataSource;
}
private static void initPopulate(EmbeddedDatabase embeddedDatabase) {
try {
Connection connection = embeddedDatabase.getConnection();
PreparedStatement prepareStatement;
prepareStatement = connection.prepareStatement("INSERT INTO Settings VALUES …
Run Code Online (Sandbox Code Playgroud) 在java中修复stringbuffer长度的最佳做法是什么?也就是说,如果固定值为10且stringbuffer保持ABCDEFGHIJ,当我们追加K将导致A被清除并且结果值将是BCDEFGHIJK.我正在考虑使用StringBuffer的reverse()和setLenght()方法组合但不知道它的性能如何达到100 K长度.
我有一个 java 1.6 应用程序,它使用批量插入使用 jdbc 驱动程序在 Oracle 数据库中插入记录。正如您所知,Statement 对象有一个名为executeBatch() 的方法,我们使用它来进行批量更新。它有一个 int 数组的返回类型,其中包含每个记录的执行结果。但如果出现错误,它也会抛出 BatchUpdateException,我们也可以从中获取结果 int 数组。我的问题是在什么错误情况下我应该期望 BatchUpdateException 以及何时我应该期望没有抛出异常,但对于某些记录我会失败。
注意:问题专门针对 Oracle JDBC。为了更清楚地说,我见过这样的情况:执行executeBatch()后,我没有得到BatchUpdateException,但某些插入语句失败了。我的问题是在什么情况下会发生这种情况?
这是Statement.executeBatch()方法的返回javadoc。根据这里的一般观点,当一个条目失败时,执行会抛出 BatchUpdateException,然后在这种情况下,我们可以预期返回数组中的某些条目会失败。
* @return an array of update counts, with one entry for each command in the
* batch. The elements are ordered according to the order in which
* the commands were added to the batch.
* <p>
* <ol>
* <li> If the value of an element is >=0, the corresponding command
* completed successfully and the value …
Run Code Online (Sandbox Code Playgroud) 如何删除的mustUnderstand从轴SOAP头属性client.even我不把它特别是,当我设置的肥皂头信息mustundertand和演员属性被自动添加到肥皂message.Does有人知道如何清理?我正在使用Axis2 1.4版本的wsdl2java来创建我的ws客户端.