小编Syn*_*sso的帖子

避免Scala中的隐式def歧义

我试图创建从任何类型(比如Int)到String的隐式转换...

隐式转换为String意味着RichString方法(如反向)不可用.

implicit def intToString(i: Int) = String.valueOf(i)
100.toCharArray  // => Array[Char] = Array(1, 0, 0)
100.reverse // => error: value reverse is not a member of Int
100.length // => 3
Run Code Online (Sandbox Code Playgroud)

隐式转换为RichString意味着String方法(如toCharArray)不可用

implicit def intToRichString(i: Int) = new RichString(String.valueOf(i))
100.reverse // => "001"
100.toCharArray  // => error: value toCharArray is not a member of Int
100.length // => 3
Run Code Online (Sandbox Code Playgroud)

使用两个隐式转换意味着重复的方法(如长度)是不明确的.

implicit def intToString(i: Int) = String.valueOf(i)
implicit def intToRichString(i: Int) = new RichString(String.valueOf(i))
100.toCharArray  // => Array[Char] = Array(1, …
Run Code Online (Sandbox Code Playgroud)

string scala implicit ambiguous

15
推荐指数
3
解决办法
4341
查看次数

JMS ReplyTo - 它是如何工作的?

JMS API允许消息声明replyTo Destination实例.(即的超类Queue,Topic).然后,服务可以使用此队列向发送方发送回复消息.

Destination可以设置为ReplyTo值的限制是什么?这似乎不太可行,因为服务可能甚至没有任何到定义的网络路由Destination,因此无法返回任何消息.JMS是否以某种方式断言所提供的有效性(可达性)Destination?或者仅仅是服务尝试响应给定Destination并在必要时失败.

jms java-ee

14
推荐指数
1
解决办法
2万
查看次数

Spring JDBC测试的事务回滚

我在尝试使用Spring-test时没有成功地获得JDBC事务回滚.当我运行以下内容时,始终会提交SQL更新.

package my.dao.impl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations={"classpath:ApplicationContext-test-DAOs.xml"})
@TransactionConfiguration(defaultRollback = true)
public class ConfirmationMatchingDAOImplTest {

    @Autowired
    private DataSource dataSource;

    @Test
    public void shouldInsertSomething() throws Exception {
        final Connection connection = dataSource.getConnection();
        final Statement statement = connection.createStatement();
        statement.executeUpdate("insert into TEST_INSERT values (1, 'hello')");
        statement.close();
        connection.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    <property name="url" value="jdbc:sqlserver://makeitfunky:1490;databaseName=fonzie"/>
    <property name="username" value="ralph"/> …
Run Code Online (Sandbox Code Playgroud)

testing spring transactions jdbc rollback

13
推荐指数
1
解决办法
3万
查看次数

使用特定的类加载器加载Spring上下文

如何使用我自己的ClassLoader实例加载Spring上下文?

spring classloader

13
推荐指数
2
解决办法
2万
查看次数

为什么我不能flatMap试试?

特定

val strings = Set("Hi", "there", "friend")
def numberOfCharsDiv2(s: String) = scala.util.Try { 
  if (s.length % 2 == 0) s.length / 2 else throw new RuntimeException("grr") 
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能flatMap离开方法调用产生的Try?即

strings.flatMap(numberOfCharsDiv2)
<console>:10: error: type mismatch;
 found   : scala.util.Try[Int]
 required: scala.collection.GenTraversableOnce[?]
              strings.flatMap(numberOfCharsDiv2)
Run Code Online (Sandbox Code Playgroud)

要么

for {
  s <- strings
  n <- numberOfCharsDiv2(s)
} yield n
<console>:12: error: type mismatch;
 found   : scala.util.Try[Int]
 required: scala.collection.GenTraversableOnce[?]
            n <- numberOfCharsDiv2(s)
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用Option而不是Try,则没有问题.

def numberOfCharsDiv2(s: String) = if (s.length % 2 == 0) 
  Some(s.length / 2) else None
strings.flatMap(numberOfCharsDiv2) …
Run Code Online (Sandbox Code Playgroud)

scala

13
推荐指数
2
解决办法
3645
查看次数

如何使用Serde在序列化期间转换字段?

如何在序列化之前将转换应用于字段?

例如,如何确保字段latlon此结构定义在序列化之前舍入到最多6个小数位?

#[derive(Debug, Serialize)]
struct NodeLocation {
    #[serde(rename = "nodeId")]
    id: u32,
    lat: f32,
    lon: f32,
}
Run Code Online (Sandbox Code Playgroud)

rust serde

13
推荐指数
1
解决办法
1748
查看次数

在Java中通过引用传递原始数据

是否可以将原始数据作为参数传递给java中的参数,而不将它们包装到相应的类对象中?

java

12
推荐指数
2
解决办法
2万
查看次数

Hibernate:如何在Annotation中的一个连接表中连接三个3个表?

能否帮助我如何使用一个公共连接表连接三个表?我有USER,APPLICATION和ROLE表.我希望将他们的ID加入名为USER_APP_ROLE(user.ID,application.ID,role.ID)的表中.

当我删除Join Many to Many中的Application或Role表时,我的代码正在运行.

我做了以下代码:

User.java

@ManyToMany (targetEntity=Role.class)
@JoinTable(name="USER_APPLICATION_ROLE",
    joinColumns=@JoinColumn(name="USER_ID"),
    inverseJoinColumns=@JoinColumn(name="ROLE_ID"))

private Collection<Role> roles;

@ManyToMany (targetEntity=Application.class)
@JoinTable(name="USER_APPLICATION_ROLE",
    joinColumns=@JoinColumn(name="USER_ID"),
    inverseJoinColumns=@JoinColumn(name="APPLICATION_ID"))
private Collection<Application> applications;
Run Code Online (Sandbox Code Playgroud)

Role.java

@ManyToMany(mappedBy="roles", targetEntity=User.class)
private Collection<User> users = new ArrayList<User>();
Run Code Online (Sandbox Code Playgroud)

Application.java

@ManyToMany(mappedBy="applications", targetEntity=User.class)
private Collection<User> users = new ArrayList<User>();
Run Code Online (Sandbox Code Playgroud)

当我尝试运行以下测试时:

    user.getRoles().add(role1);
    user.getRoles().add(role2);

    role1.getUsers().add(user);
    role1.getUsers().add(user);

    role2.getUsers().add(user);
    role2.getUsers().add(user);

    user.getApplications().add(app1);
    user.getApplications().add(app2);

    app1.getUsers().add(user);
    app2.getUsers().add(user);
      ......

      session.beginTransaction();
      session.save(user);
      session.save(role1);
      session.save(role2);
      session.save(app1);
      session.save(app2);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Hibernate: select seq_cm_user.nextval from dual
Hibernate: select seq_role.nextval from dual
Hibernate: select seq_role.nextval from dual
Hibernate: select seq_application.nextval from dual …
Run Code Online (Sandbox Code Playgroud)

many-to-many hibernate jointable

12
推荐指数
1
解决办法
2万
查看次数

xargs jar tvf - 不起作用

目标:列出所有罐子中的文件.

这有效:

for f in `find . -name "*.jar"`; do jar tvf $f; done
Run Code Online (Sandbox Code Playgroud)

这也有效:

find . -name "*.jar" -exec jar tvf {} \;
Run Code Online (Sandbox Code Playgroud)

这不(它不打印任何输出):

find . -name "*.jar" | xargs jar tvf
Run Code Online (Sandbox Code Playgroud)

为什么后者不起作用?

bash xargs

11
推荐指数
3
解决办法
3053
查看次数

自动化路由的REST API文档

是否可以自动化Spray应用程序中的路径文档?

例如,是否有一个SBT插件可以生成降价描述它对喷雾路线的了解?

scala spray

10
推荐指数
1
解决办法
1752
查看次数