小编Mit*_*dge的帖子

我们可以在Java中创建无符号字节

我试图转换无符号的有符号字节.问题是我收到的数据是无符号的,Java不支持无符号字节,因此当它读取数据时,它将其视为已签名.

我尝试通过以下我从Stack Overflow获得的解决方案来转换它.

public static int unsignedToBytes(byte a)
{
    int b = a & 0xFF;
    return b;
}
Run Code Online (Sandbox Code Playgroud)

但是当它再次以字节转换时,我得到相同的签名数据.我试图将此数据用作Java函数的参数,该函数只接受一个字节作为参数,因此我不能使用任何其他数据类型.我该如何解决这个问题?

java unsigned

174
推荐指数
6
解决办法
26万
查看次数

NodeJs child_process工作目录

我试图在不同的目录中执行子进程,然后在其父目录中执行.

var exec = require('child_process').exec;

exec(
    'pwd',
    {
        cdw: someDirectoryVariable
    },
    function(error, stdout, stderr) {
        // ...
    }
);
Run Code Online (Sandbox Code Playgroud)

我正在做上面的事情(虽然当然运行"pwd"不是我最终想要做的).这将最终将父进程的pwd写入stdout,无论我为cdw选项提供什么值.

我错过了什么?

(我确实确保传递路径作为cwd选项实际存在)

javascript working-directory child-process node.js

46
推荐指数
1
解决办法
4万
查看次数

如何将MySQL查询结果存储到本地CSV文件中?

如何将MySQL查询结果存储到本地CSV文件中?我无法访问远程计算机.

mysql

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

将Flexbox放在部分元素上

我正在尝试使用flexbox将两个元素沿下图所示的蓝线垂直居中。

例子1

问题是第二个框和文本是一个div的一部分,而flexbox希望像这样对齐它们:

例子2

通过制作div position: relative和文本,position: absolute我能够实现我的目标,除了这会导致整个容器在计算容器高度时排除文本:

例子3

我如何正确地使这些元素居中,同时仍然允许容器具有容器中所有内容的高度?

所需结果:

所需结果

问题的示例:(虽然框居中,但容器不包含文本,如蓝色边框所示。)

.container {
  display: flex;
  align-items: center;
  padding: 10px;
  border: 4px solid #00aaff;
}

.big-box {
  width: 200px;
  height: 100px;
  margin-right: 20px;
  padding: 10px;
  border: 4px solid black;
}

.small-box {
  width: 150px;
  height: 50px;
  padding: 10px;
  border: 4px solid black;
}

.group {
  position: relative;
}

.group p {
  position: absolute;
  margin-top: 20px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="big-box">
    Lots of content...
  </div>

  <div class="group">
    <div class="small-box">
      Some …
Run Code Online (Sandbox Code Playgroud)

html css flexbox css-grid

7
推荐指数
2
解决办法
566
查看次数

Scene Builder中的自定义控件ClassNotFoundException

我通过扩展现有控件创建了一个新控件,我想在我的JavaFX场景中使用这个新控件.我希望能够使用Scene Builder编辑我的场景,但在将新控件添加到FXML文件后,我ClassNotFoundException在打开Scene Builder 时遇到了问题.

例如,这是我制作的一个类,它扩展了TextField:

RegexLimitingTextField.java

public class RegexLimitingTextField extends TextField {

    private String regexLimiter = ".*";

    public void setRegexLimiter(String regex) {
        this.regexLimiter = regex;
    }

    @Override
    public void replaceText(int start, int end, String text) {
        if (text.matches(regexLimiter))
            super.replaceText(start, end, text);
    }

    @Override
    public void replaceSelection(String replacement) {
        if (replacement.matches(regexLimiter))
            super.replaceSelection(replacement);
    }
}
Run Code Online (Sandbox Code Playgroud)

将此控件添加到我的FXML文件后...

sample.fxml

<?import javafx.scene.layout.GridPane?>
<?import sample.RegexLimitingTextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <RegexLimitingTextField fx:id="textField" text="Test" />
</GridPane>
Run Code Online (Sandbox Code Playgroud)

...加载Scene Builder 2.0时出现此错误:

Caused by: java.lang.ClassNotFoundException: …
Run Code Online (Sandbox Code Playgroud)

java javafx classnotfoundexception scenebuilder

6
推荐指数
2
解决办法
3472
查看次数

"当调用createEntityManagerFactory时,您无法在托管事务期间设置自动提交"

我试图通过在我的应用程序中分离模式来实现多租户.在这样做的过程中,我有一个Tenant包含a 的实体String schemaName,并且我有一个Singleton StartupEJB,可以EntityManagerFactory在启动时创建一个s 的映射; 每个工厂分配一个工厂Tenant.

这是我的EJB:

@Startup
@Singleton
public class TenantManagementServiceImpl implements TenantManagementService {

    private Map<Tenant, EntityManagerFactory> entityManagerFactoryMap;

    @PersistenceContext
    private EntityManager entityManager;

    @PostConstruct
    private void init()
    {
        buildEntityManagerFactories();
    }

    private List<Tenant> getAllTenants() {
        return entityManager.createNamedQuery("Tenant.getAll", Tenant.class).getResultList();
    }

    private void buildEntityManagerFactories() {
        entityManagerFactoryMap = new HashMap<>();

        for (Tenant tenant : getAllTenants()) {
            Map<String, String> properties = new HashMap<>();
            properties.put("hibernate.default_schema", tenant.getSchemaName());
            EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyApp", properties);
            entityManagerFactoryMap.putIfAbsent(tenant, entityManagerFactory);
        }
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

java jpa

6
推荐指数
1
解决办法
5649
查看次数

如何在 Spring Boot 中捕获非 MVC 和非 REST 异常

我已经能够找到无数关于如何在 Spring MVC 或 Spring REST 中捕获未处理的异常的教程,但我想知道的是如何在不使用 Spring Web 框架的情况捕获未处理的异常

我正在编写一个没有 Web 组件的应用程序,并且我不打算仅为异常处理而导入 Spring Web。

@Service抛出未处理的异常时,我需要捕获它,以便我可以将其正确记录到Raygun

例如,考虑一个故意抛出未捕获异常的服务中的这个方法:

@Scheduled(fixedDelay = 100)
public void doSomething() {
    throw new RuntimeException("Uh oh!");
}
Run Code Online (Sandbox Code Playgroud)

它的输出将是:

2017-08-16 00:19:40.202 ERROR 91168 --- [pool-1-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

java.lang.RuntimeException: Uh oh!
    at com.mitchtalmadge.example.ExampleService.doSomething(ClassSyncService.java:48) ~[classes/:na]
    at com.mitchtalmadge.example.ExampleService$$FastClassBySpringCGLIB$$1dd464d8.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669)
    ...
Run Code Online (Sandbox Code Playgroud)

我怎么抓住它?

没有简单的方法可以做到这一点吗?

java spring raygun

6
推荐指数
1
解决办法
2026
查看次数

根据输入的Enum值返回强制转换的对象

我试图制作一个采用Enum值并返回基于该Enum值强制转换为类的对象的方法。例如,我有一个名为ComponentType的枚举:

public enum ComponentType
{
    HEALTH(HealthComponent.class),
    HUNGER(HungerComponent.class);

    private Class<? extends Component> componentClass;

    private ComponentType(Class<? extends Component> componentClass)
    {
        this.componentClass = componentClass;
    }

    public Class<? extends Component> getComponentClass()
    {
        return componentClass;
    }
}
Run Code Online (Sandbox Code Playgroud)

“ HealthComponent”和“ HungerComponent”是两个类,它们都扩展了称为“ Component”的类。对于这个问题,它们内部的内容并不重要。

一个实体将具有为其分配的组件的列表(例如,一个实体可能有饥饿感,而另一个实体可能有健康状况,而另一个实体可能同时有健康状况)。

我的目标是在Entity内部创建一个方法,当传入ComponentType Enum中的值时,将返回转换为该值的对应类类型的Component对象。因此,如果传入ComponentType.HEALTH,则该方法将返回强制转换为HealthComponent的对象。这是我正在尝试的方法,但是不起作用:(编辑:请参见下文)

public <T extends Component> T getComponentByType(ComponentType type)
{
    Class<? extends Component> componentClass = type.getComponentClass();
    for(Component component : componentList)
    {
        if(component.getClass() == componentClass)
        {
            return (T) componentClass.cast(component);
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

对于上述方法,当传入ComponentType.HEALTH类型时:

entity.getComponentByType(ComponentType.HEALTH);
Run Code Online (Sandbox Code Playgroud)

将转换为强制转换为“ Component”而不是“ HealthComponent”的对象。我希望它返回转换为HealthComponent而不是Component的对象。

有什么办法吗?我觉得这应该是可能的。我尝试找到一种方法来执行此操作的原因是因为它看起来像在进行所有以下铸造:

HealthComponent component = (HealthComponent) …
Run Code Online (Sandbox Code Playgroud)

java generics enums casting

5
推荐指数
1
解决办法
818
查看次数

PSQLException:不能在自动提交模式下使用大对象

我正在使用 WildFly 10、Java EE、JPA 和 Hibernate。最近我将我的应用程序从 MySQL 迁移到 PostgreSQL。在使用 MySQL 时,我会使用以下方法将图像存储在我的实体中:

@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] image;
Run Code Online (Sandbox Code Playgroud)

这很好用,MySQL 使用 aLONGBLOB来存储数据。

切换到 PostgreSQL 后,列类型为OID,并且在持久化图像时收到此错误:

Caused by: org.postgresql.util.PSQLException: Large Objects may not be used in auto-commit mode.
at org.postgresql.largeobject.LargeObjectManager.createLO(LargeObjectManager.java:308)
at org.postgresql.largeobject.LargeObjectManager.createLO(LargeObjectManager.java:296)
at org.postgresql.jdbc.PgPreparedStatement.createBlob(PgPreparedStatement.java:1202)
at org.postgresql.jdbc.PgPreparedStatement.setBlob(PgPreparedStatement.java:1243)
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setBlob(WrappedPreparedStatement.java:1157)
at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$4$1.doBind(BlobTypeDescriptor.java:112)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:257)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:252)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:39)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2598)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2883)
... 131 more
Run Code Online (Sandbox Code Playgroud)

我以这种方式插入:

@PersistenceContext
EntityManager entityManager;

...

//Simple insert method...
this.entityManager.persist(entity);
Run Code Online (Sandbox Code Playgroud)

这是我的persistence.xml

<persistence …
Run Code Online (Sandbox Code Playgroud)

postgresql hibernate jpa wildfly

5
推荐指数
1
解决办法
7905
查看次数

运行WildFly Swarm Arquillian测试时出现ClassNotFoundException

(参考https://issues.jboss.org/projects/SWARM/issues/SWARM-767)

我正在尝试使用Arquillian测试我的WildFly Swarm应用程序.我有一个自定义Main类,用于我的Swarm和自定义部署.当我尝试运行我的测试时,我在maven的某些第三方库上收到ClassNotFoundException.

首先,这是我的测试:

@RunWith(Arquillian.class)
public class PasswordStorageTest {

    @CreateSwarm
    public static Swarm createSwarm() {
        return TestBuilder.createSwarm();
    }

    @Deployment
    public static WARArchive createDeployment() {
        return TestBuilder.createDeployment();
    }

    @Test
    public void testHashesAreUnique() throws PasswordStorage.CannotPerformOperationException {
        String password = UUID.randomUUID().toString();
        String hash1 = PasswordStorage.createHash(password);
        String hash2 = PasswordStorage.createHash(password);

        assertNotEquals("Hashes were not unique.", hash1, hash2);
    }

    @Test
    public void testVerifyPassword() throws PasswordStorage.CannotPerformOperationException, PasswordStorage.InvalidHashException {
        String password = UUID.randomUUID().toString();
        String hash = PasswordStorage.createHash(password);

        assertTrue("Password verification failed.", PasswordStorage.verifyPassword(password, hash));
    }

} …
Run Code Online (Sandbox Code Playgroud)

jboss-arquillian wildfly-swarm

5
推荐指数
0
解决办法
772
查看次数

使用依赖项卸载时的 useEffect 清理

我需要一种仅在组件卸载时运行 React useEffect 清理函数的方法,但使用组件的最新状态。

考虑以下示例:

const [foo, setFoo] = useState(true)

useEffect(() => {
  return () => {
    if(foo) 
      console.log("T") 
    else
      console.log("F")
  }
}, [])

...later:

setFoo(false)
Run Code Online (Sandbox Code Playgroud)

foo在此示例中,即使当前值为,也会在卸载时打印“T” false

您的第一个想法可能是添加foo到效果的依赖数组中,但这会导致效果清理两次:一次当状态更改为false(打印“T”)时,一次当组件卸载时(打印“F”) 。

我希望它只在组件卸载后打印“F”。

reactjs react-hooks

5
推荐指数
1
解决办法
2734
查看次数

具有多个Angular路径的循环依赖

我正在使用Webpack 4.15.1开发Angular 6.0.7应用程序.使用webpack-dev-server时应用程序运行良好,但是一旦我尝试使用生产模式构建它,它就会在HtmlWebpackPlugin发射阶段失败.

它必须与HtmlWebpackPlugin有关,因为如果我删除插件,那么javascript文件生成没有问题.它可能与生成的块数有关.

直到我向我的应用程序添加了两个新路由才发生这种情况.一旦我删除任何两个路由,应用程序编译正常.这是错误:

> webpack --config webpack/webpack.production.config.js --progress

clean-webpack-plugin: C:\example\WiFi-Setup\bin has been removed.
 95% emitting HtmlWebpackPluginUnhandled rejection Error: Cyclic dependency
    at visit (C:\example\WiFi-Setup\node_modules\html-webpack-plugin\node_modules\toposort\index.js:35:13)
    at visit (C:\example\WiFi-Setup\node_modules\html-webpack-plugin\node_modules\toposort\index.js:53:9)
    at visit (C:\example\WiFi-Setup\node_modules\html-webpack-plugin\node_modules\toposort\index.js:53:9)
    at Function.toposort [as array] (C:\example\WiFi-Setup\node_modules\html-webpack-plugin\node_modules\toposort\index.js:22:22)
    at Object.module.exports.dependency (C:\example\WiFi-Setup\node_modules\html-webpack-plugin\lib\chunksorter.js:50:35)
    at HtmlWebpackPlugin.sortChunks (C:\example\WiFi-Setup\node_modules\html-webpack-plugin\index.js:364:35)
    at C:\example\WiFi-Setup\node_modules\html-webpack-plugin\index.js:113:21
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\example\WiFi-Setup\node_modules\tapable\lib\HookCodeFactory.js:24:12), <anonymous>:12:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (C:\example\WiFi-Setup\node_modules\tapable\lib\Hook.js:35:21)
    at Compiler.emitAssets (C:\example\WiFi-Setup\node_modules\webpack\lib\Compiler.js:364:19)
    at onCompiled (C:\example\WiFi-Setup\node_modules\webpack\lib\Compiler.js:231:9)
    at hooks.afterCompile.callAsync.err (C:\example\WiFi-Setup\node_modules\webpack\lib\Compiler.js:553:14)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\example\WiFi-Setup\node_modules\tapable\lib\HookCodeFactory.js:24:12), <anonymous>:15:1)
    at AsyncSeriesHook.lazyCompileHook …
Run Code Online (Sandbox Code Playgroud)

webpack html-webpack-plugin angular

0
推荐指数
1
解决办法
1015
查看次数